{-# LANGUAGE RecordWildCards #-}
-- |
-- Module      : Criterion
-- Copyright   : (c) 2009-2014 Bryan O'Sullivan
--
-- License     : BSD-style
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : GHC
--
-- Core benchmarking code.

module Criterion
    (
    -- * Benchmarkable code
      Benchmarkable
    -- * Creating a benchmark suite
    , Benchmark
    , env
    , envWithCleanup
    , perBatchEnv
    , perBatchEnvWithCleanup
    , perRunEnv
    , perRunEnvWithCleanup
    , toBenchmarkable
    , bench
    , bgroup
    -- ** Running a benchmark
    , nf
    , whnf
    , nfIO
    , whnfIO
    , nfAppIO
    , whnfAppIO
    -- * For interactive use
    , benchmark
    , benchmarkWith
    , benchmark'
    , benchmarkWith'
    ) where

import Control.Monad (void)
import Criterion.IO.Printf (note)
import Criterion.Internal (runAndAnalyseOne)
import Criterion.Main.Options (defaultConfig)
import Criterion.Measurement (initializeTime)
import Criterion.Monad (withConfig)
import Criterion.Types

-- | Run a benchmark interactively, and analyse its performance.
benchmark :: Benchmarkable -> IO ()
benchmark :: Benchmarkable -> IO ()
benchmark Benchmarkable
bm = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ Benchmarkable -> IO Report
benchmark' Benchmarkable
bm

-- | Run a benchmark interactively, analyse its performance, and
-- return the analysis.
benchmark' :: Benchmarkable -> IO Report
benchmark' :: Benchmarkable -> IO Report
benchmark' = Config -> Benchmarkable -> IO Report
benchmarkWith' Config
defaultConfig

-- | Run a benchmark interactively, and analyse its performance.
benchmarkWith :: Config -> Benchmarkable -> IO ()
benchmarkWith :: Config -> Benchmarkable -> IO ()
benchmarkWith Config
cfg Benchmarkable
bm = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ Config -> Benchmarkable -> IO Report
benchmarkWith' Config
cfg Benchmarkable
bm

-- | Run a benchmark interactively, analyse its performance, and
-- return the analysis.
benchmarkWith' :: Config -> Benchmarkable -> IO Report
benchmarkWith' :: Config -> Benchmarkable -> IO Report
benchmarkWith' Config
cfg Benchmarkable
bm = do
  IO ()
initializeTime
  forall a. Config -> Criterion a -> IO a
withConfig Config
cfg forall a b. (a -> b) -> a -> b
$ do
    Any
_ <- forall r. CritHPrintfType r => String -> r
note String
"benchmarking...\n"
    Analysed Report
rpt <- Int -> String -> Benchmarkable -> Criterion DataRecord
runAndAnalyseOne Int
0 String
"function" Benchmarkable
bm
    forall (m :: * -> *) a. Monad m => a -> m a
return Report
rpt