Skip to content

Commit

Permalink
test: Add a benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillespie committed Aug 17, 2023
1 parent 297a936 commit d91b93f
Show file tree
Hide file tree
Showing 9 changed files with 123,745 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ jobs:
run: nix build .#gibberish:test:test
- name: Run static checks
run: nix build .#fourmoluCheck .#hlintCheck
- name: Run benchmarks
run: nix build .#gibberish:bench:main
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ result
.direnv

### Tests
data/tests/gen/*.out.json
data/tests/gen/*.out.json
output.json
31 changes: 31 additions & 0 deletions bench/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Main (main) where

import Data.Gibberish.GenTrigrams (mapTrigrams)
import Data.Gibberish.Types (Digram (), Frequencies ())
import Paths_gibberish (getDataDir)

import Criterion.Main
import Data.Aeson (encode)
import Data.Char (isAlpha)
import Data.Map (Map ())
import Data.Text (Text ())
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
import System.FilePath ((</>))

dictionary :: IO [Text]
dictionary = map Text.toLower . filter (Text.all isAlpha) . Text.lines <$> contents
where
contents = Text.readFile =<< dataDir
dataDir = (</> "data" </> "dicts" </> "wamerican.txt") <$> getDataDir

trigrams :: IO (Map Digram Frequencies)
trigrams = mapTrigrams <$> dictionary

main :: IO ()
main =
defaultMain
[ bench "load dictionary file" $ nfIO dictionary,
env dictionary $ bench "generate trigrams" . nf mapTrigrams,
env trigrams $ bench "serialize to JSON" . nf encode
]
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ packages: ./*.cabal
index-state: 2023-07-18T15:00:46Z

tests: True
test-show-details: direct
test-show-details: direct
Loading

0 comments on commit d91b93f

Please sign in to comment.