2017-04-10 17:55:58 +03:00
|
|
|
module Main where
|
|
|
|
|
2024-01-27 00:12:18 +03:00
|
|
|
import Criterion
|
|
|
|
import Criterion.Main
|
2024-04-19 07:38:30 +03:00
|
|
|
import Hasql.Connection qualified as A
|
|
|
|
import Hasql.Decoders qualified as D
|
|
|
|
import Hasql.Session qualified as B
|
|
|
|
import Hasql.Statement qualified as C
|
2022-06-20 13:54:54 +03:00
|
|
|
import Prelude
|
2017-04-10 17:55:58 +03:00
|
|
|
|
2024-01-27 00:14:28 +03:00
|
|
|
main :: IO ()
|
2017-04-10 17:55:58 +03:00
|
|
|
main =
|
|
|
|
do
|
|
|
|
Right connection <- acquireConnection
|
|
|
|
useConnection connection
|
|
|
|
where
|
|
|
|
acquireConnection =
|
2017-06-06 09:38:02 +03:00
|
|
|
A.acquire ""
|
2017-04-10 17:55:58 +03:00
|
|
|
useConnection connection =
|
|
|
|
defaultMain
|
2022-06-20 13:54:54 +03:00
|
|
|
[ sessionBench "largeResultInVector" sessionWithSingleLargeResultInVector,
|
|
|
|
sessionBench "largeResultInList" sessionWithSingleLargeResultInList,
|
|
|
|
sessionBench "manyLargeResults" sessionWithManyLargeResults,
|
|
|
|
sessionBench "manySmallResults" sessionWithManySmallResults
|
|
|
|
]
|
2017-04-10 17:55:58 +03:00
|
|
|
where
|
2023-10-13 02:24:12 +03:00
|
|
|
sessionBench :: (NFData a) => String -> B.Session a -> Benchmark
|
2017-04-10 17:55:58 +03:00
|
|
|
sessionBench name session =
|
2021-03-23 15:06:42 +03:00
|
|
|
bench name (nfIO (fmap (either (error "") id) (B.run session connection)))
|
2017-04-10 17:55:58 +03:00
|
|
|
|
|
|
|
-- * Sessions
|
2022-06-20 13:54:54 +03:00
|
|
|
|
2017-04-10 17:55:58 +03:00
|
|
|
sessionWithManySmallParameters :: Vector (Int64, Int64) -> B.Session ()
|
|
|
|
sessionWithManySmallParameters =
|
2021-03-23 16:16:06 +03:00
|
|
|
error "TODO: sessionWithManySmallParameters"
|
2017-04-10 17:55:58 +03:00
|
|
|
|
|
|
|
sessionWithSingleLargeResultInVector :: B.Session (Vector (Int64, Int64))
|
|
|
|
sessionWithSingleLargeResultInVector =
|
2018-05-23 13:33:34 +03:00
|
|
|
B.statement () statementWithManyRowsInVector
|
2017-04-10 17:55:58 +03:00
|
|
|
|
2017-10-09 18:42:22 +03:00
|
|
|
sessionWithManyLargeResults :: B.Session [Vector (Int64, Int64)]
|
|
|
|
sessionWithManyLargeResults =
|
2018-05-23 13:33:34 +03:00
|
|
|
replicateM 1000 (B.statement () statementWithManyRowsInVector)
|
2017-10-09 18:42:22 +03:00
|
|
|
|
2020-03-21 20:48:16 +03:00
|
|
|
sessionWithSingleLargeResultInList :: B.Session [(Int64, Int64)]
|
2017-04-10 17:55:58 +03:00
|
|
|
sessionWithSingleLargeResultInList =
|
2018-05-23 13:33:34 +03:00
|
|
|
B.statement () statementWithManyRowsInList
|
2017-04-10 17:55:58 +03:00
|
|
|
|
2017-10-09 18:42:22 +03:00
|
|
|
sessionWithManySmallResults :: B.Session [(Int64, Int64)]
|
2017-04-10 17:55:58 +03:00
|
|
|
sessionWithManySmallResults =
|
2018-05-23 13:33:34 +03:00
|
|
|
replicateM 1000 (B.statement () statementWithSingleRow)
|
2017-04-10 17:55:58 +03:00
|
|
|
|
|
|
|
-- * Statements
|
2022-06-20 13:54:54 +03:00
|
|
|
|
2018-05-23 13:33:34 +03:00
|
|
|
statementWithManyParameters :: C.Statement (Vector (Int64, Int64)) ()
|
|
|
|
statementWithManyParameters =
|
2021-03-23 16:16:06 +03:00
|
|
|
error "TODO: statementWithManyParameters"
|
2017-04-10 17:55:58 +03:00
|
|
|
|
2018-05-23 13:33:34 +03:00
|
|
|
statementWithSingleRow :: C.Statement () (Int64, Int64)
|
|
|
|
statementWithSingleRow =
|
|
|
|
C.Statement template encoder decoder True
|
2017-04-10 17:55:58 +03:00
|
|
|
where
|
|
|
|
template =
|
|
|
|
"SELECT 1, 2"
|
|
|
|
encoder =
|
|
|
|
conquer
|
|
|
|
decoder =
|
|
|
|
D.singleRow row
|
|
|
|
where
|
|
|
|
row =
|
2019-05-21 01:20:57 +03:00
|
|
|
tuple <$> (D.column . D.nonNullable) D.int8 <*> (D.column . D.nonNullable) D.int8
|
2017-04-10 17:55:58 +03:00
|
|
|
where
|
|
|
|
tuple !a !b =
|
|
|
|
(a, b)
|
|
|
|
|
2018-05-23 13:33:34 +03:00
|
|
|
statementWithManyRows :: (D.Row (Int64, Int64) -> D.Result result) -> C.Statement () result
|
|
|
|
statementWithManyRows decoder =
|
|
|
|
C.Statement template encoder (decoder rowDecoder) True
|
2017-04-10 17:55:58 +03:00
|
|
|
where
|
|
|
|
template =
|
2017-04-14 00:43:39 +03:00
|
|
|
"SELECT generate_series(0,1000) as a, generate_series(1000,2000) as b"
|
2017-04-10 17:55:58 +03:00
|
|
|
encoder =
|
|
|
|
conquer
|
|
|
|
rowDecoder =
|
2019-05-21 01:20:57 +03:00
|
|
|
tuple <$> (D.column . D.nonNullable) D.int8 <*> (D.column . D.nonNullable) D.int8
|
2017-04-10 17:55:58 +03:00
|
|
|
where
|
|
|
|
tuple !a !b =
|
|
|
|
(a, b)
|
|
|
|
|
2018-05-23 13:33:34 +03:00
|
|
|
statementWithManyRowsInVector :: C.Statement () (Vector (Int64, Int64))
|
|
|
|
statementWithManyRowsInVector =
|
|
|
|
statementWithManyRows D.rowVector
|
2017-04-10 17:55:58 +03:00
|
|
|
|
2020-03-21 20:48:16 +03:00
|
|
|
statementWithManyRowsInList :: C.Statement () [(Int64, Int64)]
|
2018-05-23 13:33:34 +03:00
|
|
|
statementWithManyRowsInList =
|
|
|
|
statementWithManyRows D.rowList
|