开发者

accurately timing an execution of a system call using perl

Is there a better way programmatically using perl to track the timing of a unix process (start time, end time)??

For example, I have a perl script that launches a unix command using system(). I print the timestamp before the system() and after.

My log of the start time and end time are the same. I know for sure that when the process completes its a matter of minutes, so its not accurate.

Here's my code thus far:

my $dt  = strftime('%Y%m%d', localtime(time));
my $dt2 = strftime('%Y%m%d-%H:%M:%S', localtime(time)); 
my @tbls = qx(mysql -u foo -pmypw --database $dbsrc -h $node -ss -e "show tables");

open (CRLOG, ">dump-$dt.log") || die "cannot append";
foreach my $tbls (@tbls)
{
   chomp $tbls;
   print CRLOG "TIME START => $dt2\n";
   my $crfile = qq{mysql -u foo -pmypw --database $dbsrc -h $node.test.com -ss -e "SELECT 'a','b','c' UNION SELECT 1,2,3 FROM $tbls"| tr "\t" ",">$node-$tbls.$dt.csv};   system($crfile); 
开发者_StackOverflow中文版   print CRLOG "COMMAND => $crfile\n";
   print CRLOG "TIME END => $dt2\n";
}; 
close (CRLOG);


$dt2 is a constant, but it looks like you want it to recompute the timestamp every time it is used. For that purpose, you should use a function.

sub dt2 {
    return strftime('%Y%m%d-%H:%M:%S', localtime(time))
}

...

open (CRLOG, ">dump-$dt.log") || die "cannot append";
foreach my $tbls (@tbls)
{
   chomp $tbls;
   print CRLOG "TIME START => ", &dt2, "\n";
   my $crfile = qq{mysql -u foo -pmypw ...};
   system($crfile); 
   print CRLOG "COMMAND => $crfile\n";
   print CRLOG "TIME END => ", &dt2, "\n";
}; 
close (CRLOG);


Have you tried using the "time" command to time the command execution?

http://www.computerhope.com/unix/utime.htm

So in your perl script it would be something like this to capture the output:

$time_me = `/usr/bin/time $my_command`;

Then parse the $time_me variable to get the values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜