10000 Benchmarking latency · postgres-haskell/postgres-wire@ec08234 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec08234

Browse files
Benchmarking latency
1 parent 6f602ec commit ec08234

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

bench/Bench.hs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Data.ByteString.Builder (toLazyByteString)
88
import Data.ByteString (ByteString)
99
import Data.Vector as V(fromList, empty)
1010
import Data.IORef
11+
import Data.Int
1112
import Data.Foldable
1213
import Data.Maybe
1314
import Control.Concurrent
@@ -16,6 +17,7 @@ import Control.Monad
1617
import Data.Monoid
1718
import Control.DeepSeq
1819
import System.IO.Unsafe
20+
import System.Clock
1921

2022
import qualified Database.PostgreSQL.LibPQ as LibPQ
2123

@@ -91,17 +93,26 @@ benchRequests :: IO c -> (c -> IO a) -> IO ()
9193
benchRequests connectAction queryAction = do
9294
rs <- replicateM 8 newThread
9395
threadDelay 10000000
94-
traverse (killThread . snd) rs
95-
s <- sum <$> traverse (readIORef . fst) rs
96+
traverse (\(_,_, tid) -> killThread tid) rs
97+
s <- sum <$> traverse (\(ref, _, _) -> readIORef ref) rs
98+
latency_total <- sum <$> traverse (\(_, ref, _) -> readIORef ref) rs
9699
print $ "Requests: " ++ show s
100+
print $ "Average latency: " ++ show (latency_total `div` fromIntegral s)
97101
where
98102
newThread = do
99-
ref <- newIORef 0 :: IO (IORef Word)
103+
ref_count <- newIORef 0 :: IO (IORef Word)
104+
ref_latency <- newIORef 0 :: IO (IORef Int64)
100105
c <- connectAction
101106
tid <- forkIO $ forever $ do
107+
t1 <- getTime Monotonic
102108
queryAction c
103-
modifyIORef' ref (+1)
104-
pure (ref, tid)
109+
t2 <- getTime Monotonic
110+
modifyIORef' ref_latency (+ (getDifference t2 t1))
111+
modifyIORef' ref_count (+1)
112+
pure (ref_count, ref_latency, tid)
113+
114+
getDifference (TimeSpec end_s end_ns) (TimeSpec start_s start_ns) =
115+
(end_s - start_s) * 1000000000 + end_ns - start_ns
105116

106117
requestAction c = replicateM_ 100 $ do
107118
sendBatchAndSync c [q]

postgres-wire.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ benchmark postgres-wire-bench
109109
, criterion
110110
, deepseq
111111
, postgresql-libpq
112+
, clock
112113
ghc-options: -O2 -threaded -rtsopts -with-rtsopts=-s
113114
default-language: Haskell2010
114115
default-extensions:

0 commit comments

Comments
 (0)
0