-
Notifications
You must be signed in to change notification settings - Fork 42
Add builtin data structure benchmark framework for booster #4139
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
Draft
Stevengre
wants to merge
3
commits into
master
Choose a base branch
from
benchmark-framework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| {- | | ||
| Re-export benchmark fixture helpers for the benchmark executable. | ||
| -} | ||
| module BenchData ( | ||
| module Booster.Benchmark.Data, | ||
| ) where | ||
|
|
||
| import Booster.Benchmark.Data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| {-# LANGUAGE OverloadedRecordDot #-} | ||
|
|
||
| module Main (main) where | ||
|
|
||
| import BenchData | ||
| import Booster.Benchmark.Ops | ||
| import Booster.Pattern.Base | ||
| import Test.Tasty.Bench | ||
|
|
||
| main :: IO () | ||
| main = | ||
| defaultMain | ||
| [ bgroup "kmap" (map kmapBenchFor benchmarkSizes) | ||
| , bgroup "kset" (map ksetBenchFor benchmarkSizes) | ||
| , bgroup "klist" (map klistBenchFor benchmarkSizes) | ||
| , pipelineBenchmarks | ||
| ] | ||
|
|
||
| kmapBenchFor :: Int -> Benchmark | ||
| kmapBenchFor size = | ||
| let mapTerm = mkMapTerm size | ||
| existingKey = mkLookupExistingKey size | ||
| missingKey = mkLookupMissingKey size | ||
| insertKey = mkInsertKey size | ||
| insertValue = mkUpdatedValue (size + 1) | ||
| updateValue = mkUpdatedValue (size + 2) | ||
| duplicatePairs = case mapTerm of | ||
| KMap _ pairs _ -> pairs <> reverse pairs | ||
| _ -> [] | ||
| coreBenches = | ||
| [ bench "lookup-existing" $ nf (runMapLookup mapTerm) existingKey | ||
| , bench "lookup-missing" $ nf (runMapLookup mapTerm) missingKey | ||
| , bench "size" $ nf runMapSize mapTerm | ||
| ] | ||
| heavyBenches = | ||
| [ bench "insert" $ nf (\(m, k, v) -> runMapUpdate m k v) (mapTerm, insertKey, insertValue) | ||
| , bench "update" $ nf (\(m, k, v) -> runMapUpdate m k v) (mapTerm, existingKey, updateValue) | ||
| , bench "remove" $ nf (\(m, k) -> runMapRemove m k) (mapTerm, existingKey) | ||
| , bench "keys" $ nf runMapKeys mapTerm | ||
| , bench "values" $ nf runMapValues mapTerm | ||
| , bench "in_keys" $ nf (runMapInKeys mapTerm) existingKey | ||
| , bench "sortAndDeduplicate" $ nf (\pairs -> KMap benchmarkKMapDef pairs Nothing) duplicatePairs | ||
| ] | ||
| in bgroup | ||
| ("size-" <> show size) | ||
| (coreBenches <> whenSizeAtMost 10000 size heavyBenches) | ||
|
|
||
| ksetBenchFor :: Int -> Benchmark | ||
| ksetBenchFor size = | ||
| let leftSet = mkSetTerm size | ||
| rightSet = mkSetTerm (max 1 (size `div` 2)) | ||
| probe = mkSetElement (max 1 (size `div` 3)) | ||
| duplicateElements = case leftSet of | ||
| KSet _ elements _ -> elements <> reverse elements | ||
| _ -> [] | ||
| coreBenches = | ||
| [ bench "in" $ nf (ksetIn probe) leftSet | ||
| , bench "size" $ nf ksetSize leftSet | ||
| ] | ||
| heavyBenches = | ||
| [ bench "difference" $ nf (\(l, r) -> ksetDifference l r) (leftSet, rightSet) | ||
| , bench "union" $ nf (\(l, r) -> ksetUnion l r) (leftSet, rightSet) | ||
| , bench "intersection" $ nf (\(l, r) -> ksetIntersection l r) (leftSet, rightSet) | ||
| , bench "sortAndDeduplicate" $ | ||
| nf (\elements -> KSet benchmarkKSetDef elements Nothing) duplicateElements | ||
| ] | ||
| in bgroup | ||
| ("size-" <> show size) | ||
| (coreBenches <> whenSizeAtMost 10000 size heavyBenches) | ||
|
|
||
| klistBenchFor :: Int -> Benchmark | ||
| klistBenchFor size = | ||
| let listTerm = mkListTerm size | ||
| concatRhs = mkListConcatRhs size | ||
| idxMiddle = size `div` 2 | ||
| idxLast = max 0 (size - 1) | ||
| rangeTrim = max 0 (size `div` 4) | ||
| coreBenches = | ||
| [ bench "get-0" $ nf (runListGet listTerm) 0 | ||
| , bench "get-middle" $ nf (runListGet listTerm) idxMiddle | ||
| , bench "get-last" $ nf (runListGet listTerm) idxLast | ||
| , bench "size" $ nf runListSize listTerm | ||
| ] | ||
| heavyBenches = | ||
| [ bench "range" $ nf (\(l, f, b) -> runListRange l f b) (listTerm, rangeTrim, rangeTrim) | ||
| , bench "concat" $ nf (\(l, r) -> runListConcat l r) (listTerm, concatRhs) | ||
| ] | ||
| in bgroup | ||
| ("size-" <> show size) | ||
| (coreBenches <> whenSizeAtMost 10000 size heavyBenches) | ||
|
|
||
| pipelineBenchmarks :: Benchmark | ||
| pipelineBenchmarks = | ||
| bgroup | ||
| "pipeline" | ||
| [ bgroup "kmap-matchMaps" (map mapMatchBenchFor matchMapBenchSizes) | ||
| , bgroup "ord-term" (map ordBenchFor pipelineBenchSizes) | ||
| , bgroup "substitution" (map substitutionBenchFor pipelineBenchSizes) | ||
| , bench "full-single-rule-pipeline" $ nfIO runPipelineOnce | ||
| ] | ||
|
|
||
| matchMapBenchSizes :: [Int] | ||
| matchMapBenchSizes = [10, 100, 1000] | ||
|
|
||
| pipelineBenchSizes :: [Int] | ||
| pipelineBenchSizes = [10, 100, 1000, 5000] | ||
|
|
||
| mapMatchBenchFor :: Int -> Benchmark | ||
| mapMatchBenchFor size = | ||
| let patternMap = mkPatternMapForMatch size | ||
| subjectMap = mkSubjectMapForMatch size | ||
| in bgroup | ||
| ("size-" <> show size) | ||
| [ bench "matchMaps" $ whnf (\(p, s) -> matchMapTerms p s) (patternMap, subjectMap) | ||
| ] | ||
|
|
||
| whenSizeAtMost :: Int -> Int -> [Benchmark] -> [Benchmark] | ||
| whenSizeAtMost limit size benchmarks | ||
| | size <= limit = benchmarks | ||
| | otherwise = [] | ||
|
|
||
| ordBenchFor :: Int -> Benchmark | ||
| ordBenchFor size = | ||
| let left = mkMapTerm size | ||
| right = | ||
| case mkMapTerm size of | ||
| KMap def pairs rest -> | ||
| KMap def ((mkInsertKey (size + 7), mkUpdatedValue (size + 7)) : pairs) rest | ||
| other -> other | ||
| in bgroup | ||
| ("size-" <> show size) | ||
| [ bench "derived" $ whnf (\(a, b) -> compare a b) (left, right) | ||
| , bench "hash-first" $ whnf (\(a, b) -> compareTermHashFirst a b) (left, right) | ||
| ] | ||
|
|
||
| substitutionBenchFor :: Int -> Benchmark | ||
| substitutionBenchFor size = | ||
| let unchangedKeyMap = mkMapWithValueVariables size | ||
| unchangedKeySubst = mkValueSubstitution size | ||
| changedKeyMap = mkMapWithKeyVariables size | ||
| changedKeySubst = mkKeySubstitution size | ||
| in bgroup | ||
| ("size-" <> show size) | ||
| [ bench "unchanged-keys" $ | ||
| nf (\(subst, term) -> substituteMap subst term) (unchangedKeySubst, unchangedKeyMap) | ||
| , bench "changed-keys" $ | ||
| nf (\(subst, term) -> substituteMap subst term) (changedKeySubst, changedKeyMap) | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of these tests are measuring the generation of the large test data structures (such as
mapTerm) as well as the operation they are trying to measure. It might be better to evaluate the data structure in full beforehand (usingforce) - which may require aNFDatainstance.See the long comment on the
nffunction inTest.Tasty.Benchfor more information.