File: benchmark.nim

package info (click to toggle)
nim 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 462,356 kB
  • sloc: sh: 11,089; ansic: 4,699; makefile: 706; python: 309; sql: 297; asm: 141; xml: 13
file content (47 lines) | stat: -rw-r--r-- 1,286 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
#
#            Nim Benchmark tool
#        (c) Copyright 2012 Dominik Picheta
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This program runs benchmarks
import osproc, os, times, json

type
  TBenchResult = tuple[file: string, success: bool, time: float]

proc compileBench(file: string) =
  ## Compiles ``file``.
  doAssert(execCmdEx("nim c -d:release " & file).exitCode == QuitSuccess)

proc runBench(file: string): TBenchResult =
  ## Runs ``file`` and returns info on how long it took to run.
  var start = epochTime()
  if execCmdEx(file.addFileExt(ExeExt)).exitCode == QuitSuccess:
    var t = epochTime() - start
    result = (file, true, t)
  else: result = (file, false, -1.0)

proc genOutput(benches: seq[TBenchResult]): PJsonNode =
  result = newJObject()
  for i in benches:
    if i.success:
      result[i.file.extractFilename] = newJFloat(i.time)
    else:
      result[i.file.extractFilename] = newJNull()

proc doBench(): seq[TBenchResult] =
  result = @[]
  for i in walkFiles("tests/benchmarks/*.nim"):
    echo(i.extractFilename)
    compileBench(i)
    result.add(runBench(i))

when isMainModule:
  var b = doBench()
  var output = genOutput(b)
  writeFile("benchmarkResults.json", pretty(output))