ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
Revision: 1.19
Committed: Wed Mar 19 15:17:54 2008 UTC (16 years, 2 months ago) by root
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.18: +4 -4 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 root 1.10 # which modules to test (JSON usually excluded because its so slow)
6     my %tst = (
7 root 1.19 "JSON" => ['JSON::encode_json $perl' , 'JSON::decode_json $json'],
8     "JSON::PP" => ['$pp->encode ($perl)' , '$pp->decode ($json)'],
9 root 1.16 "JSON::DWIW" => ['$dwiw->to_json ($perl)' , '$dwiw->from_json ($json)'],
10 root 1.19 "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
11 root 1.16 "JSON::Syck" => ['JSON::Syck::Dump $perl' , 'JSON::Syck::Load $json'],
12 root 1.17 "JSON::XS" => ['encode_json $perl' , 'decode_json $json'],
13 root 1.13 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'],
14     "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'],
15     "Storable" => ['Storable::nfreeze $perl' , 'Storable::thaw $pst'],
16 root 1.10 );
17    
18 root 1.13 use JSON ();
19 root 1.1 use JSON::DWIW;
20     use JSON::PC;
21 root 1.13 use JSON::PP ();
22 root 1.17 use JSON::XS qw(encode_json decode_json);
23 root 1.1 use JSON::Syck;
24 root 1.9 use Storable ();
25 root 1.1
26     use Time::HiRes;
27     use List::Util;
28    
29     use utf8;
30    
31     my $dwiw = new JSON::DWIW;
32     my $pc = new JSON::PC;
33 root 1.13 my $pp = JSON::PP->new->max_depth (512);
34 root 1.6 my $xs2 = JSON::XS->new->utf8->pretty->canonical;
35     my $xs3 = JSON::XS->new->utf8->shrink;
36 root 1.1
37     my $json; # the test string
38    
39     local $/;
40     $json = <>;
41    
42 root 1.9 # fix syck-brokenised stuff
43 root 1.18 #$json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json);
44 root 1.9
45 root 1.6 #srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]);
46 root 1.3
47 root 1.1 #if (1) {
48     # use Storable;
49     # open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
50     # my $faces = Storable::thaw do { <$fh> };
51     # $json = objToJson $faces;
52     # open my $fh2, ">:unix", "faces.json" or die "$!";
53     # print $fh2 $json;
54     # warn length $json;
55     #}
56    
57     sub bench($) {
58     my ($code) = @_;
59    
60 root 1.19 my $pst = Storable::nfreeze JSON::XS::decode_json $json; # seperately decode as storable stringifies :/
61 root 1.17 my $perl = JSON::XS::decode_json $json;
62 root 1.9
63 root 1.10 my $count = 5;
64     my $times = 200;
65 root 1.1
66 root 1.10 my $cent = eval "sub { my \$t = Time::HiRes::time; " . (join ";", ($code) x $count) . "; Time::HiRes::time - \$t }";
67 root 1.1 $cent->();
68    
69 root 1.10 my $min = 1e99;
70 root 1.1
71 root 1.10 for (1..$times) {
72     my $t = $cent->();
73    
74     $min = $t if $t < $min;
75 root 1.1 }
76 root 1.10
77     return $count / $min;
78 root 1.1 }
79    
80     printf "%-10s | %10s | %10s |\n", "module", "encode", "decode";
81     printf "-----------|------------|------------|\n";
82     for my $module (sort keys %tst) {
83     my $enc = bench $tst{$module}[0];
84     my $dec = bench $tst{$module}[1];
85    
86     printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec;
87     }
88     printf "-----------+------------+------------+\n";
89