2017-02-04 07:30:54 +03:00
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Data.ByteString.Lazy (toStrict)
|
|
|
|
import Data.ByteString.Builder (toLazyByteString)
|
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Data.Vector as V(fromList, empty)
|
2017-02-13 19:46:14 +03:00
|
|
|
import Data.IORef
|
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Applicative
|
|
|
|
import Control.Monad
|
|
|
|
import Data.Monoid
|
2017-02-04 07:30:54 +03:00
|
|
|
|
|
|
|
import Database.PostgreSQL.Protocol.Types
|
|
|
|
import Database.PostgreSQL.Protocol.Encoders
|
2017-02-13 19:46:14 +03:00
|
|
|
import Database.PostgreSQL.Driver
|
2017-02-04 07:30:54 +03:00
|
|
|
import Criterion.Main
|
|
|
|
|
2017-02-13 19:46:14 +03:00
|
|
|
main = benchMultiPw
|
|
|
|
|
|
|
|
benchRequests :: IO c -> (c -> IO a) -> IO ()
|
|
|
|
benchRequests connectAction queryAction = do
|
|
|
|
rs <- replicateM 8 newThread
|
|
|
|
threadDelay 10000000
|
|
|
|
traverse (killThread . snd) rs
|
|
|
|
s <- sum <$> traverse (readIORef . fst) rs
|
|
|
|
print $ "Requests: " ++ show s
|
|
|
|
where
|
|
|
|
newThread = do
|
|
|
|
ref <- newIORef 0 :: IO (IORef Word)
|
|
|
|
c <- connectAction
|
|
|
|
tid <- forkIO $ forever $ do
|
|
|
|
queryAction c
|
|
|
|
modifyIORef' ref (+1)
|
|
|
|
pure (ref, tid)
|
|
|
|
|
|
|
|
benchMultiPw :: IO ()
|
|
|
|
benchMultiPw = benchRequests createConnection $ \c -> do
|
2017-02-14 00:11:27 +03:00
|
|
|
sendBatchAndSync c [q, q, q, q, q, q, q, q, q, q]
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
|
|
|
readNextData c
|
2017-02-13 19:46:14 +03:00
|
|
|
readNextData c
|
|
|
|
waitReadyForQuery c
|
2017-02-04 07:30:54 +03:00
|
|
|
where
|
2017-02-13 19:46:14 +03:00
|
|
|
q = Query largeStmt V.empty Binary Binary AlwaysCache
|
|
|
|
largeStmt = "select typname, typnamespace, typowner, typlen, typbyval,"
|
|
|
|
<> "typcategory, typispreferred, typisdefined, typdelim,"
|
|
|
|
<> "typrelid, typelem, typarray from pg_type "
|
|
|
|
|
|
|
|
-- Connection
|
|
|
|
-- | Creates connection with default filter.
|
|
|
|
createConnection :: IO Connection
|
|
|
|
createConnection = getConnection <$> connect defaultSettings
|
|
|
|
|
|
|
|
getConnection :: Either Error Connection -> Connection
|
|
|
|
getConnection (Left e) = error $ "Connection error " ++ show e
|
|
|
|
getConnection (Right c) = c
|
|
|
|
|
|
|
|
defaultSettings = defaultConnectionSettings
|
|
|
|
{ settingsHost = "localhost"
|
|
|
|
, settingsDatabase = "travis_test"
|
|
|
|
, settingsUser = "postgres"
|
|
|
|
, settingsPassword = ""
|
|
|
|
}
|
2017-02-04 07:30:54 +03:00
|
|
|
|