@@ -8,6 +8,7 @@ import Data.ByteString.Builder (toLazyByteString)
8
8
import Data.ByteString (ByteString )
9
9
import Data.Vector as V (fromList , empty )
10
10
import Data.IORef
11
+ import Data.Int
11
12
import Data.Foldable
12
13
import Data.Maybe
13
14
import Control.Concurrent
@@ -16,6 +17,7 @@ import Control.Monad
16
17
import Data.Monoid
17
18
import Control.DeepSeq
18
19
import System.IO.Unsafe
20
+ import System.Clock
19
21
20
22
import qualified Database.PostgreSQL.LibPQ as LibPQ
21
23
@@ -91,17 +93,26 @@ benchRequests :: IO c -> (c -> IO a) -> IO ()
91
93
benchRequests connectAction queryAction = do
92
94
rs <- replicateM 8 newThread
93
95
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
96
99
print $ " Requests: " ++ show s
100
+ print $ " Average latency: " ++ show (latency_total `div` fromIntegral s)
97
101
where
98
102
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 )
100
105
c <- connectAction
101
106
tid <- forkIO $ forever $ do
107
+ t1 <- getTime Monotonic
102
108
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
105
116
106
117
requestAction c = replicateM_ 100 $ do
107
118
sendBatchAndSync c [q]
0 commit comments