ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
Revision: 1.9
Committed: Tue Apr 10 03:47:26 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
CVS Tags: rel-1_2, rel-1_21, rel-1_22
Changes since 1.8: +8 -1 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 root 1.7 use JSON::XS qw(to_json from_json);
9 root 1.1 use JSON::Syck;
10 root 1.9 use Storable ();
11 root 1.1
12     use Time::HiRes;
13     use List::Util;
14    
15     use utf8;
16    
17     my $dwiw = new JSON::DWIW;
18     my $pc = new JSON::PC;
19 root 1.6 my $xs2 = JSON::XS->new->utf8->pretty->canonical;
20     my $xs3 = JSON::XS->new->utf8->shrink;
21 root 1.1
22     my $json; # the test string
23    
24     local $/;
25     $json = <>;
26    
27 root 1.9 # fix syck-brokenised stuff
28     $json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json);
29    
30 root 1.6 #srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]);
31 root 1.3
32 root 1.1 #if (1) {
33     # use Storable;
34     # open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
35     # my $faces = Storable::thaw do { <$fh> };
36     # $json = objToJson $faces;
37     # open my $fh2, ">:unix", "faces.json" or die "$!";
38     # print $fh2 $json;
39     # warn length $json;
40     #}
41    
42     my %tst = (
43 root 1.8 # "JSON" => ['objToJson $perl' , 'jsonToObj $json'],
44 root 1.6 "JSON::DWIW" => ['$dwiw->to_json ($perl)', '$dwiw->from_json ($json)'],
45     "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
46     "JSON::Syck" => ['JSON::Syck::Dump $perl', 'JSON::Syck::Load $json'],
47 root 1.1 "JSON::XS" => ['to_json $perl' , 'from_json $json'],
48 root 1.6 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'],
49     "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'],
50 root 1.9 "Storable" => ['Storable::nfreeze $perl', 'Storable::thaw $pst'],
51 root 1.1 );
52    
53     sub bench($) {
54     my ($code) = @_;
55    
56     my $perl = jsonToObj $json;
57 root 1.9 my $pst = Storable::nfreeze $perl;
58    
59     my $count = 2;
60 root 1.5 my $times = 25;
61 root 1.1
62     my $cent = eval "sub { " . (join ";", ($code) x $count) . "}";
63     $cent->();
64    
65     my @meas;
66    
67     while () {
68     push @meas, Time::HiRes::time;
69     $cent->();
70     $meas[-1] = Time::HiRes::time - $meas[-1];
71    
72     my $mean = (List::Util::sum @meas) / @meas;
73     if (@meas >= $times) {
74     @meas = grep $_ > $mean * 0.9 && $_ < $mean * 1.1, @meas;
75     my @meas = grep $_ > $mean * 0.9999 && $_ < $mean * 1.1111, @meas;
76     if (@meas >= $times) {
77     return $count / $mean;
78     }
79     }
80     }
81     }
82    
83     printf "%-10s | %10s | %10s |\n", "module", "encode", "decode";
84     printf "-----------|------------|------------|\n";
85     for my $module (sort keys %tst) {
86     my $enc = bench $tst{$module}[0];
87     my $dec = bench $tst{$module}[1];
88    
89     printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec;
90     }
91     printf "-----------+------------+------------+\n";
92