summaryrefslogtreecommitdiff
path: root/perl-profiler.pl
blob: 4fcc169396d38066ad25125b9dc6cd4ae7c77b41 (plain)
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
#!/usr/bin/perl -w

# Based on /usr/share/doc/libdevel-nytprof-perl/examples/demo/demo-run.pl
# from the libdevel-nytprof-perl package.
# License: Artistic or GPL-1+

use strict;
use IO::Handle;

my $NYTPROF = ($ENV{NYTPROF}) ? "$ENV{NYTPROF}:" : "";

my %runs = (
    start_begin => {
        skip => 0,
        NYTPROF => 'start=begin:optimize=0',
    },
    start_check => {
        skip => 1,
        NYTPROF => 'start=init:optimize=0',
    },
    start_end => {
        skip => 1,
        NYTPROF => 'start=end:optimize=0',
    },
);

my $bin = shift @ARGV;
my $name = $bin;
$name =~ s,^.*/,,;

for my $run (keys %runs) {

    next if $runs{$run}{skip};
    $ENV{NYTPROF}      = $NYTPROF . $runs{$run}{NYTPROF} || '';
    $ENV{NYTPROF_HTML} = $runs{$run}{NYTPROF_HTML} || '';

    my $cmd = "perl -d:NYTProf $bin @ARGV";
    open my $fh, "| $cmd"
        or die "Error starting $cmd\n";
    $fh->autoflush;
    close $fh
        or die "Error closing pipe to $cmd: $!\n";

    my $outdir = "${name}-profiler/$run";
    system("rm -rf $outdir") == 0 or exit 0;
    system("mkdir -p $outdir") == 0 or exit 0;
    system("nytprofhtml --open --out=$outdir") == 0
        or exit 0;

    #system "ls -lrt $outdir/.";

    sleep 1;
}