Skip to content

Commit

Permalink
Fix bug in FromJSON instance for Measured. It was inconsistent with t…
Browse files Browse the repository at this point in the history
…he ToJSON instance.
  • Loading branch information
rrnewton committed May 30, 2016
1 parent deb3d68 commit bfa9dbe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Criterion/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Criterion.Monad (Criterion)
import Criterion.Report (report)
import Criterion.Types hiding (measure)
import qualified Data.Map as Map
import Data.Vector (Vector, fromList)
import qualified Data.Vector as V
import Statistics.Resampling.Bootstrap (Estimate(..))
import System.Directory (getTemporaryDirectory, removeFile)
import System.IO (IOMode(..), SeekMode(..), hClose, hSeek, openBinaryFile,
Expand All @@ -54,7 +54,7 @@ runOne i desc bm = do
return (Measurement i desc meas)

-- | Analyse a single benchmark.
analyseOne :: Int -> String -> Vector Measured -> Criterion DataRecord
analyseOne :: Int -> String -> V.Vector Measured -> Criterion DataRecord
analyseOne i desc meas = do
Config{..} <- ask
_ <- prolix "analysing with %d resamples\n" resamples
Expand Down Expand Up @@ -130,6 +130,11 @@ runAndAnalyse select bs = do
for select bs $ \idx desc bm -> do
_ <- note "benchmarking %s\n" desc
Analysed rpt <- runAndAnalyseOne idx desc bm
liftIO $ putStrLn $ "First Measured in report: " ++ show (V.head (reportMeasured rpt))
liftIO $ putStrLn $ "Same thing JSON-encoded " ++ show (Aeson.encode $ V.head $ reportMeasured rpt)
liftIO $ putStrLn $ "Round tripped through JSON " ++ show
(Aeson.eitherDecode (Aeson.encode $ V.head $ reportMeasured rpt)
:: Either String Measured)
liftIO $ L.hPut handle (Aeson.encode (rpt::Report))
liftIO $ hPutStr handle ","
liftIO $ hPutStr handle "]]\n"
Expand Down
11 changes: 9 additions & 2 deletions Criterion/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,15 @@ data Measured = Measured {
instance FromJSON Measured where
parseJSON v = do
(a,b,c,d,e,f,g,h,i,j,k) <- parseJSON v
return $ Measured a b c d e f g h i j k

-- The first four fields are not subject to the encoding policy:
return $ Measured a b c d
(int e) (int f) (int g)
(db h) (db i) (db j) (db k)
where int = toInt; db = toDouble

-- Here we treat the numeric fields as `Maybe Int64` and `Maybe Double`
-- and we use a specific policy for deciding when they should be Nothing,
-- which becomes null in JSON.
instance ToJSON Measured where
toJSON Measured{..} = toJSON
(measTime, measCpuTime, measCycles, measIters,
Expand Down

0 comments on commit bfa9dbe

Please sign in to comment.