Haskell : Setting up Criterion with Stack

After fighting with stack for some time, i finally found how to do benchmarking with criterion and stack. Following workflow works for me :

1) Create a folder bench
2) Have your <benchmark-file>.hs in bench/
3) Don't have any module name in <benchmark-file>.hs
     so it can start directly with  import statements.
     import ... 
4) Add benchmark in your cabal file :
     Like
benchmark my-benchmark
  type:                       exitcode-stdio-1.0
  hs-source-dirs:      bench
  main-is:                  <benchmark-file>.hs
  build-depends:       criterion
                                , base
  ghc-options:          -O2 -threaded
                                 -Wall


 
5) Run stack bench.

Sample benchmark file:

import Criterion.Main as C
qsort1 :: (Ord a) => [a] -> [a]
qsort1 [] = []
qsort1 (x:xs) =
  qsort1 [y | y <- xs, y < x] ++
  [y | y <- xs, y == x] ++
  qsort1 [y | y <- xs, y > x]

main :: IO ()
main = C.defaultMain [
  C.bgroup "qsort1" [ C.bench "1000" $ C.whnf qsort1 [1..1000 :: Int]
                    , C.bench "10000" $ C.whnf qsort1 [1..2000 :: Int]
                    ]
  ] 

6) Run stack bench --benchmark-arguments "--output <benchmark-file>.html" to generate pretty graphs to look at in browser.

Comments