Skip to content

Commit

Permalink
Merge pull request #6 from adarqui/multiple
Browse files Browse the repository at this point in the history
Supports single & multiple commands
  • Loading branch information
Gabriella439 committed Aug 21, 2016
2 parents 26e72d9 + 0bf6009 commit feb7880
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.stack-work
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,31 @@ std dev 47.69 μs (40.09 μs .. 57.91 μs)
variance introduced by outliers: 81% (severely inflated)
```

All output from the command being benchmarked is discarded
All output from the command being benchmarked is discarded.

Multiple commands are also supported:

```bash
$ bench id ls "sleep 0.1"
benchmarking bench/id
time 4.798 ms (4.764 ms .. 4.833 ms)
0.999 R² (0.998 R² .. 1.000 R²)
mean 4.909 ms (4.879 ms .. 4.953 ms)
std dev 104.6 μs (78.91 μs .. 135.7 μs)

benchmarking bench/ls
time 2.941 ms (2.889 ms .. 3.006 ms)
0.996 R² (0.992 R² .. 0.998 R²)
mean 3.051 ms (3.015 ms .. 3.094 ms)
std dev 129.7 μs (104.3 μs .. 178.3 μs)
variance introduced by outliers: 25% (moderately inflated)

benchmarking bench/sleep 0.1
time 109.9 ms (108.5 ms .. 111.0 ms)
1.000 R² (1.000 R² .. 1.000 R²)
mean 109.2 ms (108.5 ms .. 109.7 ms)
std dev 903.0 μs (676.4 μs .. 1.212 ms)
```

You can also output an HTML file graphing the distribution of
timings by using the `--output` flag:
Expand Down
31 changes: 24 additions & 7 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Main where

Expand All @@ -15,20 +16,36 @@ import qualified System.IO.Silently as Silently
import qualified Turtle

data Options = Options
{ command :: Text
, mode :: Criterion.Mode
}
{ cmd :: [Text]
, mode :: Criterion.Mode
} deriving (Show)

parser :: Parser Options
parser =
Options
<$> Turtle.argText "command" "The command line to benchmark"
<$> some (Turtle.argText "command(s)" "The command line(s) to benchmark")
<*> Criterion.parseWith Criterion.defaultConfig

main :: IO ()
main = do
o <- Turtle.options "Command-line tool to benchmark other programs" parser
let io = Turtle.shells (command o) empty
case (cmd o) of
[command] -> benchCommand command o
commands -> benchCommands commands o

benchCommands :: [Text] -> Options -> IO ()
benchCommands commands opts@Options{..} = do
let benches = map (\command -> buildBench command opts) commands
Criterion.runMode mode [Criterion.bgroup "bench" benches]

benchCommand :: Text -> Options -> IO ()
benchCommand command opts@Options{..} = do
let bench = buildBench command opts
Criterion.runMode mode [ bench ]

buildBench :: Text -> Options -> Criterion.Benchmark
buildBench command Options{..} = do
let io = Turtle.shells command empty
let benchmark = Criterion.nfIO (Silently.hSilence [IO.stdout, IO.stderr] io)
let name = Text.unpack (command o)
Criterion.runMode (mode o) [ Criterion.bench name benchmark ]
let bench = Criterion.bench (Text.unpack command) benchmark
bench

0 comments on commit feb7880

Please sign in to comment.