ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/eg/bench
Revision: 1.3
Committed: Fri Mar 23 15:10:55 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
Changes since 1.2: +4 -2 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;
9 use JSON::Syck;
10
11 use Time::HiRes;
12 use List::Util;
13
14 use utf8;
15
16 my $dwiw = new JSON::DWIW;
17 my $pc = new JSON::PC;
18 my $xs = JSON::XS->new->pretty (1)->canonical (1);
19
20 my $json; # the test string
21
22 local $/;
23 $json = <>;
24
25 $json = to_json [join "", map +(chr rand 255), 0..8191];
26
27 #if (1) {
28 # use Storable;
29 # open my $fh, "<:unix", "/opt/crossfire/share/cfserver/faces" or die "$!";
30 # my $faces = Storable::thaw do { <$fh> };
31 # $json = objToJson $faces;
32 # open my $fh2, ">:unix", "faces.json" or die "$!";
33 # print $fh2 $json;
34 # warn length $json;
35 #}
36
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($) {
47 my ($code) = @_;
48
49 my $perl = jsonToObj $json;
50 my $count = 10;
51 my $times = 15;
52
53 my $cent = eval "sub { " . (join ";", ($code) x $count) . "}";
54 $cent->();
55
56 my @meas;
57
58 while () {
59 push @meas, Time::HiRes::time;
60 $cent->();
61 $meas[-1] = Time::HiRes::time - $meas[-1];
62
63 my $mean = (List::Util::sum @meas) / @meas;
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 printf "%-10s | %10s | %10s |\n", "module", "encode", "decode";
75 printf "-----------|------------|------------|\n";
76 for my $module (sort keys %tst) {
77 my $enc = bench $tst{$module}[0];
78 my $dec = bench $tst{$module}[1];
79
80 printf "%-10s | %10.3f | %10.3f |\n", $module, $enc, $dec;
81 }
82 printf "-----------+------------+------------+\n";
83