Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
70 changes: 70 additions & 0 deletions modules/bench/src/main/scala/skunk/bench/WarmSessionBench.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2018-2024 by Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package skunk.bench

import java.util.concurrent.TimeUnit

import cats.effect._
import cats.effect.unsafe.implicits.global
import org.openjdk.jmh.annotations._
import org.typelevel.otel4s.metrics.MeterProvider
import org.typelevel.otel4s.trace.TracerProvider
import skunk._
import skunk.codec.all._
import skunk.data.Completion
import skunk.implicits._

/**
* Warm parameterized statements on one session, so that parse and describe are cached and each
* operation is a single Bind/Execute/Sync exchange. Needs the `world` database from
* docker-compose.yml on localhost:5432.
*/
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(value = 1, jvmArgsAppend = Array("-Xmx2g"))
@Warmup(iterations = 5, time = 5)
@Measurement(iterations = 5, time = 5)
class WarmSessionBench {

implicit val tracerProvider: TracerProvider[IO] = TracerProvider.noop[IO]
implicit val meterProvider: MeterProvider[IO] = MeterProvider.noop[IO]

var session: Session[IO] = _
var release: IO[Unit] = _

val query: Query[Int, Int] = sql"select $int4".query(int4)
val command: Command[Int] = sql"update warm_session_bench set v = $int4".command

@Setup(Level.Trial)
def setup(): Unit = {
val (s, rel) =
Session.Builder[IO]
.withUserAndPassword("jimmy", "banana")
.withDatabase("world")
.single
.allocated
.unsafeRunSync()
session = s
release = rel
(s.execute(sql"create temporary table warm_session_bench (v int4)".command) *>
s.execute(sql"insert into warm_session_bench values (0)".command) *>
s.unique(query)(0) *>
s.execute(command)(0)).void.unsafeRunSync()
}

@TearDown(Level.Trial)
def tearDown(): Unit =
release.unsafeRunSync()

@Benchmark
def warmQuery(): Int =
session.unique(query)(42).unsafeRunSync()

@Benchmark
def warmCommand(): Completion =
session.execute(command)(42).unsafeRunSync()

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package skunk.net

import skunk.net.message.BackendMessage
import skunk.net.message.{ BackendMessage, FrontendMessage }
import skunk.util.Origin
import cats.effect.Concurrent
import cats.syntax.all._
Expand All @@ -13,6 +13,10 @@ import skunk.exception.ProtocolError
abstract class AbstractMessageSocket[F[_]: Concurrent]
extends MessageSocket[F] {

/** Default implementation in terms of `send`, one write per message. */
override def sendAll(messages: List[FrontendMessage]): F[Unit] =
messages.traverse_(send)

override def expect[B](f: PartialFunction[BackendMessage, B])(implicit or: Origin): F[B] =
receive.flatMap { m =>
if (f.isDefinedAt(m)) f(m).pure[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ object BufferedMessageSocket {
case None => ms.send(message)
}

override def sendAll(messages: List[FrontendMessage]): F[Unit] =
term.get.flatMap {
case Some(t) => Concurrent[F].raiseError(t)
case None => ms.sendAll(messages)
}

override def transactionStatus: SignallingRef[F, TransactionStatus] = xaSig
override def parameters: SignallingRef[F, Map[String, String]] = paSig
override def backendKeyData: Deferred[F, BackendKeyData] = bkSig
Expand Down
20 changes: 20 additions & 0 deletions modules/core/shared/src/main/scala/net/MessageSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import cats.effect.std.Console
import cats.effect.std.Queue
import cats.syntax.all._
import scala.io.AnsiColor
import scodec.bits.BitVector
import skunk.net.message.{ Sync => _, _ }
import skunk.util.Origin
import fs2.io.net.Socket
Expand All @@ -27,6 +28,13 @@ trait MessageSocket[F[_]] {
/** Send the specified message. */
def send(message: FrontendMessage): F[Unit]

/**
* Send the specified messages, in order, in a single write. Use this for messages that are sent
* back to back before reading a reply (e.g. `Bind`, `Execute`, `Sync`), so that they go out in one
* write rather than in one write (and possibly one TCP segment) each.
*/
def sendAll(messages: List[FrontendMessage]): F[Unit]

/** Destructively read the last `n` messages from the circular buffer. */
def history(max: Int): F[List[Either[Any, Any]]]

Expand Down Expand Up @@ -76,6 +84,18 @@ object MessageSocket {
_ <- cb.offer(Left(message))
} yield ()

override def sendAll(messages: List[FrontendMessage]): F[Unit] =
messages match {
case Nil => Applicative[F].unit
case m :: Nil => send(m)
case _ =>
for {
_ <- messages.traverse_(m => debug(s" → ${AnsiColor.YELLOW}$m${AnsiColor.RESET}"))
_ <- bvs.write(messages.foldLeft(BitVector.empty)(_ ++ _.encode))
_ <- messages.traverse_(m => cb.offer(Left(m)))
} yield ()
}

override def history(max: Int): F[List[Either[Any, Any]]] =
cb.take.flatMap { first =>
def pump(acc: List[Either[Any, Any]]): F[List[Either[Any, Any]]] =
Expand Down
3 changes: 1 addition & 2 deletions modules/core/shared/src/main/scala/net/protocol/Bind.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ object Bind {
SkunkAttributes.portalId(pn.value),
SkunkAttributes.statementId(statement.id.value)
)
_ <- send(BindMessage(pn.value, statement.id.value, ea.map(_.map(_.value))))
_ <- send(Flush)
_ <- sendAll(BindMessage(pn.value, statement.id.value, ea.map(_.map(_.value))), Flush)
_ <- flatExpect {
case BindComplete => ().pure[F]
case ErrorResponse(info) =>
Expand Down
32 changes: 16 additions & 16 deletions modules/core/shared/src/main/scala/net/protocol/BindExecute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ object BindExecute {
args: A,
argsOrigin: Origin,
syncSent: Boolean
): (F[PortalId], F[Unit]) = {
): (F[(PortalId, BindMessage)], F[Unit]) = {
val ea = statement.statement.encoder.encode(args) // encoded args

val preBind: F[PortalId] = for {

// Yields the Bind rather than sending it, so the caller can send it in one write together
// with the Execute and Sync/Flush that follow.
val preBind: F[(PortalId, BindMessage)] = for {
pn <- nextName("portal").map(PortalId(_))
_ <- Telemetry[F].addAttributes(
SkunkAttributes.portalId(pn.value),
SkunkAttributes.statementId(statement.id.value)
)
_ <- send(BindMessage(pn.value, statement.id.value, ea.map(_.map(_.value))))
} yield pn
} yield (pn, BindMessage(pn.value, statement.id.value, ea.map(_.map(_.value))))

val postBind: F[Unit] = flatExpect {
case BindComplete => ().pure[F]
Expand Down Expand Up @@ -159,11 +160,11 @@ object BindExecute {
) {

for {
pn <- preBind
_ <- send(ExecuteMessage(pn.value, 0))
pb <- preBind
(pn, bind) = pb
// Sync rather than Flush: a command never pages, so there is no portal to keep alive
// between Executes, and all three replies come back from one flush.
_ <- send(Sync)
_ <- sendAll(bind, ExecuteMessage(pn.value, 0), Sync)
_ <- postBind
ca <- postExec
} yield {
Expand Down Expand Up @@ -207,14 +208,14 @@ object BindExecute {
redactionStrategy,
) {
for {
pn <- preBind
pb <- preBind
(pn, bind) = pb
_ <- Telemetry[F].addAttributes(
SkunkAttributes.fetchMaxRows(maxRows.toLong),
SkunkAttributes.portalId(pn.value),
SkunkAttributes.statementId(statement.id.value)
)
_ <- send(ExecuteMessage(pn.value, maxRows))
_ <- send(Sync)
_ <- sendAll(bind, ExecuteMessage(pn.value, maxRows), Sync)
_ <- postBind
// Leaves the ReadyForQuery unread on success -- see unrollPresynced -- since we need
// the status it carries. It must be read on the PortalSuspended path too, taken
Expand All @@ -227,8 +228,7 @@ object BindExecute {
// because the mutex is not reentrant, and attempted so that failing to close
// cannot mask the decode error.
.onError { case _: DecodeException[_, _, _] =>
(send(CloseMessage.portal(pn.value)) *>
send(Flush) *>
(sendAll(CloseMessage.portal(pn.value), Flush) *>
expect { case CloseComplete => }).attempt.void
}
xa <- expect { case ReadyForQuery(s) => s }
Expand Down Expand Up @@ -267,10 +267,10 @@ object BindExecute {
redactionStrategy,
) {
for {
pn <- preBind
pb <- preBind
(pn, bind) = pb
_ <- Telemetry[F].addAttributes(SkunkAttributes.fetchMaxRows(initialSize.toLong))
_ <- send(ExecuteMessage(pn.value, initialSize))
_ <- send(Flush)
_ <- sendAll(bind, ExecuteMessage(pn.value, initialSize), Flush)
_ <- postBind
rs <- unroll(statement, args, argsOrigin, redactionStrategy)
} yield new Protocol.QueryPortal[F, A, B](pn, statement, args, argsOrigin, redactionStrategy) {
Expand Down
3 changes: 1 addition & 2 deletions modules/core/shared/src/main/scala/net/protocol/Close.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ object Close {

def close(message: CloseMessage): F[Unit] =
for {
_ <- send(message)
_ <- send(Flush)
_ <- sendAll(message, Flush)
_ <- expect { case CloseComplete => }
} yield ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ object Execute {
SkunkAttributes.portalId(portal.id.value),
SkunkAttributes.statementId(portal.preparedStatement.id.value)
)
_ <- send(ExecuteMessage(portal.id.value, maxRows))
_ <- send(Flush)
_ <- sendAll(ExecuteMessage(portal.id.value, maxRows), Flush)
rs <- unroll(portal)
} yield rs
}
Expand Down
47 changes: 24 additions & 23 deletions modules/core/shared/src/main/scala/net/protocol/ParseDescribe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ object ParseDescribe {
).raiseError[F, Unit]
} yield a

def parseExchange(stmt: Statement[_], ty: Typer): F[(F[StatementId], StatementId => F[Unit])] =
// Send `messages` followed by Flush in one write, or nothing if there are none, as then there is
// no reply to wait for.
def sendWithFlush(messages: List[FrontendMessage]): F[Unit] =
sendAll[F]((messages :+ Flush): _*).whenA(messages.nonEmpty)

// The `pre` halves below yield the messages to send rather than sending them, so that Parse,
// Describe and Flush go out in one write. Nothing is sent at all if both are cached.
def parseExchange(stmt: Statement[_], ty: Typer): F[(F[(StatementId, List[FrontendMessage])], StatementId => F[Unit])] =
stmt.encoder.oids(ty) match {

case Right(os) if os.length > Short.MaxValue =>
TooManyParametersException(stmt).raiseError[F, (F[StatementId], StatementId => F[Unit])]
TooManyParametersException(stmt).raiseError[F, (F[(StatementId, List[FrontendMessage])], StatementId => F[Unit])]

case Right(os) =>

Expand All @@ -58,12 +65,11 @@ object ParseDescribe {
)
)

OptionT(parseCache.value.get(stmt)).map(id => (addStatement(id).as(id), (_:StatementId) => ().pure)).getOrElse {
OptionT(parseCache.value.get(stmt)).map(id => (addStatement(id).as((id, List.empty[FrontendMessage])), (_:StatementId) => ().pure)).getOrElse {
val pre = for {
id <- nextName("statement").map(StatementId(_))
_ <- addStatement(id)
_ <- send(ParseMessage(id.value, stmt.sql, os))
} yield id
} yield (id, List[FrontendMessage](ParseMessage(id.value, stmt.sql, os)))
val post = (id: StatementId) => for {
_ <- flatExpect {
case ParseComplete => ().pure[F]
Expand All @@ -75,17 +81,15 @@ object ParseDescribe {
(pre, post)
}
case Left(err) =>
UnknownTypeException(stmt, err, ty.strategy).raiseError[F, (F[StatementId], StatementId => F[Unit])]
UnknownTypeException(stmt, err, ty.strategy).raiseError[F, (F[(StatementId, List[FrontendMessage])], StatementId => F[Unit])]

}

override def command[A](cmd: skunk.Command[A], ty: Typer): F[StatementId] = {

def describeExchange: F[(StatementId => F[Unit], F[Unit])] = {
OptionT(cache.commandCache.get(cmd)).as(((_: StatementId) => ().pure[F], ().pure[F])).getOrElse {
val pre = (id: StatementId) => for {
_ <- send(DescribeMessage.statement(id.value))
} yield ()
def describeExchange: F[(StatementId => List[FrontendMessage], F[Unit])] = {
OptionT(cache.commandCache.get(cmd)).as(((_: StatementId) => List.empty[FrontendMessage], ().pure[F])).getOrElse {
val pre = (id: StatementId) => List[FrontendMessage](DescribeMessage.statement(id.value))

val post: F[Unit] = for {
_ <- expect { case ParameterDescription(_) => } // always ok
Expand Down Expand Up @@ -114,9 +118,9 @@ object ParseDescribe {
parseExchange(cmd, ty).flatMap { case (preParse, postParse) =>
describeExchange.flatMap { case (preDesc, postDesc) =>
for {
id <- preParse
_ <- preDesc(id)
_ <- send(Flush)
pp <- preParse
(id, parse) = pp
_ <- sendWithFlush(parse ++ preDesc(id))
_ <- postParse(id)
_ <- postDesc
} yield id
Expand All @@ -128,21 +132,18 @@ object ParseDescribe {

override def apply[A, B](query: skunk.Query[A, B], ty: Typer): F[(StatementId, TypedRowDescription)] = {

def describeExchange: F[(StatementId => F[Unit], F[TypedRowDescription])] = {
def describeExchange: F[(StatementId => List[FrontendMessage], F[TypedRowDescription])] = {
def addColumnTypes(td: TypedRowDescription): F[Unit] =
Telemetry[F].addProtocolAttributes(
SkunkAttributes.resultColumnTypes(td.fields.map(_.tpe).mkString("[", ", ", "]"))
)

OptionT(cache.queryCache.get(query)).map { rd =>
val pre = (_: StatementId) => ().pure[F]
val pre = (_: StatementId) => List.empty[FrontendMessage]
val post = addColumnTypes(rd).as(rd)
(pre, post)
}.getOrElse {
val pre = (id: StatementId) =>
for {
_ <- send(DescribeMessage.statement(id.value))
} yield ()
val pre = (id: StatementId) => List[FrontendMessage](DescribeMessage.statement(id.value))

val post =
for {
Expand All @@ -168,9 +169,9 @@ object ParseDescribe {
parseExchange(query, ty).flatMap { case (preParse, postParse) =>
describeExchange.flatMap { case (preDesc, postDesc) =>
for {
id <- preParse
_ <- preDesc(id)
_ <- send(Flush)
pp <- preParse
(id, parse) = pp
_ <- sendWithFlush(parse ++ preDesc(id))
_ <- postParse(id)
rd <- postDesc
} yield (id, rd)
Expand Down
4 changes: 4 additions & 0 deletions modules/core/shared/src/main/scala/net/protocol/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ package object protocol {
def send[F[_]](message: FrontendMessage)(implicit ev: MessageSocket[F]): F[Unit] =
ev.send(message)

/** Send `messages` in a single write; see `MessageSocket.sendAll`. */
def sendAll[F[_]](messages: FrontendMessage*)(implicit ev: MessageSocket[F]): F[Unit] =
ev.sendAll(messages.toList)

def history[F[_]](max: Int)(implicit ev: MessageSocket[F]): F[List[Either[Any, Any]]] =
ev.history(max)

Expand Down
Loading
Loading