From 6da023af0c838818e56eb4a882bde3e812287f98 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Thu, 2 May 2024 15:56:05 +0300 Subject: [PATCH] cleanup --- benchmarks/create_circuits.nim | 17 ++--------------- benchmarks/run_benchmarks.nim | 18 +----------------- benchmarks/utils.nim | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 benchmarks/utils.nim diff --git a/benchmarks/create_circuits.nim b/benchmarks/create_circuits.nim index 016f4ed4..e98b5353 100644 --- a/benchmarks/create_circuits.nim +++ b/benchmarks/create_circuits.nim @@ -1,5 +1,7 @@ import std/[hashes, json, strutils, strformat, os, osproc] +import ./utils + type CircuitEnv* = object nimCircuitCli*: string @@ -32,21 +34,6 @@ func default*(tp: typedesc[CircuitEnv]): CircuitEnv = result.ptauUrl = "https://storage.googleapis.com/zkevm/ptau/" result.codexProjDir = codexDir -template withDir(dir: string, blk: untyped) = - ## set working dir for duration of blk - let prev = getCurrentDir() - try: - setCurrentDir(dir) - `blk` - finally: - setCurrentDir(prev) - -template runit(cmd: string) = - echo "RUNNING: ", cmd - let cmdRes = execShellCmd(cmd) - echo "STATUS: ", cmdRes - assert cmdRes == 0 - proc check*(env: var CircuitEnv) = ## check that the CWD of script is in the codex parent let codexProjDir = findCodexProjectDir() diff --git a/benchmarks/run_benchmarks.nim b/benchmarks/run_benchmarks.nim index 12934869..b636f5ac 100644 --- a/benchmarks/run_benchmarks.nim +++ b/benchmarks/run_benchmarks.nim @@ -9,6 +9,7 @@ import pkg/codex/utils/[json, poseidon2digest] import pkg/codex/slots/[builder, sampler/utils, backends/helpers] import pkg/constantine/math/[arithmetic, io/io_bigints, io/io_fields] +import ./utils import ./create_circuits type CircuitFiles* = object @@ -17,23 +18,6 @@ type CircuitFiles* = object zkey*: string inputs*: string -template benchmark(benchmarkName: string, blk: untyped) = - let nn = 5 - var vals = newSeqOfCap[float](nn) - for i in 1 .. nn: - block: - let t0 = epochTime() - `blk` - let elapsed = epochTime() - t0 - vals.add elapsed - - var elapsedStr = "" - for v in vals: - elapsedStr &= ", " & v.formatFloat(format = ffDecimal, precision = 3) - stdout.styledWriteLine( - fgGreen, "CPU Time [", benchmarkName, "] ", "avg(", $nn, "): ", elapsedStr, " s" - ) - proc runArkCircom(args: CircuitArgs, files: CircuitFiles) = echo "Loading sample proof..." var diff --git a/benchmarks/utils.nim b/benchmarks/utils.nim new file mode 100644 index 00000000..1b0dbeeb --- /dev/null +++ b/benchmarks/utils.nim @@ -0,0 +1,34 @@ + +template withDir*(dir: string, blk: untyped) = + ## set working dir for duration of blk + let prev = getCurrentDir() + try: + setCurrentDir(dir) + `blk` + finally: + setCurrentDir(prev) + +template runit*(cmd: string) = + ## run shell commands and verify it runs without an error code + echo "RUNNING: ", cmd + let cmdRes = execShellCmd(cmd) + echo "STATUS: ", cmdRes + assert cmdRes == 0 + +template benchmark*(benchmarkName: string, blk: untyped) = + ## simple benchmarking of a block of code + let nn = 5 + var vals = newSeqOfCap[float](nn) + for i in 1 .. nn: + block: + let t0 = epochTime() + `blk` + let elapsed = epochTime() - t0 + vals.add elapsed + + var elapsedStr = "" + for v in vals: + elapsedStr &= ", " & v.formatFloat(format = ffDecimal, precision = 3) + stdout.styledWriteLine( + fgGreen, "CPU Time [", benchmarkName, "] ", "avg(", $nn, "): ", elapsedStr, " s" + )