Monotonic timing library for Scala 3 and Cats Effect.
				
			
		| 
		
			All checks were successful
		
		
	 / Build and Release Library (push) Successful in 2m5s Reviewed-on: #2 | ||
|---|---|---|
| .forgejo/workflows | ||
| project | ||
| src | ||
| .gitignore | ||
| .pre-commit-config.yaml | ||
| .scalafmt.conf | ||
| build.sbt | ||
| LICENSE | ||
| README.md | ||
gs-timing
GS Open Source | License (MIT)
Timing library for Cats Effect and Scala 3.
Usage
Dependency
This artifact is available in the Garrity Software Maven repository.
externalResolvers +=
  "Garrity Software Releases" at "https://maven.garrity.co/gs"
val Gstiming: ModuleID =
  "gs" %% "gs-timing-v0" % "$VERSION"
System Time
import gs.timing.v0.*
import cats.effect.IO
import cats.effect.unsafe.IORuntime
given IORuntime = IORuntime.global
val timing = new Timing(MonotonicProvider.system[IO])
val nanoTime: Long = timing.monotonic().unsafeRunSync()
Elapsed Time
import gs.timing.v0.*
import cats.effect.IO
import cats.effect.unsafe.IORuntime
import scala.concurrent.duration._
given IORuntime = IORuntime.global
val timing = new Timing(MonotonicProvider.system[IO])
val program: IO[(ElapsedTime, ElapsedTime)] =
  for
    timer    <- timing.start()
    _        <- IO.sleep(1.seconds)
    elapsed1 <- timer.checkpoint()
    _        <- IO.sleep(1.seconds)
    elapsed2 <- timer.checkpoint()
  yield
    (elapsed1, elapsed2)
val (e1, e2) = program.unsafeRunSync()
assert(e1.start == e2.start)
assert(e1.end != e2.end)
println(e1.duration)
println(e1.toNanoseconds())
println(e1.toMilliseconds())
println(e1.toSeconds())
Testing
For example, using munit and a simple helper for running IO-based tests:
iotest("should retrieve monotonic time") {
  for
    (provider, timing) <- Timing.manual[IO]
    t1                 <- timing.monotonic()
    t2                 <- timing.monotonic()
    _                  <- provider.tick()
    t3                 <- timing.monotonic()
    _                  <- provider.tick()
    t4                 <- timing.monotonic()
    _                  <- provider.reset()
    t5                 <- timing.monotonic()
  yield
    assertEquals(t1, 0L)
    assertEquals(t2, 0L)
    assertEquals(t3, 1L)
    assertEquals(t4, 2L)
    assertEquals(t5, 0L)
}
Donate
Enjoy this project or want to help me achieve my goals? Consider Donating to Pat on Ko-fi.