レスポンスタイムをログの処理開始時と終了時のタイムスタンプを比較して算出するようなときに使うコードサンプル。
- msec.pl
#!/bin/perl use strict; use warnings; use Time::Local; my $begin = '2009/07/30 11:15:36.180'; my $end = '2009/07/30 11:15:37.191'; my ($begin_year, $begin_mon, $begin_mday, $begin_hour, $begin_min, $begin_sec, $begin_msec) = $begin =~ /^([0-9]{4})\/([0-9]{2})\/([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})\.([0-9]{3})$/; my ($end_year, $end_mon, $end_mday, $end_hour, $end_min, $end_sec, $end_msec) = $end =~ /^([0-9]{4})\/([0-9]{2})\/([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})\.([0-9]{3})$/; my $begin_time = timelocal($begin_sec, $begin_min, $begin_hour, $begin_mday, $begin_mon-1, $begin_year-1900); my $end_time = timelocal($end_sec, $end_min, $end_hour, $end_mday, $end_mon-1, $end_year-1900); my $diff = "$end_time.$end_msec" - "$begin_time.$begin_msec"; print "$end - $begin = $diff\n";
- 実行結果
$ perl msec.pl 2009/07/30 11:15:37.191 - 2009/07/30 11:15:36.180 = 1.01099991798401
レスポンスタイムは 1.01099991798401 秒。