forked from AndyA/Test-Harness
-
Notifications
You must be signed in to change notification settings - Fork 1
/
by_stage.pl
executable file
·127 lines (107 loc) · 3.09 KB
/
by_stage.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl
use strict;
use warnings;
use IPC::Open3;
use IO::Select;
use IO::Handle;
use YAML qw( LoadFile DumpFile );
use Term::ANSIColor qw( :constants );
use Getopt::Long;
use File::Which;
my $TEST = 'tmassive/huge.t';
my $BASELINE_FILE = 'baseline.yaml';
my $PERL = $^X;
my @SWITCHES = ('-I../lib');
my %SPECIAL = (
runtests => '../bin/runtests',
prove => scalar which('prove'),
);
my @STAGES = qw( source grammar parser runtests prove );
GetOptions( 'baseline' => \my $BASELINE ) or syntax();
my $baseline = -f $BASELINE_FILE ? LoadFile($BASELINE_FILE) : undef;
my $current = {};
my %last = ();
for my $stage (@STAGES) {
my $script = $SPECIAL{$stage} || "${stage}_only.pl";
my @cmd = ( 'time', '-p', $PERL, @SWITCHES, $script, $TEST );
my $cmd = join ' ', @cmd;
print ">>> $cmd <<<\n";
my ( $status, $stdout, $stderr ) = capture_command(@cmd);
my $result = {};
for (@$stderr) {
next unless /^(\w+)\s+(\d+(?:[.]\d+)?)$/;
print "$1 $2";
$result->{$1} = $2;
print ' ', $2 - $last{$1} if exists $last{$1};
print "\n";
}
%last = %{ $current->{$stage} = $result };
}
if ($baseline) {
for my $stage (@STAGES) {
print "$stage\n";
if ( my $result = $baseline->{$stage} ) {
for my $type (qw( real user sys )) {
print " $type ";
if ( ( my $base_time = $result->{$type} )
&& ( my $cur_time = $current->{$stage}->{$type} ) )
{
my $delta = $cur_time - $base_time;
my $color
= ( abs($delta) > ( $cur_time + $base_time ) / 50 )
? ( $delta > 0 )
? RED
: GREEN
: '';
print "$cur_time v $base_time, delta: $color$delta",
RESET;
}
else {
print "not available";
}
print "\n";
}
}
else {
print " No baseline\n";
}
}
}
if ($BASELINE) {
DumpFile( $BASELINE_FILE, $current );
}
sub capture_command {
my @cmd = @_;
my $cmd = join ' ', @cmd;
my $out = IO::Handle->new;
my $err = IO::Handle->new;
my $pid = eval { open3( undef, $out, $err, @cmd ) };
die "Could not execute ($cmd): $@" if $@;
my $sel = IO::Select->new( $out, $err );
my $flip = 0;
my @stdout = ();
my @stderr = ();
while ( my @ready = $sel->can_read ) {
for my $fh (@ready) {
if ( defined( my $line = <$fh> ) ) {
if ( $fh == $err ) {
push @stderr, $line;
}
else {
push @stdout, $line;
}
}
else {
$sel->remove($fh);
}
}
}
my $status = undef;
if ( $pid == waitpid( $pid, 0 ) ) {
$status = $?;
}
return ( $status, \@stdout, \@stderr );
}
sub syntax {
die "by_stage.pl [--baseline]\n";
}