From c30155731f82b5cca5de3275247905352b48481e Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Fri, 30 Oct 2026 00:47:33 +0100 Subject: [PATCH 1/2] Send Bind/Execute/Sync in one write Every `MessageSocket#send` was its own socket write, so a warm parameterized command (Bind, Execute, Sync) cost three writes and, with TCP_NODELAY, up to three TCP segments. A lone Flush was also written when the statement was already parsed and described. Add `MessageSocket#sendAll`, which encodes the messages and writes them at once, and use it wherever messages go out back to back before a read: Bind+Execute+Sync/Flush, Parse+Describe+Flush, Bind+Flush, Execute+Flush and Close+Flush. Skip the Flush when Parse and Describe are both cached, as nothing is sent and no reply is read. ExchangeCountTest now asserts writes too: one write per exchange for every operation kind. Co-Authored-By: Claude Opus 5.5 --- .../scala/net/AbstractMessageSocket.scala | 6 ++- .../scala/net/BufferedMessageSocket.scala | 6 +++ .../src/main/scala/net/MessageSocket.scala | 20 ++++++++ .../src/main/scala/net/protocol/Bind.scala | 3 +- .../main/scala/net/protocol/BindExecute.scala | 32 ++++++------- .../src/main/scala/net/protocol/Close.scala | 3 +- .../src/main/scala/net/protocol/Execute.scala | 3 +- .../scala/net/protocol/ParseDescribe.scala | 47 ++++++++++--------- .../src/main/scala/net/protocol/package.scala | 4 ++ .../scala/simulation/ExchangeCountTest.scala | 33 +++++++------ .../scala/simulation/ExchangeCounter.scala | 10 +++- .../scala/simulation/SimMessageSocket.scala | 3 ++ .../src/test/scala/simulation/SimTest.scala | 1 + 13 files changed, 108 insertions(+), 63 deletions(-) diff --git a/modules/core/shared/src/main/scala/net/AbstractMessageSocket.scala b/modules/core/shared/src/main/scala/net/AbstractMessageSocket.scala index 7ad919477..a7470ac28 100644 --- a/modules/core/shared/src/main/scala/net/AbstractMessageSocket.scala +++ b/modules/core/shared/src/main/scala/net/AbstractMessageSocket.scala @@ -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._ @@ -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] diff --git a/modules/core/shared/src/main/scala/net/BufferedMessageSocket.scala b/modules/core/shared/src/main/scala/net/BufferedMessageSocket.scala index 34b317e3f..6073447bb 100644 --- a/modules/core/shared/src/main/scala/net/BufferedMessageSocket.scala +++ b/modules/core/shared/src/main/scala/net/BufferedMessageSocket.scala @@ -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 diff --git a/modules/core/shared/src/main/scala/net/MessageSocket.scala b/modules/core/shared/src/main/scala/net/MessageSocket.scala index b42f231d4..7fb53b0a3 100644 --- a/modules/core/shared/src/main/scala/net/MessageSocket.scala +++ b/modules/core/shared/src/main/scala/net/MessageSocket.scala @@ -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 @@ -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]]] @@ -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]]] = diff --git a/modules/core/shared/src/main/scala/net/protocol/Bind.scala b/modules/core/shared/src/main/scala/net/protocol/Bind.scala index c1c982320..0c9f098c0 100644 --- a/modules/core/shared/src/main/scala/net/protocol/Bind.scala +++ b/modules/core/shared/src/main/scala/net/protocol/Bind.scala @@ -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) => diff --git a/modules/core/shared/src/main/scala/net/protocol/BindExecute.scala b/modules/core/shared/src/main/scala/net/protocol/BindExecute.scala index 8b3cbb745..4ca0af106 100644 --- a/modules/core/shared/src/main/scala/net/protocol/BindExecute.scala +++ b/modules/core/shared/src/main/scala/net/protocol/BindExecute.scala @@ -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] @@ -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 { @@ -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 @@ -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 } @@ -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) { diff --git a/modules/core/shared/src/main/scala/net/protocol/Close.scala b/modules/core/shared/src/main/scala/net/protocol/Close.scala index f9ad02aec..32d17e146 100644 --- a/modules/core/shared/src/main/scala/net/protocol/Close.scala +++ b/modules/core/shared/src/main/scala/net/protocol/Close.scala @@ -34,8 +34,7 @@ object Close { def close(message: CloseMessage): F[Unit] = for { - _ <- send(message) - _ <- send(Flush) + _ <- sendAll(message, Flush) _ <- expect { case CloseComplete => } } yield () } diff --git a/modules/core/shared/src/main/scala/net/protocol/Execute.scala b/modules/core/shared/src/main/scala/net/protocol/Execute.scala index 3c5dac727..8b47d6a17 100644 --- a/modules/core/shared/src/main/scala/net/protocol/Execute.scala +++ b/modules/core/shared/src/main/scala/net/protocol/Execute.scala @@ -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 } diff --git a/modules/core/shared/src/main/scala/net/protocol/ParseDescribe.scala b/modules/core/shared/src/main/scala/net/protocol/ParseDescribe.scala index 23c73b981..227f54421 100644 --- a/modules/core/shared/src/main/scala/net/protocol/ParseDescribe.scala +++ b/modules/core/shared/src/main/scala/net/protocol/ParseDescribe.scala @@ -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) => @@ -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] @@ -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 @@ -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 @@ -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 { @@ -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) diff --git a/modules/core/shared/src/main/scala/net/protocol/package.scala b/modules/core/shared/src/main/scala/net/protocol/package.scala index 07018b514..c39359e93 100644 --- a/modules/core/shared/src/main/scala/net/protocol/package.scala +++ b/modules/core/shared/src/main/scala/net/protocol/package.scala @@ -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) diff --git a/modules/tests/shared/src/test/scala/simulation/ExchangeCountTest.scala b/modules/tests/shared/src/test/scala/simulation/ExchangeCountTest.scala index 16d65ecc8..31a362b59 100644 --- a/modules/tests/shared/src/test/scala/simulation/ExchangeCountTest.scala +++ b/modules/tests/shared/src/test/scala/simulation/ExchangeCountTest.scala @@ -179,24 +179,27 @@ class ExchangeCountTest extends FTest with SimMessageSocket.DSL { discard <- measure(simpleCount = 2)(_.executeDiscard(multi)) begin <- measure(completion = Completion.Begin)(_.execute(sql"begin".command)) + // (label, counts, expected exchanges, expected writes). Messages sent back to back before a + // read go out in one write, so writes equal exchanges. table = List( - ("parameterized command, warm", cmdWarm, 1), - ("parameterized command, cold", cmdCold, 2), - ("parameterized command, in tx", cmdInTx, 2), - ("parameterized query, all rows", qAll, 1), - ("parameterized query, all rows/tx", qAllInTx, 2), - ("parameterized query, unique", qUnique, 1), - ("parameterized query, option", qOption, 1), - ("streaming query, 3 rows / 2", qStream, 4), - ("parameterless command (simple)", simpCmd, 1), - ("parameterless query (simple)", simpQry, 1), - ("executeDiscard (simple, 2 stmt)", discard, 1), - ("BEGIN (simple)", begin, 1) + ("parameterized command, warm", cmdWarm, 1, 1), + ("parameterized command, cold", cmdCold, 2, 2), + ("parameterized command, in tx", cmdInTx, 2, 2), + ("parameterized query, all rows", qAll, 1, 1), + ("parameterized query, all rows/tx", qAllInTx, 2, 2), + ("parameterized query, unique", qUnique, 1, 1), + ("parameterized query, option", qOption, 1, 1), + ("streaming query, 3 rows / 2", qStream, 4, 4), + ("parameterless command (simple)", simpCmd, 1, 1), + ("parameterless query (simple)", simpQry, 1, 1), + ("executeDiscard (simple, 2 stmt)", discard, 1, 1), + ("BEGIN (simple)", begin, 1, 1) ) - _ <- IO.println("\n" + render(table) + "\n") - _ <- table.traverse_ { case (label, c, expected) => - assertEqual(s"exchanges for $label", c.exchanges, expected) + _ <- IO.println("\n" + render(table.map { case (l, c, e, _) => (l, c, e) }) + "\n") + _ <- table.traverse_ { case (label, c, exchanges, writes) => + assertEqual(s"exchanges for $label", c.exchanges, exchanges) *> + assertEqual(s"writes for $label", c.writes, writes) } } yield () diff --git a/modules/tests/shared/src/test/scala/simulation/ExchangeCounter.scala b/modules/tests/shared/src/test/scala/simulation/ExchangeCounter.scala index 109f253ba..0271a8ca9 100644 --- a/modules/tests/shared/src/test/scala/simulation/ExchangeCounter.scala +++ b/modules/tests/shared/src/test/scala/simulation/ExchangeCounter.scala @@ -6,6 +6,7 @@ package tests package simulation import cats.effect._ +import cats.syntax.all._ import fs2.Stream import fs2.concurrent.{ Signal, SignallingRef } import skunk.data.{ Notification, TransactionStatus } @@ -15,8 +16,8 @@ import skunk.net.message.{ BackendKeyData, BackendMessage, FrontendMessage, Para /** What one operation cost on the wire. * * `exchanges` counts `send`→`receive` transitions -- the points where the client stopped writing and - * blocked on a reply. `writes` and `bytesSent` move independently of it, since nothing buffers - * frontend messages, so three pipelined messages still cost three writes. + * blocked on a reply. `writes` counts socket writes: one per `send`, and one per `sendAll` however + * many messages it carries. `bytesSent` moves independently of both. */ final case class ExchangeCounts(exchanges: Int, writes: Int, reads: Int, bytesSent: Long) @@ -49,6 +50,11 @@ final class ExchangeCounter private ( override def send(message: FrontendMessage): IO[Unit] = state.update(_.writing(message.encode.size / 8)) *> underlying.send(message) + // One write however many messages, as that is what the real socket does. + override def sendAll(messages: List[FrontendMessage]): IO[Unit] = + state.update(_.writing(messages.foldMap(_.encode.size / 8))).whenA(messages.nonEmpty) *> + underlying.sendAll(messages) + override def history(max: Int): IO[List[Either[Any, Any]]] = underlying.history(max) diff --git a/modules/tests/shared/src/test/scala/simulation/SimMessageSocket.scala b/modules/tests/shared/src/test/scala/simulation/SimMessageSocket.scala index 16ce682f5..34a779a3d 100644 --- a/modules/tests/shared/src/test/scala/simulation/SimMessageSocket.scala +++ b/modules/tests/shared/src/test/scala/simulation/SimMessageSocket.scala @@ -118,6 +118,9 @@ object SimMessageSocket { } } .flatten + def sendAll(messages: List[FrontendMessage]): IO[Unit] = + messages.traverse_(send) + def history(max: Int): IO[List[Either[Any,Any]]] = Nil.pure[IO] // not implemeneted diff --git a/modules/tests/shared/src/test/scala/simulation/SimTest.scala b/modules/tests/shared/src/test/scala/simulation/SimTest.scala index 22072d234..403ce86bb 100644 --- a/modules/tests/shared/src/test/scala/simulation/SimTest.scala +++ b/modules/tests/shared/src/test/scala/simulation/SimTest.scala @@ -30,6 +30,7 @@ trait SimTest extends FTest with SimMessageSocket.DSL { private class SimulatedBufferedMessageSocket(ms: MessageSocket[IO]) extends BufferedMessageSocket[IO] { def receive: IO[BackendMessage] = ms.receive def send(message: FrontendMessage): IO[Unit] = ms.send(message) + def sendAll(messages: List[FrontendMessage]): IO[Unit] = ms.sendAll(messages) def history(max: Int): IO[List[Either[Any,Any]]] = ms.history(max) def expect[B](f: PartialFunction[BackendMessage,B])(implicit or: Origin): IO[B] = ms.expect(f) def flatExpect[B](f: PartialFunction[BackendMessage,IO[B]])(implicit or: Origin): IO[B] = ms.flatExpect(f) From 924f6da0607f5eb6cdf68002add4661e69754c61 Mon Sep 17 00:00:00 2001 From: Ondra Pelech Date: Fri, 30 Oct 2026 12:12:46 +0100 Subject: [PATCH 2/2] Add WarmSessionBench for warm statements on one session JMH benchmark of a warm parameterized query (`unique`) and command (`execute`) on a single session, so each operation is one Bind/Execute/Sync exchange. Needs the docker-compose Postgres. Results, `bench/Jmh/run -i 10 WarmSessionBench` (1 fork, 5x5s warmup, 10x5s measurement), local Postgres 11 in Docker, JDK 21, ops/s: | Benchmark | main (1045d3b2) | this branch | change | |-------------|-----------------|-----------------|--------| | warmCommand | 9930 +- 206 | 11977 +- 335 | +21% | | warmQuery | 10623 +- 350 | 13769 +- 343 | +30% | A second branch run gave 11765 +- 510 and 13663 +- 406. Load average was 3 to 6 during the runs quoted. A main run with load average 8 to 11 was too noisy to use (5807 +- 3013 and 9780 +- 1561). Co-Authored-By: Claude Opus 5.5 --- .../scala/skunk/bench/WarmSessionBench.scala | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 modules/bench/src/main/scala/skunk/bench/WarmSessionBench.scala diff --git a/modules/bench/src/main/scala/skunk/bench/WarmSessionBench.scala b/modules/bench/src/main/scala/skunk/bench/WarmSessionBench.scala new file mode 100644 index 000000000..f7d9b68b2 --- /dev/null +++ b/modules/bench/src/main/scala/skunk/bench/WarmSessionBench.scala @@ -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() + +}