How to run a criterion benchmark for a monadic function

164 views Asked by At

With the criterion library I want to benchmark functions with a type of Monad m => a -> m b.

Here is a typical example with the JSM monad from the jsaddle library:

import           Criterion.Main
import           JSDOM.Types (JSM)
import           GHCJS.Buffer (Buffer(..))
import qualified Data.ByteString as BS (ByteString)

main :: IO ()
main = defaultMain [
  bgroup "Image" [ bench "buildBS"  $ nf buildBS 256
                 , bench "toJsBuff" $ nfIO (toJsBuff img)
                 ] ]

img :: BS.ByteString
img = id $! buildBS 256

buildBS :: Int -> BS.ByteString
buildBS = undefined

toJsBuff :: BS.ByteString -> JSM Buffer
toJsBuff bs = undefined

Now this does not compile because the function nfIO works only for the IO monad and not for any monad.

How can I benchmark such a monadic function?

Note: There is also a compile time error when I use nf instead of nfIO.

0

There are 0 answers