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.16 by root, Tue Aug 28 02:06:06 2007 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 usually excluded because its so slow)
6my %tst = (
7# "JSON" => ['JSON::objToJson $perl' , 'JSON::jsonToObj $json'],
8# "JSON::PP" => ['$pp->encode ($perl)' , '$pp->decode ($json)'],
9 "JSON::DWIW" => ['$dwiw->to_json ($perl)' , '$dwiw->from_json ($json)'],
10# "JSON::PC" => ['$pc->convert ($perl)' , '$pc->parse ($json)'],
11 "JSON::Syck" => ['JSON::Syck::Dump $perl' , 'JSON::Syck::Load $json'],
12 "JSON::XS" => ['to_json $perl' , 'from_json $json'],
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);
17
5use JSON; 18use JSON ();
6use JSON::DWIW; 19use JSON::DWIW;
7use JSON::PC; 20use JSON::PC;
21use JSON::PP ();
8use JSON::XS qw(to_json from_json); 22use JSON::XS qw(to_json from_json);
9use JSON::Syck; 23use JSON::Syck;
10use Storable (); 24use Storable ();
11 25
12use Time::HiRes; 26use Time::HiRes;
14 28
15use utf8; 29use utf8;
16 30
17my $dwiw = new JSON::DWIW; 31my $dwiw = new JSON::DWIW;
18my $pc = new JSON::PC; 32my $pc = new JSON::PC;
33my $pp = JSON::PP->new->max_depth (512);
19my $xs2 = JSON::XS->new->utf8->pretty->canonical; 34my $xs2 = JSON::XS->new->utf8->pretty->canonical;
20my $xs3 = JSON::XS->new->utf8->shrink; 35my $xs3 = JSON::XS->new->utf8->shrink;
21 36
22my $json; # the test string 37my $json; # the test string
23 38
37# open my $fh2, ">:unix", "faces.json" or die "$!"; 52# open my $fh2, ">:unix", "faces.json" or die "$!";
38# print $fh2 $json; 53# print $fh2 $json;
39# warn length $json; 54# warn length $json;
40#} 55#}
41 56
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($) { 57sub bench($) {
54 my ($code) = @_; 58 my ($code) = @_;
55 59
56 my $perl = jsonToObj $json; 60 my $perl = JSON::XS::from_json $json;
57 my $pst = Storable::nfreeze $perl; 61 my $pst = Storable::nfreeze $perl;
58 62
59 my $count = 2; 63 my $count = 5;
60 my $times = 25; 64 my $times = 200;
61 65
62 my $cent = eval "sub { " . (join ";", ($code) x $count) . "}"; 66 my $cent = eval "sub { my \$t = Time::HiRes::time; " . (join ";", ($code) x $count) . "; Time::HiRes::time - \$t }";
63 $cent->(); 67 $cent->();
64 68
65 my @meas; 69 my $min = 1e99;
66 70
67 while () { 71 for (1..$times) {
68 push @meas, Time::HiRes::time;
69 $cent->(); 72 my $t = $cent->();
70 $meas[-1] = Time::HiRes::time - $meas[-1];
71 73
72 my $mean = (List::Util::sum @meas) / @meas; 74 $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 } 75 }
76
77 return $count / $min;
81} 78}
82 79
83printf "%-10s | %10s | %10s |\n", "module", "encode", "decode"; 80printf "%-10s | %10s | %10s |\n", "module", "encode", "decode";
84printf "-----------|------------|------------|\n"; 81printf "-----------|------------|------------|\n";
85for my $module (sort keys %tst) { 82for my $module (sort keys %tst) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines