benchpress-0.1: Micro-benchmarking with detailed statistics.





Benchmarks actions and produces statistics such as min, mean, median, standard deviation, and max execution time. Also computes execution time percentiles. There are functions to pretty-print the results.

Here's an example showing a benchmark of copying a file:

 import Control.Monad.Trans
 import qualified Data.ByteString as B
 import System.IO
 import Test.BenchPress

 inpath, outpath :: String
 inpath = "/tmp/infile"
 outpath = "/tmp/outfile"

 blockSize :: Int
 blockSize = 4 * 1024

 copyUsingByteString :: Handle -> Handle -> IO ()
 copyUsingByteString inf outf = go
       go = do
         bs <- B.hGet inf blockSize
         let numRead = B.length bs
         if numRead > 0
            then B.hPut outf bs >> go
            else return ()

 main :: IO ()
 main = bench 100 $ liftIO $ do
          inf <- openBinaryFile inpath ReadMode
          outf <- openBinaryFile outpath WriteMode
          copyUsingByteString inf outf
          hClose outf
          hClose inf


The Benchmark type

start :: Benchmark ()Source

Starts the benchmark timer again after it has been previously stopped by calling stop. It's not necessary to call start at the beginning of the action you want to benchmark as it is done automatically by benchmark.

stop :: Benchmark ()Source

Stops the benchmark timer. Stopping the timer is useful when you need to perform some set up that you don't want to count in the benchmark timings. It's not necessary to call stop at the end of the action you want to benchmark as it is done automatically by benchmark.

Running a benchmark

benchmark :: Int -> Benchmark a -> IO StatsSource

benchmark iters bm runs the action bm iters times measuring the execution time of each run.

bench :: Int -> Benchmark a -> IO ()Source

Convenience function that runs a benchmark using benchmark and prints timing statistics.

benchMany :: Int -> [(String, Benchmark a)] -> IO ()Source

Convenience function that runs several benchmarks using benchmark and prints a timing statistics summary. Each benchmark has an associated label that is used to identify the benchmark in the printed results.

Benchmark stats

data Stats Source

Timing statistics for the benchmark. All measured times are given in milliseconds.




min :: Double

Shortest execution time.

mean :: Double

Average execution time.

stddev :: Double

Execution time standard deviation.

median :: Double

Median execution time.

max :: Double

Longest execution time.

percentiles :: [(Int, Double)]

Execution time divided into percentiles. The first component of the pair is the percentile given as an integer between 0 and 100, inclusive. The second component is the execution time of the slowest iteration within the percentile.