Skip to content

Instantly share code, notes, and snippets.

@nmattia
Last active July 14, 2016 22:00
Show Gist options
  • Save nmattia/a0ca4cb6d26c609a39e25f984b9350aa to your computer and use it in GitHub Desktop.
Save nmattia/a0ca4cb6d26c609a39e25f984b9350aa to your computer and use it in GitHub Desktop.
Compare criterion outputs for regression
#!/usr/bin/env stack
-- stack --install-ghc runghc --package cassava --package vector
{-|
Compare two criterion outputs. Results are displayed in percent. Takes two
filepaths: original csv and new csv.
To generate csvs:
stack bench my-project:my-bench --benchmark-arguments "--csv out.csv"
E.g.: ./compare-crt-csv.hs target experiment
> my/benchmark +10%
means that "experiment" is 10% slower on benchmark "my/benchmark".
-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Csv
import Data.Monoid ((<>))
import Control.Monad (forM_)
import Data.Vector (Vector, find)
import System.Environment (getArgs)
import qualified Data.ByteString.Lazy as BL
data Measurement = Measurement String Double deriving (Show)
instance FromNamedRecord Measurement where
parseNamedRecord m = Measurement <$>
m .: "Name" <*>
m .: "MeanLB"
main = do
source <- getOrCrash =<< (!! 0) <$> getArgs
target <- getOrCrash =<< (!! 1) <$> getArgs
forM_ source $ \(Measurement name val) ->
case lookupVec name target of
Just (Measurement _ val') -> putStrLn (name <> " " <> perc)
where perc = show (round $ 100 * (val' - val)/val) <> "%"
Nothing -> return ()
where lookupVec str = find (\(Measurement name' _) -> name' == str)
getOrCrash :: String -> IO (Vector Measurement)
getOrCrash filepath = do
contents <- BL.readFile filepath
case decodeByName contents :: Either String (Header, Vector Measurement) of
Left err -> putStrLn err >> error "Could not continue"
Right (_, measurements) -> return measurements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment