Skip to content

Commit a00038e

Browse files
committed
benchmark-blake2: implement benchmark
1 parent 6885351 commit a00038e

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

benchmark-blake2/Main.hs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{-# LANGUAGE OverloadedStrings #-}
2-
{-# LANGUAGE PackageImports #-}
2+
{-# LANGUAGE PackageImports #-}
33

44
module Main where
55

6+
import Data.ByteString.Char8 (pack)
7+
import Data.List (foldl')
8+
69
import "criterion" Criterion.Main
710
import qualified "memory" Data.ByteArray as B
811
import qualified "base16-bytestring" Data.ByteString.Base16 as B16
@@ -25,16 +28,20 @@ patchedHash = Patched.hash 64 mempty
2528

2629
main :: IO ()
2730
main = defaultMain
28-
[ bgroup "blake2"
29-
[
30-
31+
[ bgroup "hashing"
32+
[ bench "blake2" $ nfIO (bencher originalHash)
33+
, bench "blake2-patched" $ nfIO (bencher patchedHash)
34+
, bench "cryptonite" $ nfIO (bencher cryptoniteHash)
3135
]
32-
, bgroup "blake2-patched"
33-
[
36+
]
3437

35-
]
36-
, bgroup "cryptonite"
37-
[
38+
hashes :: IO [BS.ByteString]
39+
hashes = let
40+
ns = map (cryptoniteHash . pack . show) [1..100000]
41+
in length ns `seq` pure ns
3842

39-
]
40-
]
43+
bencher :: (BS.ByteString -> BS.ByteString) -> IO Bool
44+
bencher hash = do
45+
hs <- hashes
46+
let every = foldl' (\(i, b) h -> if (hash . pack . show $ (i+1)) == h then (i+1, b) else (i+1, False)) (0, True) hs
47+
pure $ snd every

0 commit comments

Comments
 (0)