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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines