File: 61-cputime.t

package info (click to toggle)
libdevel-nytprof-perl 6.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,552 kB
  • sloc: perl: 5,616; javascript: 4,033; ansic: 107; makefile: 23
file content (103 lines) | stat: -rw-r--r-- 2,683 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Tests CORE::GLOBAL::foo plus assorted data model methods

use strict;
use Test::More;

use lib qw(t/lib);
use NYTProfTest;
use Data::Dumper;

use Devel::NYTProf::Run qw(profile_this);

my $src_code = join("", <DATA>);

run_test_group( {
    extra_options => {
        # set options for this test:
        usecputime => 1,
        # restrict irrelevant options:
        compress => 1, slowops => 0, savesrc => 0, leave => 0, stmts => 0,
    },
    extra_test_count => 6,
    extra_test_code  => sub {
        my ($profile, $env) = @_;
        my $trace = ($^O eq 'freebsd'); # XXX temp

        $profile = profile_this(
            src_code => $src_code,
            out_file => $env->{file},
            #htmlopen => 1,
            verbose => $trace,
            skip_sitecustomize => 1,
        );
        isa_ok $profile, 'Devel::NYTProf::Data';
        warn "ticks_per_sec ".$profile->attributes->{ticks_per_sec}."\n"
            if $trace;

        my $subs = $profile->subname_subinfo_map;
        my $sub = $subs->{'main::foo'};
        ok $sub;
        is $sub->calls, 1, 'main::foo should be called 1 time';
        cmp_ok $sub->incl_time, '>=', 0.4 * 0.99, 'cputime of foo() should be at least 0.4';
        cmp_ok $sub->incl_time, '<', 1.1, 'cputime of foo() should be not much more than 0.4';
        is $sub->incl_time, $sub->excl_time, 'incl_time and excl_time should be the same';
    },
});

__DATA__
#!perl

BEGIN { eval { require Time::HiRes } and Time::HiRes->import('time') }

alarm(20); # watchdog timer

my $trace = 0;
my $cpu1;
my $cpu2;

sub foo {
    my $cpuspend = shift;

    # sleep to separate cputime from realtime
    # (not very effective in cpu-starved VMs)
    sleep 1;

    my $loops = 0;
    my $prev;
    while (++$loops) {
        my @times = times;
        my $crnt = $times[0] + $times[1] - $cpu1;
        warn sprintf "tick %.4f\t%f\n", $crnt, time()
            if $trace >= 2 && $prev && $crnt != $prev;
        $prev = $crnt;

        last if $crnt >= $cpuspend;
    }
    warn "cputime loop count $loops\n" if $trace >= 2;
} 

# record start time
my $start = time() + 1;

# sync up...

# spin till wall clock ticks
1 while time() <= $start;

# spin till cpu clock ticks (typically 0.1 sec max)
my @times = times;
$cpu1 = $times[0] + $times[1];
while (1) {
    @times = times;
    $cpu2 = $times[0] + $times[1];
    last if $cpu2 != $cpu1;
}

warn sprintf "step %f\t%f\n", $cpu2-$cpu1, time() if $trace;
$cpu1 = $cpu2; # set cpu1 to new current cpu time

# consume this much cpu time inside foo()
foo(0.4);

# report realtime to help identify is cputime is really measuring realtime
print "realtime used ".(time()-$start)."\n" if $trace;