From 9ce5ed53d28c7602ec71b662a47f7bc1b1fbba99 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Fri, 22 Apr 2022 16:18:00 +0200 Subject: [PATCH] inspectPerformance (tested) --- .../__tests__/Reducer/Reducer_debugging_test.res | 5 +++++ .../Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_debugging_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_debugging_test.res index 588dd842..0ce1a90d 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_debugging_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_debugging_test.res @@ -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)") }) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res index fc14b5d6..0c1f6c04 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res @@ -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) } }