ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
Revision: 1.6
Committed: Sat Mar 24 22:55:16 2007 UTC (17 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-0_7
Changes since 1.5: +9 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3     # Usage: bench json-file
4    
5     use JSON;
6     use JSON::DWIW;
7     use JSON::PC;
8     use JSON::XS;
9     use JSON::Syck;
10    
11     use Time::HiRes;
12     use List::Util;
13    
14     use utf8;
15    
16     my $dwiw = new JSON::DWIW;
17     my $pc = new JSON::PC;
18 root 1.6 my $xs2 = JSON::XS->new->utf8->pretty->canonical;
19     my $xs3 = JSON::XS->new->utf8->shrink;
20 root 1.1
21     my $json; # the test string
22    
23     local $/;
24     $json = <>;
25    
26 root 1.6 #srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]);
27 root 1.3
28 root 1.1 #if (1) {
29     # use Storable;
30     # open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
31     # my $faces = Storable::thaw do { <$fh> };
32     # $json = objToJson $faces;
33     # open my $fh2, ">:unix", "faces.json" or die "$!";
34     # print $fh2 $json;
35     # warn length $json;
36     #}
37    
38     my %tst = (
39 root 1.6 "JSON" => ['objToJson $perl' , 'jsonToObj $json'],
40     "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'],
41     "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
42     "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'],
43 root 1.1 "JSON::XS" => ['to_json $perl' , 'from_json $json'],
44 root 1.6 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'],
45     "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'],
46 root 1.1 );
47    
48     sub bench($) {
49     my ($code) = @_;
50    
51     my $perl = jsonToObj $json;
52 root 1.4 my $count = 20;
53 root 1.5 my $times = 25;
54 root 1.1
55     my $cent = eval "sub { " . (join ";", ($code) x $count) . "}";
56     $cent->();
57    
58     my @meas;
59    
60     while () {
61     push @meas, Time::HiRes::time;
62     $cent->();
63     $meas[-1] = Time::HiRes::time - $meas[-1];
64    
65     my $mean = (List::Util::sum @meas) / @meas;
66     if (@meas >= $times) {
67     @meas = grep $_ > $mean * 0.9 && $_ < $mean * 1.1, @meas;
68     my @meas = grep $_ > $mean * 0.9999 && $_ < $mean * 1.1111, @meas;
69     if (@meas >= $times) {
70     return $count / $mean;
71     }
72     }
73     }
74     }
75    
76     printf "%-10s | %10s | %10s |\n", "module", "encode", "decode";
77     printf "-----------|------------|------------|\n";
78     for my $module (sort keys %tst) {
79     my $enc = bench $tst{$module}[0];
80     my $dec = bench $tst{$module}[1];
81    
82     printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec;
83     }
84     printf "-----------+------------+------------+\n";
85