Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.md #90

Merged
merged 3 commits into from
Jun 2, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ libraryDependencies ++= Seq(

```scala
armanbilge marked this conversation as resolved.
Show resolved Hide resolved
import io.chrisdavenport.rediculous._
import cats.implicits._
import cats.syntax.all._
import cats.effect._
import fs2.io.tcp._
import java.net.InetSocketAddress
import cats.effect.std.Console
import fs2.io.net._
import fs2._
import scala.concurrent.duration._
import com.comcast.ip4s._

// Mimics 150 req/s load with 4 operations per request.
// Completes 1,000,000 redis operations
Expand All @@ -30,40 +31,50 @@ object BasicExample extends IOApp {

def run(args: List[String]): IO[ExitCode] = {
val r = for {
blocker <- Blocker[IO]
sg <- SocketGroup[IO](blocker)
// maxQueued: How many elements before new submissions semantically block. Tradeoff of memory to queue jobs.
// maxQueued: How many elements before new submissions semantically block. Tradeoff of memory to queue jobs.
// Default 1000 is good for small servers. But can easily take 100,000.
// workers: How many threads will process pipelined messages.
connection <- RedisConnection.queued[IO](sg, "localhost", 6379, maxQueued = 10000, workers = 2)
connection <- RedisConnection
.queued[IO]
.withHost(Host.fromString("localhost").get)
.withPort(port"6379")
.withMaxQueued(10000)
.withWorkers(2)
.build
} yield connection

r.use {client =>
r.use { client =>
val r = (
RedisCommands.ping[Redis[IO, *]],
RedisCommands.get[Redis[IO, *]]("foo"),
RedisCommands.set[Redis[IO, *]]("foo", "value"),
RedisCommands.get[Redis[IO, *]]("foo")
).parTupled

val r2= List.fill(10)(r.run(client)).parSequence.map{_.flatMap{
case (_,_,_,_) => List((), (), (), ())
}}
val r2 = List.fill(10)(r.run(client)).parSequence.map {
_.flatMap { case (_, _, _, _) =>
List((), (), (), ())
}
}

val now = IO(java.time.Instant.now)
val now = Clock[IO].realTimeInstant
(
now,
Stream(()).covary[IO].repeat.map(_ => Stream.evalSeq(r2)).parJoin(15).take(1000000).compile.drain,
Stream(())
.covary[IO]
.repeat
.map(_ => Stream.evalSeq(r2))
.parJoin(15)
.take(1000000)
.compile
.drain,
now
).mapN{
case (before, _, after) => (after.toEpochMilli() - before.toEpochMilli()).millis
}.flatMap{ duration =>
IO(println(s"Operation took ${duration}"))
).mapN { case (before, _, after) =>
(after.toEpochMilli() - before.toEpochMilli()).millis
}.flatMap { duration =>
Console[IO].println(s"Operation took ${duration}")
}
} >>
IO.pure(ExitCode.Success)

}.as(ExitCode.Success)
}

}
```