Commits

Bryan O'Sullivan  committed eddafe7

Allow the specification of output plot sizes.

  • Participants
  • Parent commits 7f3bd20

Comments (0)

Files changed (4)

File Criterion/Config.hs

     mappend = max
 
 data PlotOutput = CSV
-                | PDF
-                | PNG
-                | SVG
-                | Window
-                  deriving (Eq, Ord, Bounded, Enum, Read, Show)
+                | PDF Int Int
+                | PNG Int Int
+                | SVG Int Int
+                | Window Int Int
+                  deriving (Eq, Ord, Read, Show)
 
 data Plot = KernelDensity
           | Timing
 
 defaultConfig :: Config
 defaultConfig = Config {
-                  cfgBanner       = ljust "hi mom!"
+                  cfgBanner       = ljust "I don't know what version I am."
                 , cfgConfInterval = ljust 0.95
                 , cfgPerformGC    = ljust False
                 , cfgPlot         = mempty

File Criterion/Main.hs

     , parseCommandLine
     ) where
 
+import Control.Monad (MonadPlus(..))
 import Criterion (Benchmarkable(..), Benchmark, bench, bgroup, runAndAnalyse)
 import Criterion.Config
 import Criterion.Environment (measureEnvironment)
 import Criterion.IO (note, printError)
 import Criterion.MultiMap (singleton)
-import Data.Char (toLower)
 import Data.List (isPrefixOf)
 import Data.Monoid (Monoid(..), Last(..))
 import System.Console.GetOpt
 import System.Environment (getArgs, getProgName)
 import System.Exit (ExitCode(..), exitWith)
-import qualified Data.Map as M
+import Text.ParserCombinators.Parsec
+
+parsePlot :: Parser PlotOutput
+parsePlot = try (dim "window" Window 800 600)
+    `mplus` try (dim "win" Window 800 600)
+    `mplus` try (dim "pdf" PDF 432 324)
+    `mplus` try (dim "win" PNG 800 600)
+    `mplus` try (dim "svg" SVG 432 324)
+    `mplus` (string "csv" >> return CSV)
+  where dim s c dx dy = do
+          string s
+          try (uncurry c `fmap` dimensions) `mplus`
+              (eof >> return (c dx dy))
+        dimensions = do
+            char ':'
+            a <- many1 digit
+            char 'x'
+            b <- many1 digit
+            case (reads a, reads b) of
+              ([(x,[])],[(y,[])]) -> return (x, y)
+              _                   -> mzero
+           <?> "dimensions"
 
 plot :: Plot -> String -> IO Config
-plot p s = case M.lookup s m of
-             Nothing -> parseError "unknown plot type"
-             Just t  -> return mempty { cfgPlot = singleton p t }
-  where m = M.fromList $ ("win", Window) :
-            [(map toLower (show t),t) | t <- [minBound..maxBound]]
+plot p s = case parse parsePlot "" s of
+             Left _err -> parseError "unknown plot type"
+             Right t   -> return mempty { cfgPlot = singleton p t }
 
 ci :: String -> IO Config
 ci s = case reads s' of
 printBanner :: Config -> IO ()
 printBanner cfg =
     case cfgBanner cfg of
-      Last (Just b) -> note cfg b
+      Last (Just b) -> note cfg "%s\n" b
       _             -> note cfg "Hey, nobody told me what version I am!\n"
 
 printUsage :: [OptDescr (IO Config)] -> ExitCode -> IO a
 printUsage options exitCode = do
   p <- getProgName
   putStr (usageInfo ("Usage: " ++ p ++ " [OPTIONS]") options)
+  mapM_ putStrLn [
+       ""
+    , "Plot types:"
+    , "  window or win   display a window immediately"
+    , "  csv             save a CSV file"
+    , "  pdf             save a PDF file"
+    , "  png             save a PNG file"
+    , "  svg             save an SVG file"
+    , ""
+    , "You can specify plot dimensions via a suffix, e.g. \"window:640x480\""
+    , "Units are pixels for png and window, 72dpi points for pdf and svg"
+    ]
   exitWith exitCode
 
 parseCommandLine :: Config -> [OptDescr (IO Config)] -> [String]

File Criterion/Plot.hs

     forM_ (fromU $ indexedU times) $ \(x :*: y) ->
       putLn h (show x ++ ',' : show y)
 
-plotTiming PDF desc times =
-  renderableToPNGFile (renderTiming desc times) 800 600
+plotTiming (PDF x y) desc times =
+  renderableToPDFFile (renderTiming desc times) x y
                       (manglePath "png" $ desc ++ " timings")
 
-plotTiming PNG desc times =
-  renderableToPNGFile (renderTiming desc times) 800 600
+plotTiming (PNG x y) desc times =
+  renderableToPNGFile (renderTiming desc times) x y
                       (manglePath "png" $ desc ++ " timings")
 
-plotTiming SVG desc times =
-  renderableToSVGFile (renderTiming desc times) 800 600
+plotTiming (SVG x y) desc times =
+  renderableToSVGFile (renderTiming desc times) x y
                       (manglePath "png" $ desc ++ " timings")
 
-plotTiming Window desc times =
-  renderableToWindow (renderTiming desc times) 800 600
+plotTiming (Window x y) desc times =
+  renderableToWindow (renderTiming desc times) x y
 
 plotKDE :: PlotOutput -> String -> Points -> UArr Double -> IO ()
 
-plotKDE PDF desc points pdf =
-  renderableToPDFFile (renderKDE desc points pdf) 800 600
+plotKDE (PDF x y) desc points pdf =
+  renderableToPDFFile (renderKDE desc points pdf) x y
                       (manglePath "png" $ desc ++ " densities")
 
-plotKDE PNG desc points pdf =
-  renderableToPNGFile (renderKDE desc points pdf) 800 600
+plotKDE (PNG x y) desc points pdf =
+  renderableToPNGFile (renderKDE desc points pdf) x y
                       (manglePath "png" $ desc ++ " densities")
 
-plotKDE SVG desc points pdf =
-  renderableToSVGFile (renderKDE desc points pdf) 800 600
+plotKDE (SVG x y) desc points pdf =
+  renderableToSVGFile (renderKDE desc points pdf) x y
                       (manglePath "png" $ desc ++ " densities")
 
-plotKDE Window desc points pdf =
-    renderableToWindow (renderKDE desc points pdf) 800 600
+plotKDE (Window x y) desc points pdf =
+    renderableToWindow (renderKDE desc points pdf) x y
 
 plotKDE dest _desc _points _pdf = do
   printError "plotKDE %s: not yet implemented\n" (show dest)

File criterion.cabal

     data-accessor,
     filepath,
     parallel,
+    parsec,
     statistics >= 0.3.4,
     time,
     uvector,