Commits

Bryan O'Sullivan committed d85899d

Add the ability to list benchmarks.

Comments (0)

Files changed (3)

                    (secs $ estLowerBound e) (secs $ estUpperBound e)
                    (estConfidenceLevel e)
 
-runAndAnalyse :: (String -> Bool) -> Config -> Environment -> Benchmark -> IO ()
-runAndAnalyse p cfg env (Benchmark desc b)
-    | p desc    = do note cfg "\nbenchmarking %s\n" desc
-                     runAndAnalyseOne cfg env desc b
-    | otherwise = return ()
-runAndAnalyse p cfg env (BenchGroup desc bs) =
-    mapM_ (runAndAnalyse p' cfg env) bs
-  where p' | p desc    = const True
-           | otherwise = p
+-- | Run, and analyse, one or more benchmarks.
+runAndAnalyse :: (String -> Bool) -- ^ A predicate that chooses which
+                                  -- benchmarks to run by name.
+              -> Config
+              -> Environment
+              -> Benchmark
+              -> IO ()
+runAndAnalyse p cfg env = go ""
+  where go pfx (Benchmark desc b)
+            | p desc'   = do note cfg "\nbenchmarking %s\n" desc'
+                             runAndAnalyseOne cfg env desc' b
+            | otherwise = return ()
+            where desc' = prefix pfx desc
+        go pfx (BenchGroup desc bs) = mapM_ (go (prefix pfx desc)) bs
+        prefix ""  desc = desc
+        prefix pfx desc = pfx ++ '/' : desc

Criterion/Config.hs

                  deriving (Eq, Ord, Bounded, Enum, Read, Show)
 
 data PrintExit = Nada
+               | List
                | Version
                | Help
                  deriving (Eq, Ord, Bounded, Enum, Read, Show)

Criterion/Main.hs

     , defaultMain
     , defaultMainWith
     , defaultOptions
-    , parseCommandLine
+    , parseArgs
     ) where
 
 import Control.Monad (MonadPlus(..))
 
 defaultMainWith :: Config -> [Benchmark] -> IO ()
 defaultMainWith defCfg bs = do
-  (cfg, args) <- parseCommandLine defCfg defaultOptions =<< getArgs
-  env <- measureEnvironment cfg
-  let shouldRun b = null args || any (`isPrefixOf` b) args
-  mapM_ (runAndAnalyse shouldRun cfg env) bs
+  (cfg, args) <- parseArgs defCfg defaultOptions =<< getArgs
+  if cfgPrintExit cfg == List
+    then do
+      note cfg "Benchmarks:\n"
+      mapM_ (note cfg "  %s\n") (sort $ concatMap benchNames bs)
+    else do
+      env <- measureEnvironment cfg
+      let shouldRun b = null args || any (`isPrefixOf` b) args
+      mapM_ (runAndAnalyse shouldRun cfg env) bs