fix API, add test for nested children

This commit is contained in:
gmega 2024-03-07 14:34:13 -03:00
parent 44af970bc9
commit f2c4c0d9ac
No known key found for this signature in database
GPG Key ID: FFD8DAF00660270F
2 changed files with 45 additions and 11 deletions

View File

@ -11,16 +11,18 @@ proc getMetrics*(): MetricsTotals =
## current thread.
result = profilerInstance.metrics
proc enableProfiling*(callback: EventCallback = nil) =
template enableProfiling*(callback: EventCallback) =
## Enables profiling for the the event loop running in the current thread.
## The client may optionally supply a callback to be notified of `Future`
## events.
attachMonitoring(
if (isNil(callback)):
proc(e: Event) {.nimcall.} =
profilerInstance.processEvent(e)
else:
proc(e: Event) {.nimcall.} =
proc(e: Event) {.nimcall, gcsafe, raises: [].} =
profilerInstance.processEvent(e)
callback(e)
)
template enableProfiling*() =
attachMonitoring(
proc(e: Event) {.nimcall, gcsafe, raises: [].} =
profilerInstance.processEvent(e)
)

View File

@ -282,23 +282,55 @@ suite "profiler metrics":
await barrier
advanceTime(10.milliseconds)
proc parent(): Future[Future[void]] {.async.} =
proc parent(): Future[seq[Future[void]]] {.async.} =
advanceTime(10.milliseconds)
return child()
return @[child(), child()]
let childFut = waitFor parent()
barrier.complete()
waitFor childFut
waitFor allFutures(childFut)
var metrics = recordedMetrics()
let parentMetrics = metrics.forProc("parent")
let childMetrics = metrics.forProc("child")
check parentMetrics.execTime == 10.milliseconds
check parentMetrics.childrenExecTime == 10.milliseconds
check parentMetrics.childrenExecTime == 20.milliseconds
check parentMetrics.wallClockTime == 10.milliseconds
echo "EXE TIME ", $childMetrics.execTime
check childMetrics.execTime == 20.milliseconds
check childMetrics.wallClockTime == 30.milliseconds
check childMetrics.childrenExecTime == ZeroDuration
test "should aggregate child execution times across nested children":
proc child() {.async.} =
advanceTime(10.milliseconds)
proc parent() {.async.} =
advanceTime(10.milliseconds)
await child()
advanceTime(10.milliseconds)
proc grandparent() {.async.} =
advanceTime(10.milliseconds)
await parent()
advanceTime(10.milliseconds)
waitFor grandparent()
var metrics = recordedMetrics()
let grandparentMetrics = metrics.forProc("grandparent")
let parentMetrics = metrics.forProc("parent")
let childMetrics = metrics.forProc("child")
check grandparentMetrics.execTime == 20.milliseconds
check grandparentMetrics.childrenExecTime == 30.milliseconds
check grandparentMetrics.wallClockTime == 50.milliseconds
check parentMetrics.execTime == 20.milliseconds
check parentMetrics.childrenExecTime == 10.milliseconds
check parentMetrics.wallClockTime == 30.milliseconds
check childMetrics.execTime == 10.milliseconds
check childMetrics.wallClockTime == 10.milliseconds
check childMetrics.childrenExecTime == ZeroDuration