From 02833b81e5fd86ae45c6a98d183317801103183b Mon Sep 17 00:00:00 2001 From: munna0908 Date: Wed, 5 Mar 2025 18:17:41 +0530 Subject: [PATCH] write benchmark results to csv --- benchmarks/utils.nim | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/benchmarks/utils.nim b/benchmarks/utils.nim index af5cdc25..78c82c8b 100644 --- a/benchmarks/utils.nim +++ b/benchmarks/utils.nim @@ -1,4 +1,5 @@ import std/tables +import std/times template withDir*(dir: string, blk: untyped) = ## set working dir for duration of blk @@ -41,18 +42,49 @@ template benchmark*(name: untyped, count: int, blk: untyped) = ) benchRuns[benchmarkName] = (runs.avg(), count) -template printBenchMarkSummaries*(printRegular=true, printTsv=true) = +const BenchmarkFile = "benchmarks.csv" + +template printBenchMarkSummaries*( + printRegular = true, printTsv = true, exportExcel = true +) = if printRegular: echo "" for k, v in benchRuns: echo "Benchmark average run ", v.avgTimeSec, " for ", v.count, " runs ", "for ", k - + if printTsv: echo "" echo "name", "\t", "avgTimeSec", "\t", "count" for k, v in benchRuns: echo k, "\t", v.avgTimeSec, "\t", v.count + if exportExcel: + let timestamp = now().format("yyyy-MM-dd HH:mm:ss") + var f: File + var isNewFile = not fileExists("repo-benchmars.csv") + + if f.open("repo-benchmars.csv", fmAppend): + try: + # Write header if new file + if isNewFile: + f.writeLine("Timestamp,Benchmark,Average Time (s),Run Count") + + # Write benchmark data + for name, data in benchRuns: + f.writeLine( + [ + timestamp, + name, + data.avgTimeSec.formatFloat(format = ffDecimal, precision = 3), + $data.count, + ].join(",") + ) + + echo "Benchmark results appended to: ", BenchmarkFile + finally: + f.close() + else: + echo "Error: Could not open ", BenchmarkFile, " for writing" import std/math