Commits

Bryan O'Sullivan  committed 2ce01d1

Get criterion working on OS X

  • Participants
  • Parent commits 7462d83

Comments (0)

Files changed (3)

File Criterion/Plot.hs

-{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE CPP, ScopedTypeVariables #-}
 
 -- |
 -- Module      : Criterion.Plot
     ) where
 
 import Criterion.Config
-import Data.Accessor ((^=))
 import Data.Array.Vector
 import Data.Char (isSpace, toLower)
 import Data.Foldable (forM_)
 import Data.List (group, intersperse)
-import Graphics.Rendering.Chart hiding (Plot,c)
-import Graphics.Rendering.Chart.Gtk (renderableToWindow)
 import Statistics.KernelDensity (Points, fromPoints)
 import Statistics.Types (Sample)
 import System.FilePath (pathSeparator)
 import Text.Printf (printf)
 import qualified Criterion.MultiMap as M
 
+#ifdef HAVE_CHART
+import Data.Accessor ((^=))
+import Graphics.Rendering.Chart hiding (Plot,c)
+import Graphics.Rendering.Chart.Gtk (renderableToWindow)
+#else
+import Criterion.IO (printError)
+#endif
+
 plotWith :: Plot -> Config -> (PlotOutput -> IO ()) -> IO ()
 plotWith p cfg plot =
   case M.lookup p (cfgPlot cfg) of
     forM_ (fromU $ indexedU times) $ \(x :*: y) ->
       putRow h [show x, show y]
 
+#ifdef HAVE_CHART
 plotTiming (PDF x y) desc times =
   renderableToPDFFile (renderTiming desc times) x y
                       (mangle $ printf "%s timings %dx%d.pdf" desc x y)
 
 plotTiming (Window x y) desc times =
   renderableToWindow (renderTiming desc times) x y
+#else
+plotTiming output _desc _times =
+  printError "ERROR: output type %s not supported on this platform\n"
+             (show output)
+#endif
 
 -- | Plot kernel density estimate.
 plotKDE :: PlotOutput           -- ^ The kind of output desired.
     forM_ (zip (fromU pdf) (fromU (fromPoints points))) $ \(x, y) ->
       putRow h [show x, show y]
 
+#ifdef HAVE_CHART
 plotKDE (PDF x y) desc points pdf =
   renderableToPDFFile (renderKDE desc points pdf) x y
                       (mangle $ printf "%s densities %dx%d.pdf" desc x y)
 
 plotKDE (Window x y) desc points pdf =
     renderableToWindow (renderKDE desc points pdf) x y
+#else
+plotKDE output _desc _points _pdf =
+  printError "ERROR: output type %s not supported on this platform\n"
+             (show output)
+#endif
 
+#ifdef HAVE_CHART
 renderTiming :: String -> Sample -> Renderable ()
 renderTiming desc times = toRenderable layout
   where
 secAxis = la_labelf ^= secs
         $ defaultLinearAxis
 
-writeTo :: FilePath -> (Handle -> IO a) -> IO a
-writeTo path = withBinaryFile path WriteMode
-
-escapeCSV :: String -> String
-escapeCSV xs | any (`elem`xs) escapes = '"' : concatMap esc xs ++ "\""
-          | otherwise              = xs
-    where esc '"' = "\"\""
-          esc c   = [c]
-          escapes = "\"\r\n,"
-
-putRow :: Handle -> [String] -> IO ()
-putRow h s = hPutStr h (concat (intersperse "," (map escapeCSV s)) ++ "\r\n")
-
--- | Get rid of spaces and other potentially troublesome characters
--- from output.
-mangle :: String -> FilePath
-mangle = concatMap (replace ((==) '-' . head) "-")
-       . group
-       . map (replace isSpace '-' . replace (==pathSeparator) '-' . toLower)
-    where replace p r c | p c       = r
-                        | otherwise = c
-
 -- | Try to render meaningful time-axis labels.
 --
 -- /FIXME/: Trouble is, we need to know the range of times for this to
                | t >= 1e2  = printf "%.0f %s" t u
                | t >= 1e1  = printf "%.1f %s" t u
                | otherwise = printf "%.2f %s" t u
+#endif
+
+writeTo :: FilePath -> (Handle -> IO a) -> IO a
+writeTo path = withBinaryFile path WriteMode
+
+escapeCSV :: String -> String
+escapeCSV xs | any (`elem`xs) escapes = '"' : concatMap esc xs ++ "\""
+          | otherwise              = xs
+    where esc '"' = "\"\""
+          esc c   = [c]
+          escapes = "\"\r\n,"
+
+putRow :: Handle -> [String] -> IO ()
+putRow h s = hPutStr h (concat (intersperse "," (map escapeCSV s)) ++ "\r\n")
+
+-- | Get rid of spaces and other potentially troublesome characters
+-- from output.
+mangle :: String -> FilePath
+mangle = concatMap (replace ((==) '-' . head) "-")
+       . group
+       . map (replace isSpace '-' . replace (==pathSeparator) '-' . toLower)
+    where replace p r c | p c       = r
+                        | otherwise = c
 and take a look at the programs in the examples directory.
 
 
+Building and installing
+-----------------------
+
+To build and install criterion, just run
+
+  cabal install criterion
+
+On Mac OS X, the Chart library that criterion uses is not available,
+so you'll have to build without it.
+
+  cabal install criterion -f-chart
+ 
+This will lose you the ability to generate charts, but you'll still be
+able to generate CSV files and import them into your favourite
+spreadsheet, or gnuplot, or whatnot.
+
+
 Get involved!
 -------------
 

File criterion.cabal

   The fastest way to get started is to read the documentation and
   examples in the Criterion.Main module.
 
+flag Chart
+  Description: enable use of the Chart package
+
 library
   exposed-modules:
     Criterion
     Criterion.Types
 
   build-depends:
-    Chart,
     base < 5,
     bytestring >= 0.9 && < 1.0,
     containers,
-    data-accessor,
     filepath,
     parallel,
     parsec,
     uvector,
     uvector-algorithms >= 0.2
 
+  if flag(chart)
+    build-depends:
+      Chart,
+      data-accessor
+    ghc-options: -DHAVE_CHART
+
   -- gather extensive profiling data for now
   ghc-prof-options: -auto-all