ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
(Generate patch)

Comparing JSON-XS/eg/bench (file contents):
Revision 1.9 by root, Tue Apr 10 03:47:26 2007 UTC vs.
Revision 1.22 by root, Tue Jan 19 01:02:19 2010 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3# Usage: bench json-file 3# Usage: bench json-file
4 4
5# which modules to test (JSON::PP usually excluded because its so slow)
6my %tst = (
7# "JSON" => ['JSON::encode_json $perl' , 'JSON::decode_json $json'],
8 "JSON::PP" => ['$pp->encode ($perl)' , '$pp->decode ($json)'],
9 "JSON::DWIW/FJ" => ['$dwiw->to_json ($perl)' , '$dwiw->from_json ($json)'],
10 "JSON::DWIW/DS" => ['$dwiw->to_json ($perl)' , 'JSON::DWIW::deserialize $json'],
11# "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
12 "JSON::Syck" => ['JSON::Syck::Dump $perl' , 'JSON::Syck::Load $json'],
13 "JSON::XS" => ['encode_json $perl' , 'decode_json $json'],
14 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'],
15 "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'],
16 "Storable" => ['Storable::nfreeze $perl' , 'Storable::thaw $pst'],
17);
18
5use JSON; 19use JSON ();
6use JSON::DWIW; 20use JSON::DWIW;
7use JSON::PC; 21use JSON::PC;
22use JSON::PP ();
8use JSON::XS qw(to_json from_json); 23use JSON::XS qw(encode_json decode_json);
9use JSON::Syck; 24use JSON::Syck;
10use Storable (); 25use Storable ();
11 26
12use Time::HiRes; 27use Time::HiRes;
13use List::Util; 28use List::Util;
14 29
15use utf8; 30use utf8;
16 31
17my $dwiw = new JSON::DWIW; 32my $dwiw = new JSON::DWIW;
18my $pc = new JSON::PC; 33my $pc = new JSON::PC;
34my $pp = JSON::PP->new->max_depth (512);
19my $xs2 = JSON::XS->new->utf8->pretty->canonical; 35my $xs2 = JSON::XS->new->utf8->pretty->canonical;
20my $xs3 = JSON::XS->new->utf8->shrink; 36my $xs3 = JSON::XS->new->utf8->shrink;
21 37
22my $json; # the test string 38my $json; # the test string
23 39
24local $/; 40local $/;
25$json = <>; 41$json = <>;
26 42
27# fix syck-brokenised stuff 43# fix syck-brokenised stuff
28$json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json); 44#$json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json);
29 45
30#srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]); 46#srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]);
31 47
32#if (1) { 48#if (1) {
33# use Storable; 49# use Storable;
37# open my $fh2, ">:unix", "faces.json" or die "$!"; 53# open my $fh2, ">:unix", "faces.json" or die "$!";
38# print $fh2 $json; 54# print $fh2 $json;
39# warn length $json; 55# warn length $json;
40#} 56#}
41 57
42my %tst = (
43# "JSON" => ['objToJson $perl' , 'jsonToObj $json'],
44 "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 "JSON::XS" => ['to_json $perl' , 'from_json $json'],
48 "JSON::XS/2" => ['$xs2->encode ($perl)' , '$xs2->decode ($json)'],
49 "JSON::XS/3" => ['$xs3->encode ($perl)' , '$xs3->decode ($json)'],
50 "Storable" => ['Storable::nfreeze $perl', 'Storable::thaw $pst'],
51);
52
53sub bench($) { 58sub bench($) {
54 my ($code) = @_; 59 my ($code) = @_;
55 60
56 my $perl = jsonToObj $json; 61 my $pst = Storable::nfreeze JSON::XS::decode_json $json; # seperately decode as storable stringifies :/
57 my $pst = Storable::nfreeze $perl; 62 my $perl = JSON::XS::decode_json $json;
58 63
59 my $count = 2; 64 my $count = 5;
60 my $times = 25; 65 my $times = 200;
61 66
62 my $cent = eval "sub { " . (join ";", ($code) x $count) . "}"; 67 my $cent = eval "sub { my \$t = Time::HiRes::time; " . (join ";", ($code) x $count) . "; Time::HiRes::time - \$t }";
63 $cent->(); 68 $cent->();
64 69
65 my @meas; 70 my $min = 1e99;
66 71
67 while () { 72 for (1..$times) {
68 push @meas, Time::HiRes::time;
69 $cent->(); 73 my $t = $cent->();
70 $meas[-1] = Time::HiRes::time - $meas[-1];
71 74
72 my $mean = (List::Util::sum @meas) / @meas; 75 $min = $t if $t < $min;
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 } 76 }
77
78 return $count / $min;
81} 79}
82 80
83printf "%-10s | %10s | %10s |\n", "module", "encode", "decode"; 81printf "%-13s | %10s | %10s |\n", "module", "encode", "decode";
84printf "-----------|------------|------------|\n"; 82printf "--------------|------------|------------|\n";
85for my $module (sort keys %tst) { 83for my $module (sort keys %tst) {
86 my $enc = bench $tst{$module}[0]; 84 my $enc = bench $tst{$module}[0];
87 my $dec = bench $tst{$module}[1]; 85 my $dec = bench $tst{$module}[1];
88 86
89 printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec; 87 printf "%-13s | %10.3f | %10.3f |\n", $module, $enc, $dec;
90} 88}
91printf "-----------+------------+------------+\n"; 89printf "--------------+------------+------------+\n";
92 90

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines