|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | + |
| 3 | +module Bench.Database.LSMTree.Internal.Index (benchmarks) where |
| 4 | + |
| 5 | +import Control.Category ((>>>)) |
| 6 | +import Control.DeepSeq (rnf) |
| 7 | +import Control.Monad.ST.Strict (runST) |
| 8 | +import Criterion.Main (Benchmark, Benchmarkable, bench, bgroup, env, |
| 9 | + whnf) |
| 10 | +#if __GLASGOW_HASKELL__ < 910 |
| 11 | +import Data.List (foldl') |
| 12 | +#endif |
| 13 | +import Database.LSMTree.Extras.Generators (getKeyForIndexCompact, |
| 14 | + mkPages, toAppends) |
| 15 | + -- also for @Arbitrary@ instantiation of @SerialisedKey@ |
| 16 | +import Database.LSMTree.Extras.Index (Append, append) |
| 17 | +import Database.LSMTree.Internal.Index (Index, |
| 18 | + IndexType (Compact, Ordinary), newWithDefaults, search, |
| 19 | + unsafeEnd) |
| 20 | +import Database.LSMTree.Internal.Serialise |
| 21 | + (SerialisedKey (SerialisedKey)) |
| 22 | +import Test.QuickCheck (choose, vector) |
| 23 | +import Test.QuickCheck.Gen (Gen (MkGen)) |
| 24 | +import Test.QuickCheck.Random (mkQCGen) |
| 25 | + |
| 26 | +-- * Benchmarks |
| 27 | + |
| 28 | +benchmarks :: Benchmark |
| 29 | +benchmarks = bgroup "Bench.Database.LSMTree.Internal.Index" $ |
| 30 | + map (uncurry benchmarksForSingleType) $ |
| 31 | + [("Compact", Compact), ("Ordinary", Ordinary)] |
| 32 | + where |
| 33 | + |
| 34 | + benchmarksForSingleType :: String -> IndexType -> Benchmark |
| 35 | + benchmarksForSingleType indexTypeName indexType |
| 36 | + = bgroup (indexTypeName ++ " index") $ |
| 37 | + [ |
| 38 | + -- Search |
| 39 | + env (return $ searchIndex indexType 10000) $ \ index -> |
| 40 | + env (return $ searchKeys 1000) $ \ keys -> |
| 41 | + bench "Search" $ |
| 42 | + searchBenchmarkable index keys, |
| 43 | + |
| 44 | + -- Incremental construction |
| 45 | + env (return $ incrementalConstructionAppends 10000) $ \ appends -> |
| 46 | + bench "Incremental construction" $ |
| 47 | + incrementalConstructionBenchmarkable indexType appends |
| 48 | + ] |
| 49 | + |
| 50 | +-- * Utilities |
| 51 | + |
| 52 | +-- | Deterministically constructs a value using a QuickCheck generator. |
| 53 | +generated :: Gen a -> a |
| 54 | +generated (MkGen exec) = exec (mkQCGen 411) 30 |
| 55 | + |
| 56 | +{-| |
| 57 | + Constructs serialised keys that conform to the key size constraint of |
| 58 | + compact indexes. |
| 59 | +-} |
| 60 | +keysForIndexCompact :: Int -- ^ Number of keys |
| 61 | + -> [SerialisedKey] -- ^ Constructed keys |
| 62 | +keysForIndexCompact = vector >>> |
| 63 | + generated >>> |
| 64 | + map (getKeyForIndexCompact >>> SerialisedKey) |
| 65 | + |
| 66 | +{-| |
| 67 | + Constructs append operations whose serialised keys conform to the key size |
| 68 | + constraint of compact indexes. |
| 69 | +-} |
| 70 | +appendsForIndexCompact :: Int -- ^ Number of keys used in the construction |
| 71 | + -> [Append] -- ^ Constructed append operations |
| 72 | +appendsForIndexCompact = keysForIndexCompact >>> |
| 73 | + mkPages 0.03 (choose (0, 16)) 0.01 >>> |
| 74 | + generated >>> |
| 75 | + toAppends |
| 76 | +{- |
| 77 | + The arguments used for 'mkPages' are the same as the ones used for |
| 78 | + 'genPages' in the instantiation of 'Arbitrary' for 'LogicalPageSummaries' at |
| 79 | + the time of writing. |
| 80 | +-} |
| 81 | + |
| 82 | +{-| |
| 83 | + Constructs an index by applying append operations to an initially empty |
| 84 | + index. |
| 85 | +-} |
| 86 | +indexFromAppends :: IndexType -> [Append] -> Index |
| 87 | +indexFromAppends indexType appends = runST $ do |
| 88 | + indexAcc <- newWithDefaults indexType |
| 89 | + mapM_ (flip append indexAcc) appends |
| 90 | + snd <$> unsafeEnd indexAcc |
| 91 | + |
| 92 | +-- * Benchmark ingredients |
| 93 | + |
| 94 | +-- ** Search |
| 95 | + |
| 96 | +-- | Constructs an index to be searched. |
| 97 | +searchIndex :: IndexType -- ^ Type of index to construct |
| 98 | + -> Int -- ^ Number of keys used in the construction |
| 99 | + -> Index -- ^ Constructed index |
| 100 | +searchIndex indexType keyCount |
| 101 | + = indexFromAppends indexType (appendsForIndexCompact keyCount) |
| 102 | + |
| 103 | +-- | Constructs a list of keys to search for. |
| 104 | +searchKeys :: Int -- ^ Number of searches |
| 105 | + -> [SerialisedKey] -- ^ Constructed search keys |
| 106 | +searchKeys = keysForIndexCompact |
| 107 | + |
| 108 | +-- | The action to be performed by a search benchmark. |
| 109 | +searchBenchmarkable :: Index -> [SerialisedKey] -> Benchmarkable |
| 110 | +searchBenchmarkable index = whnf $ foldl' (\ _ key -> rnf (search key index)) () |
| 111 | + |
| 112 | +-- ** Incremental construction |
| 113 | + |
| 114 | +-- | Constructs append operations to be used in index construction. |
| 115 | +incrementalConstructionAppends |
| 116 | + :: Int -- ^ Number of keys used in the construction |
| 117 | + -> [Append] -- ^ Constructed append operations |
| 118 | +incrementalConstructionAppends = appendsForIndexCompact |
| 119 | + |
| 120 | +-- | The action to be performed by an incremental-construction benchmark. |
| 121 | +incrementalConstructionBenchmarkable :: IndexType -> [Append] -> Benchmarkable |
| 122 | +incrementalConstructionBenchmarkable indexType appends |
| 123 | + = whnf (indexFromAppends indexType) appends |
0 commit comments