forked from shenwei356/seqkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.pl
executable file
·148 lines (120 loc) · 3.91 KB
/
run.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env perl
use strict;
use Getopt::Long;
use File::Basename;
$0 = basename $0;
my $usage = <<USAGE;
Usage:
1. Run all tests:
perl $0 run_benchmark*.sh --outfile benchmark.5test.tsv
2. Run one test:
perl $0 run_benchmark_04_remove_duplicated_seqs_by_name.sh -o benchmark.rmdup.tsv
3. Custom repeate times:
perl $0 -n 3 run_benchmark_04_remove_duplicated_seqs_by_name.sh -o benchmark.rmdup.tsv
USAGE
my $N = 3; # run $N times
my $resultfile = "$0.benchmark.tsv";
my $help = 0;
GetOptions( "n=i" => \$N, 'outfile|o=s' => \$resultfile, "help|h" => \$help, )
or die $usage;
die $usage if scalar @ARGV == 0 or $help;
my @tests = @ARGV;
open my $fh_result, ">", $resultfile or die "failed to write file: $resultfile\n";
my $title_line = "test\tdataset\tapp\ttime_mean\ttime_stdev\tmem_mean\tmem_stdev\n";
print $fh_result $title_line;
for my $test (@tests) {
print STDERR "Test: $test\n";
my $stat = {}; # dataset->app->round
my $stat_mem = {};
my $t = ""; # test name
# run $N times
for my $n ( 1 .. $N ) {
print STDERR "Round: $n\n";
my $outfile = "$test.round$n.out";
unlink $outfile if -e $outfile;
# run
my $cmd = "sh $test 2>&1 | tee $outfile";
my $fail = run($cmd);
die "failed to run:$cmd\n" if $fail;
# stat
my ( $app, $dataset, $time, $mem );
open my $fh, "<", $outfile or die "failed to read file: $outfile\n";
for my $line (<$fh>) {
if ( $t eq "" and $line =~ /Test: (.+)/ ) {
$t = $1;
}
if ( $line =~ /== (.+)/ ) {
$app = $1;
}
if ( $line =~ /data: (.+)/ ) {
$dataset = $1;
}
if ( $line =~ /time: (.+)/ ) {
$time = $1;
if ( not exists $$stat{$dataset}{$app} ) {
$$stat{$dataset}{$app} = [];
}
push @{ $$stat{$dataset}{$app} }, $time;
}
if ( $line =~ /rss: (\d+)/ ) {
$mem = $1;
if ( not exists $$stat_mem{$dataset}{$app} ) {
$$stat_mem{$dataset}{$app} = [];
}
push @{ $$stat_mem{$dataset}{$app} }, $mem;
}
}
close($fh);
}
# benchmark result
my $statfile = "$test.benchmark.tsv";
open my $fh, ">", $statfile or die "failed to write file: $statfile\n";
print "\n=========[ benchmark result ]========\n";
print $title_line;
print $fh $title_line;
for my $dataset ( sort keys %$stat ) {
my $st = $$stat{$dataset};
for my $app ( sort keys %$st ) {
my $times = $$st{$app};
my $mems = $$stat_mem{$dataset}{$app};
my ( $time_mean, $time_stdev ) = mean_and_stdev($times);
my ( $mem_mean, $mem_stdev ) = mean_and_stdev($mems);
my $data_line = sprintf "%s\t%s\t%s\t%.2f\t%.2f\t%d\t%d\n",
$t, $dataset, $app, $time_mean, $time_stdev,$mem_mean, $mem_stdev;
printf $data_line;
printf $fh $data_line;
printf $fh_result $data_line;
}
}
close($fh);
}
close($fh_result);
sub run {
my ($cmd) = @_;
system($cmd);
if ( $? == -1 ) {
die "[ERROR] fail to run: $cmd. Command ("
. ( split /\s+/, $cmd )[0]
. ") not found\n";
}
elsif ( $? & 127 ) {
printf STDERR "[ERROR] command died with signal %d, %s coredump\n",
( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
# 0, ok
}
return $?;
}
sub mean_and_stdev($) {
my ($list) = @_;
return ( 0, 0 ) if @$list == 0;
my $sum = 0;
$sum += $_ for @$list;
my $sum_square = 0;
$sum_square += $_ * $_ for @$list;
my $mean = $sum / @$list;
my $variance = $sum_square / @$list - $mean * $mean;
my $std = sqrt $variance;
return ( $mean, $std );
}