diff --git a/bench/Main.hs b/bench/Main.hs index 3435f3d..f9c1148 100644 --- a/bench/Main.hs +++ b/bench/Main.hs @@ -2,9 +2,50 @@ module Main where +import Control.Monad import Criterion.Main +import Data.Text +import HaskellWorks.Data.Json.LightJson +import HaskellWorks.Data.Positioning + +import qualified Data.ByteString as BS +import qualified Data.List as L +import qualified HaskellWorks.Data.ByteString as BS +import qualified HaskellWorks.Data.Json.Standard.Cursor.Fast as JCF +import qualified System.Directory as IO + +countLightJsonEntryNodes :: [(Text, JCF.Cursor)] -> Count +countLightJsonEntryNodes _ = 0 + +countLightJsonNodes :: LightJson JCF.Cursor -> Count +countLightJsonNodes j = case j of + LightJsonString _ -> 1 + LightJsonNumber _ -> 1 + LightJsonObject kvs -> 1 + sum (fmap (countLightJsonNodes . lightJsonAt . snd ) kvs ) + LightJsonArray es -> 1 + sum (fmap (countLightJsonNodes . lightJsonAt ) es ) + LightJsonBool _ -> 1 + LightJsonNull -> 1 + LightJsonError _ -> 1 + +getBenchmarkJsonFiles :: IO [FilePath] +getBenchmarkJsonFiles = do + entries <- IO.listDirectory "data/bench" + return $ L.sort (("data/bench/" ++) <$> (".json" `L.isSuffixOf`) `L.filter` entries) + +runCount :: BS.ByteString -> Count +runCount bs = countLightJsonNodes (lightJsonAt c) + where ibip = JCF.simdToIbBp bs + c = JCF.fromBsIbBp bs ibip + +mkBenchCount :: [FilePath] -> IO Benchmark +mkBenchCount files = do + benchmarks <- forM files $ \file -> return (env (BS.mmap file) $ \bs -> bench file (whnf runCount bs)) + return $ bgroup "Count nodes" benchmarks main :: IO () main = do - benchmarks <- mconcat <$> sequence mempty + benchmarkFiles <- getBenchmarkJsonFiles + benchmarks <- sequence + [ mkBenchCount benchmarkFiles + ] defaultMain benchmarks diff --git a/data/bench/README.md b/data/bench/README.md new file mode 100644 index 0000000..0e6b659 --- /dev/null +++ b/data/bench/README.md @@ -0,0 +1 @@ +Drop *.json files here for benchmarking diff --git a/hw-json.cabal b/hw-json.cabal index 3ea2062..0467d51 100644 --- a/hw-json.cabal +++ b/hw-json.cabal @@ -219,9 +219,14 @@ benchmark bench , bytestring , criterion , directory + , dlist + , generic-lens , hw-json , hw-json-standard-cursor + , hw-prim , mmap + , text + , vector type: exitcode-stdio-1.0 main-is: Main.hs hs-source-dirs: bench diff --git a/project.sh b/project.sh index d3cf215..695cb5a 100755 --- a/project.sh +++ b/project.sh @@ -17,6 +17,7 @@ cabal-install() { cabal-build() { cabal v2-build \ + --enable-benchmarks \ --enable-tests \ --write-ghc-environment-files=ghc8.4.4+ \ $CABAL_FLAGS "$@"