[alt.sources] dotime.pl: aid in command timing

jv@mh.nl (Johan Vromans) (01/04/90)

The following perl script aids in timing commands.
The command is run a number of times, and the avarage statistics are
shown together with the statistics of each pass.

Example:
$ dotime 5 ls -l
Running "ls -l" 1 2 3 4 5  done

      Avg  Pass 1     2     3     4     5
     ----- ------- ----- ----- ----- -----
real   1.8     3.7   1.4   1.4   1.2   1.2
user   0.6     0.7   0.6   0.6   0.6   0.5
sys    0.6     0.9   0.6   0.5   0.5   0.6

Have fun!

Johan

------ begin of dotime -- ascii -- complete ------
#!/usr/bin/perl

# @(#)@ dotime	1.1 - dotime
# This program requires perl version 3.0, patchlevel 4 or later.
# Copyright 1990 Johan Vromans, no rights reserved.

# Usage: dotime repeat command

die "usage: $0 <repeat> <command>\nstopped" unless $#ARGV >= 1;

$repeat = shift (@ARGV);
die "invalid repeat: $repeat, stopped" 
  unless (($repeat > 0) && ($repeat < 999));

$command = "@ARGV";

$tt_real = $tt_user = $tt_sys = 0;
@t_real = @t_user = @t_sys = ();
$| = 1;
for ($pass = 1; $pass <= $repeat; $pass++) {
  print STDOUT ($pass == 1) ? "Running \"$command\" 1 " : "$pass ";
  open (TIMES, "/bin/time $command 2>&1 |") ||
    die ("cannot open /bin/time $command 2>&1 |");
  while (<TIMES>) {
    if (/^real\s+(\d+\.\d+)\n/) {
      push (@t_real, 0+$1);
      $tt_real += $1;
    }
    elsif (/^user\s+(\d+\.\d+)/) {
      push (@t_user, 0+$1);
      $tt_user += $1;
    }
    elsif (/^sys\s+(\d+\.\d+)/) {
      push (@t_sys , 0+$1);
      $tt_sys += $1;
    }
  }
  close (TIMES);
}
$| = 0;

print " done\n";

print "\n      Avg  Pass 1";
for ($pass = 2; $pass <= $repeat; $pass++) {
  printf "%6d", $pass;
}
print "\n";
print "     ----- -------";
for ($pass = 2; $pass <= $repeat; $pass++) {
  print " -----";
}
print "\n";
for $arg ("real","user","sys ") {
  eval ("printf \"$arg %5.1f  \", \$tt_$arg/$repeat;".
	"for (\$pass = 1; \$pass <= $repeat; \$pass++) {".
	"printf \"%6.1f\", \$t_$arg[\$pass-1];".
        "}".
	"print \"\n\";");
}
------ end of dotime -- ascii -- complete ------
--
Johan Vromans				       jv@mh.nl via internet backbones
Multihouse Automatisering bv		       uucp: ..!{uunet,hp4nl}!mh.nl!jv
Doesburgweg 7, 2803 PL Gouda, The Netherlands  phone/fax: +31 1820 62944/62500
------------------------ "Arms are made for hugging" -------------------------