cup/test.nim

132 lines
3.1 KiB
Nim
Raw Normal View History

2021-07-20 04:26:26 +00:00
import math, random, strformat, strutils, times, std/monotimes
2021-07-20 23:16:01 +00:00
import cligen
2021-07-20 04:26:26 +00:00
import fixedseq, game, simulation
2021-07-13 22:54:54 +00:00
2021-07-19 18:08:13 +00:00
type
TestResults = object
2021-07-20 23:16:01 +00:00
scores: ScoreSet
2021-07-19 18:08:13 +00:00
time: Duration
2021-07-20 23:16:01 +00:00
proc ops(tr: TestResults): int =
result = sum(tr.scores)
2021-07-20 04:26:26 +00:00
proc formatNum(n: SomeNumber, decimals = 0): string =
when n is SomeFloat:
let s = $n.round(decimals)
else:
let s = $n
var parts = s.split('.')
result = parts[0].insertSep(',')
if decimals > 0:
result = result & '.' & parts[1]
proc summarize(tr: TestResults, opname = "operations") =
2021-07-19 18:08:13 +00:00
let secs = tr.time.inMilliseconds.float / 1000
stdout.write("Test completed:\n")
2021-07-20 23:16:01 +00:00
stdout.write(&" {tr.ops.formatNum} {opname} in {secs.formatNum(2)} seconds\n")
stdout.write(&" {(tr.ops.float / secs).formatNum} {opname} per second\n")
2021-07-19 18:08:13 +00:00
stdout.flushFile()
2021-07-20 23:16:01 +00:00
# proc getRand(): Rand =
# randomize()
# result = initRand(rand(int64))
proc newRandomGame(): Board =
randomize()
2021-11-01 22:44:21 +00:00
result.init
var state = newGameState()
2021-07-20 23:16:01 +00:00
for i in 0 .. 4:
2021-11-01 22:44:21 +00:00
let pos = rand(1..3)
state.camels.add((c: Color(i), p: pos))
state.camels.shuffle()
2021-07-20 23:16:01 +00:00
2021-11-01 22:44:21 +00:00
result.setState(state)
2021-07-20 23:16:01 +00:00
2021-07-19 18:08:13 +00:00
template executionTime(body: untyped): Duration =
let start = getMonoTime()
body
getMonoTime() - start
2021-07-20 23:16:01 +00:00
# template runTest(loops: Natural, opname = "operations", body: ScoreSet): TestResults =
# var res: TestResults
# for i in 1 .. loops:
# let start = getMonoTime()
# let s = body
# res.time += (getMonoTime() - start)
# res.scores.update(s)
# res.summarize(opname)
2021-07-19 18:08:13 +00:00
2021-07-20 23:16:01 +00:00
# proc games(runs, samples: Natural, parallel = true) =
# let b = newRandomGame()
# runTest(runs, "games"):
# b.randomGames(samples, parallel = parallel)
2021-07-13 22:54:54 +00:00
2021-07-20 23:16:01 +00:00
# proc legs(runs: Natural) =
# let b = newRandomGame()
# runTest(runs, "legs"):
# b.getLegScores
2021-07-20 23:16:01 +00:00
proc games(runs, samples: Natural, parallel = true) =
2021-07-20 04:26:26 +00:00
var res: TestResults
2021-07-20 23:16:01 +00:00
for i in 1 .. runs:
let b = newRandomGame()
2021-07-19 18:08:13 +00:00
let dur = executionTime:
2021-07-20 23:16:01 +00:00
let s = b.randomGames(samples, parallel = parallel)
res.scores.update(s)
2021-07-20 04:26:26 +00:00
res.time += dur
res.summarize("games")
2021-07-20 23:16:01 +00:00
proc legs(runs: Natural) =
2021-07-20 04:26:26 +00:00
var res: TestResults
2021-07-20 23:16:01 +00:00
for i in 1 .. runs:
let b = newRandomGame()
2021-07-20 04:26:26 +00:00
let dur = executionTime:
let s = b.getLegScores
2021-07-20 23:16:01 +00:00
res.scores.update(s)
2021-07-20 04:26:26 +00:00
res.time += dur
res.summarize("legs")
2021-07-20 23:16:01 +00:00
proc spread(runs, samples: Natural) =
let b = newRandomGame()
let spread = randomSpread(b, runs, samples)
2021-07-13 23:16:47 +00:00
stdout.writeLine("Variance:")
for c in Color:
let variance = 100 * (spread.hi[c] - spread.lo[c])
stdout.writeLine(fmt"{c}: {round(variance, 2):.2f}%")
let diff = 100 * (max(spread.hi) - min(spread.lo))
stdout.writeLine(fmt"Win percentage differential: {round(diff, 2):.2f}%")
stdout.flushFile()
2021-07-20 23:16:01 +00:00
const help_runs = "Number of times to run the test"
const help_samples = "Number of iterations per run"
const help_parallel = "Run test in parallel or single-threaded (default parallel)"
proc bench() =
dispatchMulti(
[games, help = {"runs": help_runs, "samples": help_samples, "parallel": help_parallel}],
[legs, help = {"runs": help_runs}],
[spread, help = {"runs": help_runs, "samples": help_samples}]
)
2021-07-13 22:54:54 +00:00
when isMainModule:
2021-07-20 23:16:01 +00:00
bench()