All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kyo.bench.SemaphoreBench.scala Maven / Gradle / Ivy

There is a newer version: 3.0.7
Show newest version
package kyo.bench

import org.openjdk.jmh.annotations._
import cats.effect.IO
import kyo._
import kyo.ios._
import zio.{ZIO, UIO}
import java.util.concurrent.Executors
import kyo.concurrent.fibers._
import kyo.concurrent.channels._
import kyo.concurrent.Access

import kyo.bench.Bench
import java.util.concurrent.atomic.AtomicInteger

class SemaphoreBench extends Bench.ForkOnly[Unit] {

  val depth = 10000

  def catsBench(): IO[Unit] = {
    import cats.effect.std.Semaphore

    def loop(s: Semaphore[IO], i: Int): IO[Unit] =
      if (i >= depth)
        IO.unit
      else
        s.acquire.flatMap(_ => s.release.flatMap(_ => loop(s, i + 1)))

    Semaphore[IO](1).flatMap(loop(_, 0))
  }

  def kyoBench() = Fibers.runBlocking(Fibers.fork(kyoBenchFiber()))

  override def kyoBenchFiber(): Unit > (IOs with Fibers) = {
    import kyo.concurrent.meters._

    def loop(s: Meter, i: Int): Unit > (IOs with Fibers) =
      if (i >= depth)
        IOs.unit
      else
        s.run(()).flatMap(_ => loop(s, i + 1))

    Meters.initSemaphore(1).flatMap(loop(_, 0))
  }

  def zioBench(): UIO[Unit] = {
    import zio.Semaphore

    def loop(s: Semaphore, i: Int): ZIO[Any, Nothing, Unit] =
      if (i >= depth)
        ZIO.unit
      else
        s.withPermit(ZIO.succeed(())).flatMap(_ => loop(s, i + 1))

    Semaphore.make(1).flatMap(loop(_, 0))
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy