From 52440486188faf0ad1310f87ff5c2acdd4169498 Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Sun, 29 Oct 2017 14:08:26 +0100 Subject: [PATCH] Remove Telemetry.Period, it was too magical to some - 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 --- lib/Telemetry.pm6 | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/lib/Telemetry.pm6 b/lib/Telemetry.pm6 index 258472fb59d..a6b24e76018 100644 --- a/lib/Telemetry.pm6 +++ b/lib/Telemetry.pm6 @@ -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() { * } @@ -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" } @@ -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