graphql-engine/server/src-test/Data/TimeSpec.hs
Brandon Simmons 58ef316118 Add request timings and count histograms to telemetry. Closes #3552
We upload a set of accumulating timers and counters to track service
time for different types of operations, across several dimensions (e.g.
did we hit the plan cache, was a remote involved, etc.)

Also...

Standardize on DiffTime as a standard duration type, and try to use it
consistently.

See discussion here:
https://github.com/hasura/graphql-engine/pull/3584#pullrequestreview-340679369

It should be possible to overwrite that module so the new threadDelay
sticks per the pattern in #3705 blocked on #3558

Rename the Control.Concurrent.Extended.threadDelay to `sleep` since a
naive use with a literal argument would be very bad!

We catch a bug in 'computeTimeDiff'.

Add convenient 'Read' instances to the time unit utility types. Make
'Second' a newtype to support this.
2020-02-03 18:50:10 -06:00

45 lines
1.3 KiB
Haskell

module Data.TimeSpec (spec) where
-- | Time-related properties we care about.
import Prelude
import Data.Time.Clock.Units
import Data.Time
import Data.Aeson
import Test.Hspec
spec :: Spec
spec = do
timeUnitsSpec
diffTimeSpec
timeUnitsSpec :: Spec
timeUnitsSpec =
describe "time units" $ do
it "converts correctly" $ do
seconds 123 `shouldBe` 123
milliseconds 123 `shouldBe` 0.123
microseconds 123 `shouldBe` 0.000123
nanoseconds 123 `shouldBe` 0.000000123
it "has a correct Read instance" $ do
seconds (read "123") `shouldBe` 123
milliseconds (read "123") `shouldBe` 0.123
microseconds (read "123") `shouldBe` 0.000123
nanoseconds (read "123") `shouldBe` 0.000000123
it "JSON serializes as proper units" $ do
toJSON (1 :: Seconds) `shouldBe` Number 1
decode "1.0" `shouldBe` Just (1 :: Seconds)
it "converts with fromUnits" $ do
fromUnits (2 :: Minutes) `shouldBe` (120 :: NominalDiffTime)
fromUnits (60 :: Seconds) `shouldBe` (1 :: Minutes)
diffTimeSpec :: Spec
diffTimeSpec =
describe "DiffTime" $ do
it "JSON serializes as seconds" $ do
-- although we would prefer to use Seconds instead...
toJSON (1 :: DiffTime) `shouldBe` Number 1
decode "1.0" `shouldBe` Just (1 :: DiffTime)