Merge branch 'sb/color-h-cleanup'
[git/debian.git] / t / perf / aggregate.perl
blob821cf1498b78bbb5b43b2bda32bbcc88d580c31b
1 #!/usr/bin/perl
3 use lib '../../perl/build/lib';
4 use strict;
5 use warnings;
6 use JSON;
7 use Git;
9 sub get_times {
10 my $name = shift;
11 open my $fh, "<", $name or return undef;
12 my $line = <$fh>;
13 return undef if not defined $line;
14 close $fh or die "cannot close $name: $!";
15 $line =~ /^(?:(\d+):)?(\d+):(\d+(?:\.\d+)?) (\d+(?:\.\d+)?) (\d+(?:\.\d+)?)$/
16 or die "bad input line: $line";
17 my $rt = ((defined $1 ? $1 : 0.0)*60+$2)*60+$3;
18 return ($rt, $4, $5);
21 sub format_times {
22 my ($r, $u, $s, $firstr) = @_;
23 if (!defined $r) {
24 return "<missing>";
26 my $out = sprintf "%.2f(%.2f+%.2f)", $r, $u, $s;
27 if (defined $firstr) {
28 if ($firstr > 0) {
29 $out .= sprintf " %+.1f%%", 100.0*($r-$firstr)/$firstr;
30 } elsif ($r == 0) {
31 $out .= " =";
32 } else {
33 $out .= " +inf";
36 return $out;
39 my (@dirs, %dirnames, %dirabbrevs, %prefixes, @tests,
40 $codespeed, $subsection, $reponame);
41 while (scalar @ARGV) {
42 my $arg = $ARGV[0];
43 my $dir;
44 if ($arg eq "--codespeed") {
45 $codespeed = 1;
46 shift @ARGV;
47 next;
49 if ($arg eq "--subsection") {
50 shift @ARGV;
51 $subsection = $ARGV[0];
52 shift @ARGV;
53 if (! $subsection) {
54 die "empty subsection";
56 next;
58 if ($arg eq "--reponame") {
59 shift @ARGV;
60 $reponame = $ARGV[0];
61 shift @ARGV;
62 if (! $reponame) {
63 die "empty reponame";
65 next;
67 last if -f $arg or $arg eq "--";
68 if (! -d $arg) {
69 my $rev = Git::command_oneline(qw(rev-parse --verify), $arg);
70 $dir = "build/".$rev;
71 } else {
72 $arg =~ s{/*$}{};
73 $dir = $arg;
74 $dirabbrevs{$dir} = $dir;
76 push @dirs, $dir;
77 $dirnames{$dir} = $arg;
78 my $prefix = $dir;
79 $prefix =~ tr/^a-zA-Z0-9/_/c;
80 $prefixes{$dir} = $prefix . '.';
81 shift @ARGV;
84 if (not @dirs) {
85 @dirs = ('.');
87 $dirnames{'.'} = $dirabbrevs{'.'} = "this tree";
88 $prefixes{'.'} = '';
90 shift @ARGV if scalar @ARGV and $ARGV[0] eq "--";
92 @tests = @ARGV;
93 if (not @tests) {
94 @tests = glob "p????-*.sh";
97 my $resultsdir = "test-results";
99 if (! $subsection and
100 exists $ENV{GIT_PERF_SUBSECTION} and
101 $ENV{GIT_PERF_SUBSECTION} ne "") {
102 $subsection = $ENV{GIT_PERF_SUBSECTION};
105 if ($subsection) {
106 $resultsdir .= "/" . $subsection;
109 my @subtests;
110 my %shorttests;
111 for my $t (@tests) {
112 $t =~ s{(?:.*/)?(p(\d+)-[^/]+)\.sh$}{$1} or die "bad test name: $t";
113 my $n = $2;
114 my $fname = "$resultsdir/$t.subtests";
115 open my $fp, "<", $fname or die "cannot open $fname: $!";
116 for (<$fp>) {
117 chomp;
118 /^(\d+)$/ or die "malformed subtest line: $_";
119 push @subtests, "$t.$1";
120 $shorttests{"$t.$1"} = "$n.$1";
122 close $fp or die "cannot close $fname: $!";
125 sub read_descr {
126 my $name = shift;
127 open my $fh, "<", $name or return "<error reading description>";
128 binmode $fh, ":utf8" or die "PANIC on binmode: $!";
129 my $line = <$fh>;
130 close $fh or die "cannot close $name";
131 chomp $line;
132 return $line;
135 sub have_duplicate {
136 my %seen;
137 for (@_) {
138 return 1 if exists $seen{$_};
139 $seen{$_} = 1;
141 return 0;
143 sub have_slash {
144 for (@_) {
145 return 1 if m{/};
147 return 0;
150 sub print_default_results {
151 my %descrs;
152 my $descrlen = 4; # "Test"
153 for my $t (@subtests) {
154 $descrs{$t} = $shorttests{$t}.": ".read_descr("$resultsdir/$t.descr");
155 $descrlen = length $descrs{$t} if length $descrs{$t}>$descrlen;
158 my %newdirabbrevs = %dirabbrevs;
159 while (!have_duplicate(values %newdirabbrevs)) {
160 %dirabbrevs = %newdirabbrevs;
161 last if !have_slash(values %dirabbrevs);
162 %newdirabbrevs = %dirabbrevs;
163 for (values %newdirabbrevs) {
164 s{^[^/]*/}{};
168 my %times;
169 my @colwidth = ((0)x@dirs);
170 for my $i (0..$#dirs) {
171 my $d = $dirs[$i];
172 my $w = length (exists $dirabbrevs{$d} ? $dirabbrevs{$d} : $dirnames{$d});
173 $colwidth[$i] = $w if $w > $colwidth[$i];
175 for my $t (@subtests) {
176 my $firstr;
177 for my $i (0..$#dirs) {
178 my $d = $dirs[$i];
179 $times{$prefixes{$d}.$t} = [get_times("$resultsdir/$prefixes{$d}$t.times")];
180 my ($r,$u,$s) = @{$times{$prefixes{$d}.$t}};
181 my $w = length format_times($r,$u,$s,$firstr);
182 $colwidth[$i] = $w if $w > $colwidth[$i];
183 $firstr = $r unless defined $firstr;
186 my $totalwidth = 3*@dirs+$descrlen;
187 $totalwidth += $_ for (@colwidth);
189 printf "%-${descrlen}s", "Test";
190 for my $i (0..$#dirs) {
191 my $d = $dirs[$i];
192 printf " %-$colwidth[$i]s", (exists $dirabbrevs{$d} ? $dirabbrevs{$d} : $dirnames{$d});
194 print "\n";
195 print "-"x$totalwidth, "\n";
196 for my $t (@subtests) {
197 printf "%-${descrlen}s", $descrs{$t};
198 my $firstr;
199 for my $i (0..$#dirs) {
200 my $d = $dirs[$i];
201 my ($r,$u,$s) = @{$times{$prefixes{$d}.$t}};
202 printf " %-$colwidth[$i]s", format_times($r,$u,$s,$firstr);
203 $firstr = $r unless defined $firstr;
205 print "\n";
209 sub print_codespeed_results {
210 my ($subsection) = @_;
212 my $project = "Git";
214 my $executable = `uname -s -m`;
215 chomp $executable;
217 if ($subsection) {
218 $executable .= ", " . $subsection;
221 my $environment;
222 if ($reponame) {
223 $environment = $reponame;
224 } elsif (exists $ENV{GIT_PERF_REPO_NAME} and $ENV{GIT_PERF_REPO_NAME} ne "") {
225 $environment = $ENV{GIT_PERF_REPO_NAME};
226 } elsif (exists $ENV{GIT_TEST_INSTALLED} and $ENV{GIT_TEST_INSTALLED} ne "") {
227 $environment = $ENV{GIT_TEST_INSTALLED};
228 $environment =~ s|/bin-wrappers$||;
229 } else {
230 $environment = `uname -r`;
231 chomp $environment;
234 my @data;
236 for my $t (@subtests) {
237 for my $d (@dirs) {
238 my $commitid = $prefixes{$d};
239 $commitid =~ s/^build_//;
240 $commitid =~ s/\.$//;
241 my ($result_value, $u, $s) = get_times("$resultsdir/$prefixes{$d}$t.times");
243 my %vals = (
244 "commitid" => $commitid,
245 "project" => $project,
246 "branch" => $dirnames{$d},
247 "executable" => $executable,
248 "benchmark" => $shorttests{$t} . " " . read_descr("$resultsdir/$t.descr"),
249 "environment" => $environment,
250 "result_value" => $result_value,
252 push @data, \%vals;
256 print to_json(\@data, {utf8 => 1, pretty => 1, canonical => 1}), "\n";
259 binmode STDOUT, ":utf8" or die "PANIC on binmode: $!";
261 if ($codespeed) {
262 print_codespeed_results($subsection);
263 } else {
264 print_default_results();