Skip to content

Commit

Permalink
Let bench:db work using withConnectionPool
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Feb 26, 2021
1 parent e97ef4b commit e497dfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/core/cardano-wallet-core.cabal
Expand Up @@ -393,6 +393,7 @@ benchmark db
, cardano-crypto
, cardano-wallet-core
, cardano-wallet-launcher
, cardano-wallet-test-utils
, containers
, criterion
, cryptonite
Expand All @@ -403,7 +404,6 @@ benchmark db
, iohk-monitoring
, memory
, random
, temporary
, text
, text-class
, time
Expand Down
39 changes: 27 additions & 12 deletions lib/core/test/bench/db/Main.hs
Expand Up @@ -57,9 +57,8 @@ import Cardano.DB.Sqlite
( ConnectionPool
, DBLog
, SqliteContext (..)
, destroyConnectionPool
, newConnectionPool
, newSqliteContext
, withConnectionPool
)
import Cardano.Mnemonic
( EntropySize, SomeMnemonic (..), entropyToMnemonic, genEntropy )
Expand Down Expand Up @@ -150,7 +149,7 @@ import Cardano.Wallet.Primitive.Types.UTxO
import Cardano.Wallet.Unsafe
( someDummyMnemonic, unsafeRunExceptT )
import Control.DeepSeq
( NFData (..), force )
( NFData (..), deepseq, force )
import Control.Monad
( join )
import Control.Monad.Trans.Except
Expand Down Expand Up @@ -206,6 +205,8 @@ import System.IO.Unsafe
( unsafePerformIO )
import System.Random
( mkStdGen, randoms )
import Test.Utils.Resource
( unBracket )
import UnliftIO.Exception
( bracket, throwIO )

Expand Down Expand Up @@ -658,7 +659,20 @@ withDB
=> Tracer IO DBLog
-> (DBLayer IO s k -> Benchmark)
-> Benchmark
withDB tr bm = envWithCleanup (setupDB tr) cleanupDB (\ ~(_, _, db) -> bm db)
withDB tr bm = envWithCleanup (setupDB tr) cleanupDB (\(BenchEnv _ _ _ db) -> bm db)

data BenchEnv s k = BenchEnv
{ _connectionPool :: !ConnectionPool
, _destroyPool :: IO ()
, _ctx :: !SqliteContext
, _dbLayer :: !(DBLayer IO s k)
}

instance NFData (BenchEnv s k) where
rnf (BenchEnv p _ ctx db) =
deepseq (rnf p) $
deepseq (rnf ctx) $
deepseq (rnf db) ()

setupDB
:: forall s k.
Expand All @@ -667,13 +681,14 @@ setupDB
, WalletKey k
)
=> Tracer IO DBLog
-> IO (ConnectionPool, SqliteContext, DBLayer IO s k)
-> IO (BenchEnv s k)
setupDB tr = do
f <- emptySystemTempFile "bench.db"
pool <- newConnectionPool tr f
(createPool, destroyPool) <- unBracket (withConnectionPool tr f)
pool <- createPool
ctx <- either throwIO pure =<< newSqliteContext tr pool [] migrateAll f
db <- newDBLayerWith NoCache singleEraInterpreter ctx
pure (pool, ctx, db)
pure $ BenchEnv pool destroyPool ctx db

singleEraInterpreter :: TimeInterpreter IO
singleEraInterpreter = hoistTimeInterpreter (pure . runIdentity) $
Expand All @@ -686,9 +701,9 @@ singleEraInterpreter = hoistTimeInterpreter (pure . runIdentity) $
, getSecurityParameter = Quantity 2160
})

cleanupDB :: (ConnectionPool, SqliteContext, DBLayer IO s k) -> IO ()
cleanupDB (pool, SqliteContext{dbFile}, _) = do
destroyConnectionPool pool
cleanupDB :: BenchEnv s k -> IO ()
cleanupDB (BenchEnv _ destroyPool SqliteContext{dbFile} _) = do
destroyPool
let f = fromMaybe ":memory:" dbFile
mapM_ remove [f, f <> "-shm", f <> "-wal"]
where
Expand Down Expand Up @@ -774,11 +789,11 @@ txHistoryDiskSpaceTests tr = do

benchDiskSize :: Tracer IO DBLog -> (DBLayerBench -> IO ()) -> IO ()
benchDiskSize tr action = bracket (setupDB tr) cleanupDB
$ \(pool, SqliteContext{dbFile}, db) -> do
$ \(BenchEnv _ destroyPool SqliteContext{dbFile} db) -> do
let f = fromMaybe ":memory:" dbFile
action db
mapM_ (printFileSize "") [f, f <> "-shm", f <> "-wal"]
destroyConnectionPool pool
destroyPool
printFileSize " (closed)" f
putStrLn ""
where
Expand Down

0 comments on commit e497dfe

Please sign in to comment.