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