Debian Codegraph @

benchmarks/loading/Benchmark.hs

raw
1 {-# LANGUAGE RecordWildCards #-}
2
3 import Benchmark.Prelude
4 import Criterion.Main
5 import qualified Data.Acid as Acid
6 import qualified Data.Acid.Memory as Memory
7 import qualified Benchmark.FileSystem as FS
8 import qualified Benchmark.Model as Model; import Benchmark.Model (Model)
9 import qualified System.Random as Random
10
11
12 main :: IO ()
13 main = defaultMain [benchmark defaultBenchmarkInterfaces [100,200,300,400]]
14
15 benchmark :: [BenchmarkInterface] -> [Int] -> Benchmark
16 benchmark bis sizes = env setupWorkingPath $ \ workingPath ->
17 bgroup "" $ map (benchmarksGroup bis workingPath) sizes
18
19
20 -- | An acid-state interface to be benchmarked.
21 data BenchmarkInterface = forall m .
22 BenchmarkInterface
23 { benchName :: String
24 -- ^ Name of the interface, for use in constructing benchmarks.
25 , benchPersist :: Bool
26 -- ^ Does this interface actually persist data to disk? If
27 -- it doesn't, some benchmarks are not applicable.
28 , benchOpen :: FS.FilePath -> IO (Acid.AcidState m)
29 -- ^ Open an acid-state component with the given path. Note
30 -- that the type of the state is encapsulated within
31 -- 'BenchmarkInterface'.
32 , benchUpdate :: Acid.AcidState m -> [[Int]] -> IO ()
33 -- ^ Execute an 'insert' update against the acid-state.
34 , benchQuery :: Acid.AcidState m -> IO Int
35 -- ^ Execute a 'sumUp' query against the state.
36 }
37
38 memoryBenchmarkInterface :: BenchmarkInterface
39 memoryBenchmarkInterface =
40 BenchmarkInterface { benchName = "Memory"
41 , benchPersist = False
42 , benchOpen = const $ Memory.openMemoryState mempty
43 , benchUpdate = \ inst v -> Acid.update inst (Model.Insert v)
44 , benchQuery = \ inst -> Acid.query inst Model.SumUp
45 }
46
47 localBenchmarkInterface :: BenchmarkInterface
48 localBenchmarkInterface =
49 BenchmarkInterface { benchName = "Local"
50 , benchPersist = True
51 , benchOpen = \ p -> Acid.openLocalStateFrom (FS.encodeString p) mempty
52 , benchUpdate = \ inst v -> Acid.update inst (Model.Insert v)
53 , benchQuery = \ inst -> Acid.query inst Model.SumUp
54 }
55
56 defaultBenchmarkInterfaces :: [BenchmarkInterface]
57 defaultBenchmarkInterfaces = [memoryBenchmarkInterface, localBenchmarkInterface]
58
59
60 setupWorkingPath :: IO FS.FilePath
61 setupWorkingPath = do
62 workingPath <- do workingPath <- FS.getTemporaryDirectory
63 rndStr <- replicateM 16 $ Random.randomRIO ('a', 'z')
64 return $ workingPath <> "acid-state" <> "benchmarks" <> "loading" <> FS.decodeString rndStr
65 putStrLn $ "Working under the following temporary directory: " ++ FS.encodeString workingPath
66 FS.removeTreeIfExists workingPath
67 FS.createTree workingPath
68 return workingPath
69
70
71 benchmarksGroup :: [BenchmarkInterface] -> FS.FilePath -> Int -> Benchmark
72 benchmarksGroup bis workingPath size =
73 bgroup (show size)
74 [ bgroup (benchName bi) $
75 initializeBenchmarksGroup bi workingPath' size
76 : if benchPersist bi then [openCloseBenchmarksGroup bi workingPath' size] else []
77 | bi <- bis
78 ]
79 where
80 workingPath' = workingPath <> FS.decodeString (show size)
81
82
83 -- | The Initialize benchmarks measure how long it takes to open an
84 -- empty 'AcidState' component, call 'initialize' to populate it with
85 -- data, and optionally checkpoint before closing.
86 initializeBenchmarksGroup :: BenchmarkInterface -> FS.FilePath -> Int -> Benchmark
87 initializeBenchmarksGroup bi workingPath size =
88 bgroup "Initialize"
89 [ bench "Without checkpoint" $ perRunEnv (prepareInitialize bi workingPath) $ \ _ ->
90 initializeClose bi workingPath size
91 , bench "With checkpoint" $ perRunEnv (prepareInitialize bi workingPath) $ \ _ ->
92 initializeCheckpointClose bi workingPath size
93 ]
94
95 prepareInitialize :: BenchmarkInterface -> FS.FilePath -> IO ()
96 prepareInitialize bi workingPath =
97 when (benchPersist bi) $ do FS.removeTreeIfExists workingPath
98 FS.createTree workingPath
99
100
101 -- | The OpenClose benchmarks measure how long it takes to open an
102 -- existing on-disk 'AcidState' component (either from a checkpoint or
103 -- from a transaction log), optionally execute a query over the entire
104 -- state, then close. These benchmarks are not applicable if the
105 -- interface being benchmarked does not persist data.
106 openCloseBenchmarksGroup :: BenchmarkInterface -> FS.FilePath -> Int -> Benchmark
107 openCloseBenchmarksGroup bi workingPath size =
108 env (prepareOpenCloseBenchmarksGroup bi workingPath size) $ \ ~(logsInstancePath, checkpointInstancePath) -> bgroup "OpenClose"
109 [
110 bench "From Logs" $ nfIO $
111 openClose bi logsInstancePath
112 , bench "From Checkpoint" $ nfIO $
113 openClose bi checkpointInstancePath
114 , bench "From Logs (with query)" $ nfIO $
115 openQueryClose bi logsInstancePath
116 , bench "From Checkpoint (with query)" $ nfIO $
117 openQueryClose bi checkpointInstancePath
118 ]
119
120 -- | Set up data on disk for the open/close benchmarks. This
121 -- initializes an instance, creates a copy of it (for restoring from
122 -- transaction logs), then checkpoints.
123 prepareOpenCloseBenchmarksGroup :: BenchmarkInterface -> FS.FilePath -> Int -> IO (FS.FilePath, FS.FilePath)
124 prepareOpenCloseBenchmarksGroup bi workingPath size = do
125 putStrLn $ "Preparing instances for size " ++ show size
126
127 let
128 logsInstancePath = workingPath <> "logs-instance"
129 checkpointInstancePath = workingPath <> "checkpoint-instance"
130
131 FS.createTree logsInstancePath
132 FS.createTree checkpointInstancePath
133
134 putStrLn "Initializing"
135 initialize bi checkpointInstancePath size $ \inst -> do
136
137 putStrLn "Copying"
138 FS.copy checkpointInstancePath logsInstancePath
139 FS.removeFile $ logsInstancePath <> "open.lock"
140
141 putStrLn "Checkpointing"
142 Acid.createCheckpoint inst
143
144 putStrLn "Closing"
145 Acid.closeAcidState inst
146
147 return (logsInstancePath, checkpointInstancePath)
148
149
150 initialize :: BenchmarkInterface -> FS.FilePath -> Int -> (forall m . Acid.AcidState m -> IO r) -> IO r
151 initialize BenchmarkInterface{..} p size k = do
152 inst <- benchOpen p
153 let values = replicate size $ replicate 100 $ replicate 100 1
154 mapM_ (benchUpdate inst) values
155 k inst
156
157 initializeClose :: BenchmarkInterface -> FS.FilePath -> Int -> IO ()
158 initializeClose bi p size = initialize bi p size Acid.closeAcidState
159
160 initializeCheckpointClose :: BenchmarkInterface -> FS.FilePath -> Int -> IO ()
161 initializeCheckpointClose bi p size =
162 initialize bi p size $ \ inst -> do
163 Acid.createCheckpoint inst
164 Acid.closeAcidState inst
165
166
167 openClose :: BenchmarkInterface -> FS.FilePath -> IO ()
168 openClose BenchmarkInterface{..} p = benchOpen p >>= Acid.closeAcidState
169
170 openQueryClose :: BenchmarkInterface -> FS.FilePath -> IO Int
171 openQueryClose BenchmarkInterface{..} p = do
172 inst <- benchOpen p
173 n <- benchQuery inst
174 Acid.closeAcidState inst
175 return n