pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / scripts / compare.shingle.benchmark.tables.pl
1 #!/usr/bin/perl
2 #
3 # Licensed to the Apache Software Foundation (ASF) under one or more
4 # contributor license agreements.  See the NOTICE file distributed with
5 # this work for additional information regarding copyright ownership.
6 # The ASF licenses this file to You under the Apache License, Version 2.0
7 # (the "License"); you may not use this file except in compliance with
8 # the License.  You may obtain a copy of the License at
9
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # ------------------------------------------
19 # compare.shingle.benchmark.jira.tables.pl
20 #
21 # Takes as cmdline parameters two JIRA-formatted benchmark results, as produced
22 # by shingle.bm2jira.pl (located in the same directory as this script), and
23 # outputs a third JIRA-formatted comparison table.
24 #
25 # The difference is calculated as a percentage:
26 #
27 #   100 * (unpatched-elapsed - patched-elapsed / patched-elapsed)
28 #
29 # where (un)patched-elapsed values have had the no-shingle-filter 
30 # (StandardAnalyzer) elapsed time subtracted from them.
31 #
32 #
33 # Example shingle.bm2jira.pl output:
34 # ----------------------------------
35 # JAVA:
36 # java version "1.5.0_15"
37 # Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
38 # Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_15-b04, mixed mode)
39 #
40 # OS:
41 # cygwin
42 # WinVistaService Pack 2
43 # Service Pack 26060022202561
44 #
45 # ||Max Shingle Size||Unigrams?||Elapsed||
46 # |1 (Unigrams)|yes|2.19s|
47 # |2|no|4.74s|
48 # |2|yes|4.90s|
49 # |4|no|5.82s|
50 # |4|yes|5.97s|
51
52 use strict;
53 use warnings;
54
55 my $usage = "Usage: $0 <unpatched-file> <patched-file>\n";
56
57 die $usage unless ($#ARGV == 1 && -f $ARGV[0] && -f $ARGV[1]);
58
59 my %stats = ();
60
61 open UNPATCHED, "<$ARGV[0]" || die "ERROR opening '$ARGV[0]': $!";
62 my $table_encountered = 0;
63 my $standard_analyzer_elapsed = 0;
64 my %unpatched_stats = ();
65 my %patched_stats = ();
66 while (<UNPATCHED>) {
67   unless ($table_encountered) {
68     if (/\Q||Max Shingle Size||Unigrams?||Elapsed||\E/) {
69       $table_encountered = 1;
70     } else {
71       print;
72     }
73   } elsif (/\|([^|]+)\|([^|]+)\|([\d.]+)s\|/) {
74     my $max_shingle_size = $1;
75     my $output_unigrams = $2;
76     my $elapsed = $3;
77     if ($max_shingle_size =~ /Unigrams/) {
78       $standard_analyzer_elapsed = $elapsed;
79     } else {
80       $unpatched_stats{$max_shingle_size}{$output_unigrams} = $elapsed;
81     }
82   }
83 }
84 close UNPATCHED;
85
86 open PATCHED, "<$ARGV[1]" || die "ERROR opening '$ARGV[1]': $!";
87 while (<PATCHED>) {
88   if (/\|([^|]+)\|([^|]+)\|([\d.]+)s\|/) {
89     my $max_shingle_size = $1;
90     my $output_unigrams = $2;
91     my $elapsed = $3;
92     if ($max_shingle_size =~ /Unigrams/) {
93       $standard_analyzer_elapsed = $elapsed
94          if ($elapsed < $standard_analyzer_elapsed);
95     } else {
96       $patched_stats{$max_shingle_size}{$output_unigrams} = $elapsed;
97     }
98   }
99 }
100 close PATCHED;
101
102 print "||Max Shingle Size||Unigrams?||Unpatched||Patched||StandardAnalyzer||Improvement||\n";
103 for my $max_shingle_size (sort { $a <=> $b } keys %unpatched_stats) {
104   for my $output_unigrams (sort keys %{$unpatched_stats{$max_shingle_size}}) {
105     my $improvement 
106       = ( $unpatched_stats{$max_shingle_size}{$output_unigrams}
107         - $patched_stats{$max_shingle_size}{$output_unigrams})
108       / ( $patched_stats{$max_shingle_size}{$output_unigrams}
109         - $standard_analyzer_elapsed);
110     $improvement = int($improvement * 1000 + .5) / 10; # Round and truncate
111     printf "|$max_shingle_size|$output_unigrams"
112           ."|$unpatched_stats{$max_shingle_size}{$output_unigrams}s"
113           ."|$patched_stats{$max_shingle_size}{$output_unigrams}s"
114           ."|${standard_analyzer_elapsed}s|%2.1f%%|\n", $improvement;
115   }
116 }