Skip to content

Add benchmarking #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions data/bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Drop *.json files here for benchmarking
5 changes: 5 additions & 0 deletions hw-json.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cabal-install() {

cabal-build() {
cabal v2-build \
--enable-benchmarks \
--enable-tests \
--write-ghc-environment-files=ghc8.4.4+ \
$CABAL_FLAGS "$@"
Expand Down