Skip to content

Commit 08f7e0c

Browse files
committed
Use "criterion" for benchmarks
1 parent 59c3f6e commit 08f7e0c

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

benchmarks/Counter.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@
33
-- | Perform 100,000 atomic increments using 100 concurrent writers.
44
module Main where
55

6+
import Prelude hiding (read)
7+
68
import Control.Concurrent
79
import Control.Monad
10+
import Criterion
11+
import Criterion.Main (defaultMain)
12+
import Data.Int (Int64)
813
import System.Metrics.Counter
914

1015
main :: IO ()
11-
main = do
16+
main = defaultMain
17+
[ bench "Increment counter with multiple writers" $
18+
whnfIO incrementWithMultipleWriters
19+
]
20+
21+
incrementWithMultipleWriters :: IO Int64
22+
incrementWithMultipleWriters = do
1223
counter <- new
1324
locks <- replicateM n newEmptyMVar
1425
mapM_ (forkIO . work counter iters) locks
1526
mapM_ takeMVar locks
27+
total <- read counter
28+
unless (fromIntegral total == n*iters) $
29+
error "Incorrect count!"
30+
pure total
1631
where
1732
n = 100
1833
iters = 100000

benchmarks/Distribution.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@
44
-- writers.
55
module Main where
66

7+
import Prelude hiding (read)
8+
79
import Control.Concurrent
810
import Control.Monad
11+
import Criterion
12+
import Criterion.Main (defaultMain)
13+
import Data.Int (Int64)
914
import System.Metrics.Distribution
1015

1116
main :: IO ()
12-
main = do
17+
main = defaultMain
18+
[ bench "Add to distribution with multiple writers" $
19+
whnfIO addWithMultipleWriters
20+
]
21+
22+
addWithMultipleWriters :: IO Int64
23+
addWithMultipleWriters = do
1324
distrib <- new
1425
locks <- replicateM n newEmptyMVar
1526
mapM_ (forkIO . work distrib iters) locks
1627
mapM_ takeMVar locks
28+
total <- count <$> read distrib
29+
unless (fromIntegral total == n*iters) $
30+
error "Incorrect count!"
31+
pure total
1732
where
1833
n = 100
1934
iters = 100000

ekg-core.cabal

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ benchmark counter
5858
type: exitcode-stdio-1.0
5959
build-depends:
6060
base,
61-
ekg-core
61+
ekg-core,
62+
criterion ^>= 1.5.9.0
6263
default-language: Haskell2010
6364
hs-source-dirs: benchmarks
6465
ghc-options: -O2 -threaded -Wall
@@ -68,7 +69,8 @@ benchmark distribution
6869
type: exitcode-stdio-1.0
6970
build-depends:
7071
base,
71-
ekg-core
72+
ekg-core,
73+
criterion ^>= 1.5.9.0
7274
default-language: Haskell2010
7375
hs-source-dirs: benchmarks
7476
ghc-options: -O2 -threaded -Wall

sample.hie.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ cradle:
44
component: "lib:ekg-core"
55
- path: "./test"
66
component: "test:ekg-core-test"
7+
- path: "./benchmarks"
8+
component: "bench:counter"
9+
- path: "./benchmarks"
10+
component: "bench:distribution"

0 commit comments

Comments
 (0)