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

# Content
1 #!/opt/bin/perl
2
3 # Usage: bench json-file
4
5 use JSON;
6 use JSON::DWIW;
7 use JSON::PC;
8 use JSON::XS qw(to_json from_json);
9 use JSON::Syck;
10 use Storable ();
11
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 my $xs2 = JSON::XS->new->utf8->pretty->canonical;
20 my $xs3 = JSON::XS->new->utf8->shrink;
21
22 my $json; # the test string
23
24 local $/;
25 $json = <>;
26
27 # fix syck-brokenised stuff
28 $json = JSON::XS->new->ascii(1)->encode (JSON::Syck::Load $json);
29
30 #srand 0; $json = JSON::XS->new->utf8(1)->ascii(0)->encode ([join "", map +(chr rand 255), 0..2047]);
31
32 #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 # "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
53 sub bench($) {
54 my ($code) = @_;
55
56 my $perl = jsonToObj $json;
57 my $pst = Storable::nfreeze $perl;
58
59 my $count = 2;
60 my $times = 25;
61
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