Skip to content

Commit

Permalink
Add --version flag. Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Gonzalez committed Oct 15, 2016
1 parent feb7880 commit beebfd1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
13 changes: 7 additions & 6 deletions bench.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: bench
version: 1.0.1
version: 1.0.2
synopsis: Command-line benchmark tool
description: Think of this as a more powerful alternative to the @time@
command. Use this command-line tool to benchmark a command
Expand All @@ -25,9 +25,10 @@ executable bench
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.5 && < 5
, criterion >= 1.1.1.0 && < 1.2
, text < 1.3
, silently >= 1.1.1 && < 1.3
, turtle >= 1.2.5 && < 1.3
build-depends: base >= 4.5 && < 5
, criterion >= 1.1.1.0 && < 1.2
, optparse-applicative >= 0.2.0 && < 0.14
, silently >= 1.1.1 && < 1.3
, text < 1.3
, turtle >= 1.2.5 && < 1.3
ghc-options: -Wall -O2 -threaded
54 changes: 33 additions & 21 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,59 @@
module Main where

import Control.Applicative
import Data.Monoid ((<>))
import Data.Text (Text)
import Turtle (Parser)
import Turtle (Parser, s, (%))

import qualified Criterion
import qualified Criterion.Main as Criterion
import qualified Criterion.Main.Options as Criterion
import qualified Data.Text as Text
import qualified Options.Applicative
import qualified System.IO as IO
import qualified System.IO.Silently as Silently
import qualified Turtle

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

data Options = Options [Text] Criterion.Mode | Version deriving (Show)

parser :: Parser Options
parser =
Options
<$> some (Turtle.argText "command(s)" "The command line(s) to benchmark")
<*> Criterion.parseWith Criterion.defaultConfig
Version
<$ Options.Applicative.flag'
()
( Options.Applicative.short 'v'
<> Options.Applicative.long "version"
<> Options.Applicative.help "Version number"
)
<|> Options
<$> 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
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
x <- Turtle.options "Command-line tool to benchmark other programs" parser
case x of
Options [command] mode -> benchCommand command mode
Options commands mode -> benchCommands commands mode
Version -> do
Turtle.printf ("bench version "%s%"\n") version

benchCommands :: [Text] -> Criterion.Mode -> IO ()
benchCommands commands mode = do
let benches = map buildBench commands
Criterion.runMode mode [Criterion.bgroup "bench" benches]

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

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

0 comments on commit beebfd1

Please sign in to comment.