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