Skip to content

Instantly share code, notes, and snippets.

@tauty
Last active August 29, 2015 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tauty/c1d2349cc33a3a3448c8 to your computer and use it in GitHub Desktop.
Save tauty/c1d2349cc33a3a3448c8 to your computer and use it in GitHub Desktop.
#
# Copyright 2015 tetsuo.ohta[at]gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package performance_meter;
use Exporter;
@performance_meter::ISA = qw(Exporter);
@performance_meter::EXPORT = qw(PM_begin PM_end PM_show);
use strict;
use warnings;
use Time::HiRes qw( gettimeofday tv_interval );
my %startTime_micro;
my %summaryTime_micro;
my %count;
sub PM_begin {
my ($key) = @_;
my $t0 = [gettimeofday];
$startTime_micro{$key} = $t0;
}
sub PM_end {
# my ($key) = @_;
my ($key, @result) = @_;
my $elapsed = tv_interval($startTime_micro{$key});
if($summaryTime_micro{$key}) {
$summaryTime_micro{$key} = $summaryTime_micro{$key} + $elapsed;
} else {
$summaryTime_micro{$key} = $elapsed;
}
if($count{$key}) {
$count{$key} = $count{$key} + 1;
} else {
$count{$key} = 1;
}
return @result if(@result);
}
sub PM_show {
print STDOUT "===== Performance Meter Result =====\n";
foreach my $key(sort keys(%summaryTime_micro)){
my $res_mili = $summaryTime_micro{$key} * 1000;
my $ave_mili = $res_mili / $count{$key};
print STDOUT "\t$key - summary: $res_mili(msec), $count{$key} times, ave.: $ave_mili(msec)\n";
}
}
1;
__END__
--------------------- sample code ---------------------
use Time::HiRes qw( usleep gettimeofday tv_interval );
use File::Basename qw(basename);
use FindBin qw($Bin $Script);
use lib ("$Bin", "$Bin/lib/", "$Bin/conf/");
use performance_meter;
PM_begin('foo');
PM_begin('bar');
usleep(200 * 1000);
PM_end('foo');
PM_end('bar');
PM_begin('foo');
usleep(200 * 1000);
PM_end('foo');
PM_show();
--------------------- the output of sample code ---------------------
===== Performance Meter Result =====
bar - summary: 199.154(msec), 1 times, ave.: 199.154(msec)
foo - summary: 399.1(msec), 2 times, ave.: 199.55(msec)
@tauty
Copy link
Author

tauty commented Apr 24, 2015

Advice for PM_end function from itoshige. Implement it later.
my ($key, @Result) = @_;
...
return @Result if(@Result);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment