File: run.pl

package info (click to toggle)
golang-github-shenwei356-bio 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 852 kB
  • sloc: perl: 114; sh: 58; makefile: 21
file content (152 lines) | stat: -rwxr-xr-x 4,070 bytes parent folder | download | duplicates (3)
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
149
150
151
152
#!/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\tmem\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);

            for my $i (0..@$times-1) {
                my ($time, $mem) = ($$times[$i], $$mems[$i]);

                my $data_line = sprintf "%s\t%s\t%s\t%.2f\t%d\n",
                            $t, $dataset, $app, $time, $mem;

                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 );
}