2019-10-29 19:16:15 +00:00
|
|
|
{- git-annex database benchmarks
|
|
|
|
-
|
|
|
|
- Copyright 2016-2019 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Database.Benchmark (benchmarkDbs) where
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Types.Benchmark
|
|
|
|
#ifdef WITH_BENCHMARK
|
|
|
|
import qualified Database.Keys.SQL as SQL
|
|
|
|
import qualified Database.Queue as H
|
|
|
|
import Database.Init
|
|
|
|
import Utility.Tmp.Dir
|
|
|
|
import Git.FilePath
|
|
|
|
import Types.Key
|
2019-11-21 21:25:20 +00:00
|
|
|
import Utility.DataUnits
|
2019-10-29 19:16:15 +00:00
|
|
|
|
|
|
|
import Criterion.Main
|
|
|
|
import qualified Data.ByteString.Char8 as B8
|
|
|
|
import System.Random
|
2019-11-22 17:20:22 +00:00
|
|
|
import Control.Concurrent
|
2020-11-05 22:45:37 +00:00
|
|
|
import qualified System.FilePath.ByteString as P
|
2019-10-29 19:16:15 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-21 21:25:20 +00:00
|
|
|
benchmarkDbs :: CriterionMode -> Integer -> Annex ()
|
2019-10-29 19:16:15 +00:00
|
|
|
#ifdef WITH_BENCHMARK
|
2019-11-21 21:25:20 +00:00
|
|
|
benchmarkDbs mode n = withTmpDirIn "." "benchmark" $ \tmpdir -> do
|
2020-11-05 22:45:37 +00:00
|
|
|
db <- benchDb (toRawFilePath tmpdir) n
|
2019-10-29 19:16:15 +00:00
|
|
|
liftIO $ runMode mode
|
2019-11-21 21:25:20 +00:00
|
|
|
[ bgroup "keys database"
|
2019-10-29 19:16:15 +00:00
|
|
|
[ getAssociatedFilesHitBench db
|
|
|
|
, getAssociatedFilesMissBench db
|
|
|
|
, getAssociatedKeyHitBench db
|
|
|
|
, getAssociatedKeyMissBench db
|
|
|
|
, addAssociatedFileOldBench db
|
|
|
|
, addAssociatedFileNewBench db
|
|
|
|
]
|
|
|
|
]
|
|
|
|
#else
|
|
|
|
benchmarkDbs _ = error "not built with criterion, cannot benchmark"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_BENCHMARK
|
|
|
|
|
|
|
|
getAssociatedFilesHitBench :: BenchDb -> Benchmark
|
2019-11-22 17:20:22 +00:00
|
|
|
getAssociatedFilesHitBench (BenchDb h num _) = bench ("getAssociatedFiles (hit)") $ nfIO $ do
|
2019-10-29 19:16:15 +00:00
|
|
|
n <- getStdRandom (randomR (1,num))
|
2019-10-29 16:28:01 +00:00
|
|
|
SQL.getAssociatedFiles (keyN n) (SQL.ReadHandle h)
|
2019-10-29 19:16:15 +00:00
|
|
|
|
|
|
|
getAssociatedFilesMissBench :: BenchDb -> Benchmark
|
2019-11-22 17:20:22 +00:00
|
|
|
getAssociatedFilesMissBench (BenchDb h _ _) = bench ("getAssociatedFiles (miss)") $ nfIO $
|
2019-10-29 16:28:01 +00:00
|
|
|
SQL.getAssociatedFiles keyMiss (SQL.ReadHandle h)
|
2019-10-29 19:16:15 +00:00
|
|
|
|
|
|
|
getAssociatedKeyHitBench :: BenchDb -> Benchmark
|
2019-11-22 17:20:22 +00:00
|
|
|
getAssociatedKeyHitBench (BenchDb h num _) = bench ("getAssociatedKey (hit)") $ nfIO $ do
|
2019-10-29 19:16:15 +00:00
|
|
|
n <- getStdRandom (randomR (1,num))
|
2019-10-29 19:16:15 +00:00
|
|
|
SQL.getAssociatedKey (fileN n) (SQL.ReadHandle h)
|
2019-10-29 19:16:15 +00:00
|
|
|
|
|
|
|
getAssociatedKeyMissBench :: BenchDb -> Benchmark
|
2019-11-22 17:20:22 +00:00
|
|
|
getAssociatedKeyMissBench (BenchDb h _ _) = bench ("getAssociatedKey (miss)") $ nfIO $
|
2019-10-29 19:16:15 +00:00
|
|
|
SQL.getAssociatedKey fileMiss (SQL.ReadHandle h)
|
2019-10-29 19:16:15 +00:00
|
|
|
|
|
|
|
addAssociatedFileOldBench :: BenchDb -> Benchmark
|
2019-11-22 17:20:22 +00:00
|
|
|
addAssociatedFileOldBench (BenchDb h num _) = bench ("addAssociatedFile to (old)") $ nfIO $ do
|
2019-10-29 19:16:15 +00:00
|
|
|
n <- getStdRandom (randomR (1,num))
|
2019-10-29 16:28:01 +00:00
|
|
|
SQL.addAssociatedFile (keyN n) (fileN n) (SQL.WriteHandle h)
|
2019-10-29 19:16:15 +00:00
|
|
|
H.flushDbQueue h
|
|
|
|
|
|
|
|
addAssociatedFileNewBench :: BenchDb -> Benchmark
|
2019-11-22 17:20:22 +00:00
|
|
|
addAssociatedFileNewBench (BenchDb h num mv) = bench ("addAssociatedFile to (new)") $ nfIO $ do
|
|
|
|
n <- takeMVar mv
|
|
|
|
putMVar mv (n+1)
|
2019-11-21 21:26:50 +00:00
|
|
|
SQL.addAssociatedFile (keyN n) (fileN (num+n)) (SQL.WriteHandle h)
|
2019-10-29 19:16:15 +00:00
|
|
|
H.flushDbQueue h
|
|
|
|
|
2019-11-21 21:25:20 +00:00
|
|
|
populateAssociatedFiles :: H.DbQueue -> Integer -> IO ()
|
2019-10-29 19:16:15 +00:00
|
|
|
populateAssociatedFiles h num = do
|
|
|
|
forM_ [1..num] $ \n ->
|
2019-10-29 16:28:01 +00:00
|
|
|
SQL.addAssociatedFile (keyN n) (fileN n) (SQL.WriteHandle h)
|
2019-10-29 19:16:15 +00:00
|
|
|
H.flushDbQueue h
|
|
|
|
|
2019-11-21 21:25:20 +00:00
|
|
|
keyN :: Integer -> Key
|
2019-11-22 20:24:04 +00:00
|
|
|
keyN n = mkKey $ \k -> k
|
2019-10-29 19:16:15 +00:00
|
|
|
{ keyName = B8.pack $ "key" ++ show n
|
|
|
|
, keyVariety = OtherKey "BENCH"
|
|
|
|
}
|
|
|
|
|
2019-11-21 21:25:20 +00:00
|
|
|
fileN :: Integer -> TopFilePath
|
2020-10-02 17:30:24 +00:00
|
|
|
fileN n = asTopFilePath (toRawFilePath ("file" ++ show n))
|
2019-10-29 19:16:15 +00:00
|
|
|
|
|
|
|
keyMiss :: Key
|
|
|
|
keyMiss = keyN 0 -- 0 is never stored
|
|
|
|
|
|
|
|
fileMiss :: TopFilePath
|
|
|
|
fileMiss = fileN 0 -- 0 is never stored
|
|
|
|
|
2019-11-22 17:20:22 +00:00
|
|
|
data BenchDb = BenchDb H.DbQueue Integer (MVar Integer)
|
2019-10-29 19:16:15 +00:00
|
|
|
|
2020-11-05 22:45:37 +00:00
|
|
|
benchDb :: RawFilePath -> Integer -> Annex BenchDb
|
2019-10-29 19:16:15 +00:00
|
|
|
benchDb tmpdir num = do
|
2019-11-21 21:25:20 +00:00
|
|
|
liftIO $ putStrLn $ "setting up database with " ++ show num ++ " items"
|
2019-10-29 19:16:15 +00:00
|
|
|
initDb db SQL.createTables
|
|
|
|
h <- liftIO $ H.openDbQueue H.MultiWriter db SQL.containedTable
|
|
|
|
liftIO $ populateAssociatedFiles h num
|
2020-11-05 22:45:37 +00:00
|
|
|
sz <- liftIO $ getFileSize db
|
2019-11-21 21:25:20 +00:00
|
|
|
liftIO $ putStrLn $ "size of database on disk: " ++
|
|
|
|
roughSize storageUnits False sz
|
2019-11-22 17:20:22 +00:00
|
|
|
mv <- liftIO $ newMVar 1
|
|
|
|
return (BenchDb h num mv)
|
2019-10-29 19:16:15 +00:00
|
|
|
where
|
2020-11-05 22:45:37 +00:00
|
|
|
db = tmpdir P.</> toRawFilePath (show num </> "db")
|
2019-10-29 19:16:15 +00:00
|
|
|
|
|
|
|
#endif /* WITH_BENCHMARK */
|