Skip to content

Commit

Permalink
Remove Telemetry.Period, it was too magical to some
Browse files Browse the repository at this point in the history
- fix Telemetry.wallclock: it shouldn't return since epoch
- improved snap() subroutine, now also allows pushing to a given array
- added periods() subroutine, for generating a Seq of Telemetry::Period objects
  • Loading branch information
lizmat committed Oct 29, 2017
1 parent 59a59be commit 5244048
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions lib/Telemetry.pm6
Expand Up @@ -11,16 +11,13 @@ class Telemetry {

my num $start = Rakudo::Internals.INITTIME;

multi method new(Telemetry:) { nqp::create(self).SET-SELF }

method SET-SELF() {
submethod BUILD() {
my \rusage = nqp::getrusage;
$!cpu-user = nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC);
$!cpu-sys = nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC);
$!wallclock = nqp::fromnum_I(1000000*nqp::sub_n(nqp::time_n,$start),Int);
self
}

proto method cpu() { * }
Expand Down Expand Up @@ -53,33 +50,10 @@ class Telemetry {

proto method wallclock() { * }
multi method wallclock(Telemetry:U:) is raw {
nqp::fromnum_I(1000000 * nqp::time_n,Int)
nqp::fromnum_I(1000000 * nqp::sub_n(nqp::time_n,$start),Int)
}
multi method wallclock(Telemetry:D:) is raw { $!wallclock }

proto method Period() { * }
multi method Period(Telemetry:U \SELF:) is raw {
if nqp::iscont(SELF) {
SELF = SELF.new;
nqp::create(Telemetry::Period)
}
else {
die "Must use container of type Telemetry"
}
}
multi method Period(Telemetry:D:) is raw {
my int $cpu-user = $!cpu-user;
my int $cpu-sys = $!cpu-sys;
my int $wallclock = $!wallclock;
self.SET-SELF;

Telemetry::Period.new(
nqp::sub_i($!cpu-user,$cpu-user),
nqp::sub_i($!cpu-sys,$cpu-sys),
nqp::sub_i($!wallclock,$wallclock)
)
}

multi method Str(Telemetry:D:) {
$!wallclock ?? "$.cpu / $!wallclock" !! "cpu / wallclock"
}
Expand Down Expand Up @@ -141,6 +115,18 @@ multi sub infix:<->(Telemetry:D $a, Telemetry:D $b) is export {
)
}

sub snap() is export { Telemetry.new }
my @snaps;
proto sub snap(|) is export { * }
multi sub snap(--> Nil) { @snaps.push(Telemetry.new) }
multi sub snap(@s --> Nil) { @s.push(Telemetry.new) }

proto sub periods(|) is export { * }
multi sub periods() {
(1..^@snaps).map: {
LAST @snaps = ();
@snaps[$_] - @snaps[$_ - 1]
}
}
multi sub periods(@s) { (1..^@s).map: { @s[$_] - @s[$_ - 1] } }

# vim: ft=perl6 expandtab sw=4

0 comments on commit 5244048

Please sign in to comment.