inspectPerformance (tested)

This commit is contained in:
Umur Ozkul 2022-04-22 16:18:00 +02:00
parent 417f0060b6
commit 9ce5ed53d2
2 changed files with 15 additions and 0 deletions

View File

@ -4,9 +4,14 @@ open Reducer_TestHelpers
/*
You can wrap around any expression with inspect(expr) to log the value of that expression.
This is useful for debugging. inspect(expr) returns the value of expr, but also prints it out.
There is a second version of inspect that takes a label, which will print out the label and the value.
inpsectPerformace(expr, label) will print out the value of expr, the label, and the time it took to evaluate expr.
*/
describe("Debugging", () => {
testEvalToBe("inspect(1)", "Ok(1)")
testEvalToBe("inspect(1, \"one\")", "Ok(1)")
testEvalToBe("inspect(1, \"one\")", "Ok(1)")
testEvalToBe("inspectPerformance(1, \"one\")", "Ok(1)")
})

View File

@ -53,6 +53,15 @@ let callInternal = (call: functionCall): result<'b, errorValue> => {
value->Ok
}
let inspectPerformance = (value: expressionValue, label: string) => {
let _ = %raw("{performance} = require('perf_hooks')")
let start = %raw(`performance.now()`)
let finish = %raw(`performance.now()`)
let performance = finish - start
Js.log(`${label}: ${value->toString} performance: ${Js.String.make(performance)}ms`)
value->Ok
}
switch call {
| ("$atIndex", [EvArray(aValueArray), EvArray([EvNumber(fIndex)])]) =>
arrayAtIndex(aValueArray, fIndex)
@ -62,6 +71,7 @@ let callInternal = (call: functionCall): result<'b, errorValue> => {
| ("$constructRecord", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
| ("inspect", [value, EvString(label)]) => inspectLabel(value, label)
| ("inspect", [value]) => inspect(value)
| ("inspectPerformance", [value, EvString(label)]) => inspectPerformance(value, label)
| call => callMathJs(call)
}
}