Commits

Bryan O'Sullivan committed e13009b

WIP on better plotting.

  • Participants
  • Parent commits 1fff75c

Comments (0)

Files changed (3)

 import Criterion.Environment (Environment(..))
 import Criterion.IO (note, prolix)
 import Criterion.Measurement (getTime, runForAtLeast, secs, time_)
-import Criterion.Plot (plotWith)
+import Criterion.Plot (plotWith, foo, bar)
 import Criterion.Types (Benchmarkable(..), Benchmark(..), bench, bgroup)
 import Data.Array.Vector ((:*:)(..), lengthU, mapU)
 import Prelude hiding (catch)
   let numSamples = lengthU times
   plotWith Timing cfg (desc ++ " timing") "sample" "time"
            (mapU fromIntegral $ indices times) times
+  foo desc times
   let (points, pdf) = epanechnikovPDF 100 times
   plotWith KernelDensity cfg (desc ++ " kde") "time" "pdf"
            (fromPoints points) pdf
+  bar desc points pdf
   let ests = [mean,stdDev]
       numResamples = fromLJ cfgResamples cfg
   note cfg "bootstrapping with %d resamples\n" numResamples

Criterion/Plot.hs

+{-# LANGUAGE ScopedTypeVariables #-}
+
 module Criterion.Plot
     (
       plotWith
+    , foo
+    , bar
     ) where
 
 import Criterion.Config
 import Data.Char (isSpace)
 import Data.Foldable (forM_)
 import Data.List (group)
+import Statistics.KernelDensity (Points, fromPoints)
 import Graphics.Rendering.Chart.Simple hiding (plot)
 import System.FilePath (addExtension, pathSeparator)
 import System.IO (IOMode(..), Handle, hPutStr, stdout, withBinaryFile)
 import qualified Criterion.MultiMap as M
+import Graphics.Rendering.Chart hiding (Plot,c)
+import Graphics.Rendering.Chart.Gtk
+import Data.Accessor
+import Statistics.Types (Sample)
+import Text.Printf
 
 manglePath :: String -> String -> FilePath
 manglePath _ "-"    = "-"
 
 putLn :: Handle -> String -> IO ()
 putLn h s = hPutStr h (s ++ "\r\n")
+
+renderTimes :: String -> Sample -> Renderable ()
+renderTimes desc times = toRenderable layout
+  where
+    layout = layout1_title ^= "Execution times for \"" ++ desc ++ "\""
+           $ layout1_plots ^= [ Left (plotBars bars) ]
+           $ layout1_left_axis ^= leftAxis
+           $ layout1_bottom_axis ^= bottomAxis
+           $ defaultLayout1 :: Layout1 Double Double
+
+    leftAxis = laxis_generate ^= autoScaledAxis secAxis
+             $ laxis_title ^= "execution time"
+             $ defaultLayoutAxis
+
+    bottomAxis = laxis_title ^= "number of samples"
+               $ defaultLayoutAxis
+
+    bars = plot_bars_values ^= (zip [0..] . map (:[]) . fromU $ times)
+         $ defaultPlotBars
+
+renderKDE :: String -> Points -> UArr Double -> Renderable ()
+renderKDE desc points pdf = toRenderable layout
+  where
+    layout = layout1_title ^= "Densities of execution times for \"" ++
+                              desc ++ "\""
+           $ layout1_plots ^= [ Left (toPlot lines) ]
+           $ layout1_left_axis ^= leftAxis
+           $ layout1_bottom_axis ^= bottomAxis
+           $ defaultLayout1 :: Layout1 Double Double
+
+    leftAxis = laxis_title ^= "estimate of probability density"
+             $ defaultLayoutAxis
+
+    bottomAxis = laxis_generate ^= autoScaledAxis secAxis
+               $ laxis_title ^= "execution time"
+               $ defaultLayoutAxis
+
+    lines = plot_lines_values ^= [zip (fromU (fromPoints points)) (fromU spdf)]
+          $ defaultPlotLines
+
+    spdf = mapU (/ sumU pdf) pdf
+
+secAxis :: LinearAxisParams
+secAxis = la_labelf ^= secs
+        $ defaultLinearAxis
+
+foo desc times = renderableToWindow (renderTimes desc times) 800 600
+bar desc points pdf = renderableToWindow (renderKDE desc points pdf) 800 600
+
+secs :: Double -> String
+secs k
+    | k < 0      = '-' : secs (-k)
+    | k >= 1e9   = (k/1e9)  `with` "Gs"
+    | k >= 1e6   = (k/1e6)  `with` "Ms"
+    | k >= 1e4   = (k/1e3)  `with` "Ks"
+    | k >= 1     = k        `with` "s"
+    | k >= 1e-3  = (k*1e3)  `with` "ms"
+    | k >= 1e-6  = (k*1e6)  `with` "us"
+    | k >= 1e-9  = (k*1e9)  `with` "ns"
+    | k >= 1e-12 = (k*1e12) `with` "ps"
+    | otherwise  = printf "%g s" k
+     where with (t :: Double) (u :: String)
+               | t >= 1e9  = printf "%.4g %s" t u
+               | t >= 1e6  = printf "%.0f %s" t u
+               | t >= 1e5  = printf "%.0f %s" t u
+               | t >= 1e4  = printf "%.0f %s" t u
+               | t >= 1e3  = printf "%.0f %s" t u
+               | t >= 1e2  = printf "%.0f %s" t u
+               | t >= 1e1  = printf "%.1f %s" t u
+               | otherwise = printf "%.1f %s" t u
     Criterion.Types
 
   build-depends:
-    base       < 5,
+    Chart,
+    base < 5,
     bytestring >= 0.9 && < 1.0,
     containers,
+    data-accessor,
     filepath,
-    uvector,
-    uvector-algorithms >= 0.2,
-    Chart,
+    parallel,
     statistics >= 0.3.4,
     time,
-    parallel
+    uvector,
+    uvector-algorithms >= 0.2
 
   -- gather extensive profiling data for now
   ghc-prof-options: -auto-all