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