Debian Codegraph @

t/0LIB/MyTmpTimer.pm

raw
1 package MyTmpTimer;
2 use strict;
3 use warnings;
4 use MyTest;
5 use File::Temp;
6 use Time::HiRes qw(gettimeofday tv_interval);
7 require Exporter;
8 use vars qw(@EXPORT);
9
10 @EXPORT = qw(tmptimer int_commify);
11
12 *import = \&Exporter::import;
13
14 sub tmptimer {
15 my ($n, $callback) = @_;
16 my $self = bless {files=>[]}, __PACKAGE__;
17
18 my $dir = $self->{dir} = File::Temp->newdir('UUID-test-XXXXXXXX', TMPDIR => 1, CLEANUP => 1);
19
20 my $fh = File::Temp->new( DIR => $dir );
21 push @{$self->{files}}, $fh->filename;
22
23 my ($t0, $t1, $t2);
24 {
25 our $NOW = 0;
26 local $SIG{'INT'} = sub { $NOW = 1; die 'SIGINT' };
27 local $SIG{'HUP'} = sub { $NOW = 1; die 'SIGHUP' };
28 local $SIG{'PIPE'} = sub { $NOW = 1; die 'SIGPIPE' };
29 local $SIG{'TERM'} = sub { $NOW = 1; die 'SIGTERM' };
30
31 $t0 = [gettimeofday];
32 $t1 = [gettimeofday];
33
34 eval { $callback->($n, $fh) };
35
36 $t2 = [gettimeofday];
37
38 if ($NOW) {
39 undef $self;
40 die 'Caught '.$@;
41 }
42 }
43
44 my $i0 = tv_interval $t0, $t1;
45 my $i1 = tv_interval $t1, $t2;
46 my $e = $i1 - $i0;
47
48 note int_commify($n, 1). " tests";
49 note "$e seconds";
50 note int_commify($n, $e), " UUIDs / second";
51
52 $fh->seek(SEEK_SET, 0);
53 return $fh;
54 }
55
56 sub int_commify {
57 my ($n, $e) = @_;
58 return 'Inf' if $e == 0;
59 my $input = int($n / $e);
60 $input = reverse $input;
61 $input =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
62 return scalar reverse $input;
63 }
64
65 1;