fix: fixes server timings' precision (#3227)

* test: fix test_io accordingly
This commit is contained in:
Andrei Dziahel
2024-02-14 12:26:34 -05:00
committed by GitHub
parent b22bb748f2
commit 3432f75ed4
2 changed files with 9 additions and 6 deletions
+5 -2
View File
@@ -1,3 +1,4 @@
{-# LANGUAGE NumericUnderscores #-}
module PostgREST.Response.Performance
( ServerTiming (..)
, serverTimingHeader
@@ -8,6 +9,7 @@ import qualified Network.HTTP.Types as HTTP
import Numeric (showFFloat)
import Protolude
-- | ServerTiming represents the timing data for a request, in seconds.
data ServerTiming =
ServerTiming
{ jwt :: Maybe Double
@@ -19,14 +21,15 @@ data ServerTiming =
deriving (Show)
-- | Render the Server-Timing header from a ServerTimingData
-- The duration precision is milliseconds, per the docs
--
-- >>> serverTimingHeader ServerTiming { plan=Just 0.1, transaction=Just 0.2, response=Just 0.3, jwt=Just 0.4, parse=Just 0.5}
-- ("Server-Timing","jwt;dur=400000.0, parse;dur=500000.0, plan;dur=100000.0, transaction;dur=200000.0, response;dur=300000.0")
-- ("Server-Timing","jwt;dur=400.0, parse;dur=500.0, plan;dur=100.0, transaction;dur=200.0, response;dur=300.0")
serverTimingHeader :: ServerTiming -> HTTP.Header
serverTimingHeader timing =
("Server-Timing", renderTiming)
where
renderMetric metric = maybe "" (\dur -> BS.concat [metric, BS.pack $ ";dur=" <> showFFloat (Just 1) (dur * 1000000) ""])
renderMetric metric = maybe "" (\dur -> BS.concat [metric, BS.pack $ ";dur=" <> showFFloat (Just 1) (dur * 1_000) ""])
renderTiming = BS.intercalate ", " $ (\(k, v) -> renderMetric k (v timing)) <$>
[ ("jwt", jwt)
, ("parse", parse)