Skip to content

Commit

Permalink
Add a unit test mode that runs benchmarks to check that they do not b…
Browse files Browse the repository at this point in the history
…it-rot.
  • Loading branch information
axch committed Sep 1, 2022
1 parent c86b920 commit 610fe4a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions benchmarks/fused_sum.dx
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ n = if dex_test_mode() then 1000 else 1000000
sum $ for i:(Fin n).
x = n_to_i64 (ordinal i)
x * x
> 332833500
>
> fused-sum
> Compile time: 22.192 ms
> Run time: 5.186 us (based on 1 run)
18 changes: 15 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,13 @@ test-names = uexpr-tests adt-tests type-tests eval-tests show-tests read-tests \

doc-names = conditionals functions

all-names = $(test-names:%=tests/%) $(example-names:%=examples/%) $(doc-names:%=doc/%)
benchmark-names = fused_sum

quine-test-targets = $(all-names:%=run-%)
quine-test-targets = \
$(test-names:%=run-tests/%) \
$(example-names:%=run-examples/%) \
$(doc-names:%=run-doc/%) \
$(benchmark-names:%=run-bench-tests/%)

update-test-targets = $(test-names:%=update-tests/%)
update-doc-targets = $(doc-names:%=update-doc/%)
Expand Down Expand Up @@ -264,6 +268,10 @@ run-doc/%: doc/%.dx just-build
misc/check-quine $< $(dex) script
run-examples/%: examples/%.dx just-build
misc/check-quine $< $(dex) -O script
# This runs the benchmark in test mode, which means we're checking
# that it's not broken, but not actually trying to measure runtimes
run-bench-tests/%: benchmarks/%.dx just-build
misc/check-quine $< $(dex) -O script

lower-tests: export DEX_LOWER=1
lower-tests: export DEX_ALLOW_CONTRACTIONS=0
Expand Down Expand Up @@ -301,7 +309,11 @@ update-examples/%: examples/%.dx just-build
$(dex) script $< > $<.tmp
mv $<.tmp $<

run-gpu-tests: export DEX_ALLOC_CONTRACTIONS=0
update-bench-tests/%: benchmarks/%.dx just-build
$(dex) script $< > $<.tmp
mv $<.tmp $<

run-gpu-tests: export DEX_ALLOW_CONTRACTIONS=0
run-gpu-tests: tests/gpu-tests.dx just-build
misc/check-quine $< $(dex) --backend llvm-cuda script

Expand Down
5 changes: 4 additions & 1 deletion src/lib/LLVMExec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ compileAndBench shouldSyncCUDA logger objFiles ast fname args resultTypes = do
freeLitVals resultPtr resultTypes
let sync = when shouldSyncCUDA $ synchronizeCUDA
exampleDuration <- snd <$> measureSeconds (run >> sync)
test_mode <- (Just "t" ==) <$> E.lookupEnv "DEX_TEST_MODE"
let timeBudget = 2 -- seconds
let benchRuns = (ceiling $ timeBudget / exampleDuration) :: Int
let benchRuns = if test_mode
then 1
else (ceiling $ timeBudget / exampleDuration) :: Int
sync
totalTime <- liftM snd $ measureSeconds $ do
forM_ [1..benchRuns] $ const run
Expand Down
6 changes: 4 additions & 2 deletions src/lib/PPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,10 @@ instance Pretty Output where
benchName <> hardline <>
"Compile time: " <> prettyDuration compileTime <> hardline <>
"Run time: " <> prettyDuration runTime <+>
(case stats of Just (runs, _) -> "\t" <> parens ("based on" <+> p runs <+> "runs")
Nothing -> "")
(case stats of
Just (runs, _) ->
"\t" <> parens ("based on" <+> p runs <+> plural "run" "runs" runs)
Nothing -> "")
where benchName = case name of "" -> ""
_ -> "\n" <> p name
pretty (PassInfo _ s) = p s
Expand Down

0 comments on commit 610fe4a

Please sign in to comment.