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
13 changes: 13 additions & 0 deletions frameworks/sharaf/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM sbtscala/scala-sbt:eclipse-temurin-21.0.11_10_1.12.15_3.3.8 AS build
WORKDIR /app
COPY build.sbt .
COPY project/build.properties project/plugins.sbt ./project/
RUN sbt update
COPY src ./src
RUN sbt assembly

FROM eclipse-temurin:25-jre-alpine
WORKDIR /app
COPY --from=build /app/target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
20 changes: 20 additions & 0 deletions frameworks/sharaf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Sharaf

Sharaf uses one virtual thread per persistent connection. PostgreSQL-backed
profiles use Squery and HikariCP.

Supported profiles:

- `baseline`, `pipelined`, `limited-conn`
- `async`
- `json-comp`
- `async-db`, `fortunes`
- `latency-1m`, `latency-10k`, `latency-500k-8cpu`

Potateh does not currently provide TLS, HTTP/2, HTTP/3, WebSocket, or gRPC, so
those profiles are intentionally not advertised.

```bash
./scripts/validate.sh sharaf
./scripts/benchmark-lite.sh sharaf baseline,pipelined,limited-conn
```
19 changes: 19 additions & 0 deletions frameworks/sharaf/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ThisBuild / scalaVersion := "3.10.0"

lazy val root = (project in file("."))
.settings(
name := "httparena-sharaf",
libraryDependencies ++= Seq(
"ba.sake" %% "sharaf-potateh" % "0.23.0",
"ba.sake" %% "squery" % "0.13.0",
"com.zaxxer" % "HikariCP" % "7.1.0",
"org.postgresql" % "postgresql" % "42.7.14"
),
assembly / mainClass := Some("Main"),
assembly / assemblyOutputPath := baseDirectory.value / "target" / "app.jar",
assembly / assemblyMergeStrategy := {
case PathList("META-INF", "services", _*) => MergeStrategy.concat
case PathList("META-INF", _*) => MergeStrategy.discard
case _ => MergeStrategy.first
}
)
29 changes: 29 additions & 0 deletions frameworks/sharaf/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"display_name": "Sharaf",
"language": "Scala",
"type": "experimental",
"mode": "standard",
"completeness": {
"routing": true,
"middleware": true,
"request": true,
"response": true
},
"engine": "Potateh",
"description": "Sharaf with one virtual thread per persistent connection, Squery-backed PostgreSQL, compression, and the supported HTTP/1 profiles.",
"repo": "https://github.com/sake92/sharaf",
"enabled": true,
"tests": [
"baseline",
"pipelined",
"limited-conn",
"async",
"json-comp",
"async-db",
"fortunes",
"latency-1m",
"latency-10k",
"latency-500k-8cpu"
],
"maintainers": ["sake92"]
}
1 change: 1 addition & 0 deletions frameworks/sharaf/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.13.0
1 change: 1 addition & 0 deletions frameworks/sharaf/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.4.1")
88 changes: 88 additions & 0 deletions frameworks/sharaf/src/main/scala/Database.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.net.URI

import com.zaxxer.hikari.{HikariConfig, HikariDataSource}

import ba.sake.squery.*
import ba.sake.squery.given
import ba.sake.squery.postgres.{*, given}
import ba.sake.squery.read.{*, given}
import ba.sake.squery.write.{*, given}

case class DbItem(
id: Int,
name: String,
category: String,
price: Long,
quantity: Long,
active: Boolean,
tags: Vector[String],
rating_score: Int,
rating_count: Int
) derives SqlReadRow

case class Fortune(id: Int, message: String) derives SqlReadRow

final class Database private (dataSource: HikariDataSource, context: SqueryContext) {
def items(min: Int, max: Int, limit: Int): Payload = {
val result = context.run {
sql"""
SELECT id, name, category, price, quantity, active,
ARRAY(SELECT jsonb_array_elements_text(tags)) AS tags,
rating_score, rating_count
FROM items
WHERE price BETWEEN $min AND $max
LIMIT ${math.min(math.max(limit, 1), 50)}
""".readRows[DbItem]()
}
val items = result.map { row =>
Item(
row.id,
row.name,
row.category,
row.price,
row.quantity,
row.active,
row.tags,
Rating(row.rating_score, row.rating_count)
)
}
Payload(items, items.size)
}

def fortunes(): Seq[Fortune] = context.run {
sql"SELECT id, message FROM fortune".readRows[Fortune]()
}

def close(): Unit = dataSource.close()
}

object Database {
def fromEnvironment(): Option[Database] = {
sys.env.get("DATABASE_URL").filter(_.nonEmpty).flatMap { url =>
try {
val dataSource = new HikariDataSource(hikariConfig(url))
Some(new Database(dataSource, SqueryContext(dataSource)))
} catch {
case error: Throwable =>
error.printStackTrace()
None
}
}
}

private def hikariConfig(databaseUrl: String): HikariConfig = {
val uri = URI.create(databaseUrl.replaceFirst("^postgres://", "postgresql://"))
val port = if (uri.getPort > 0) uri.getPort else 5432
val userInfo = Option(uri.getUserInfo).toSeq.flatMap(_.split(":", 2))
val maximumPoolSize = sys.env.get("DATABASE_MAX_CONN").flatMap(_.toIntOption).getOrElse(64)
val config = new HikariConfig()
config.setJdbcUrl(s"jdbc:postgresql://${uri.getHost}:$port${uri.getPath}")
config.setUsername(userInfo.headOption.getOrElse("bench"))
config.setPassword(userInfo.drop(1).headOption.getOrElse(""))
config.setMaximumPoolSize(maximumPoolSize)
config.setMinimumIdle(math.min(10, maximumPoolSize))
config.setReadOnly(true)
config.setPoolName("httparena-squery")
config
}
}
118 changes: 118 additions & 0 deletions frameworks/sharaf/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import java.net.InetSocketAddress
import java.util.concurrent.Executors

import ba.sake.potateh.ServerConfig
import ba.sake.querson.QueryStringRW
import ba.sake.sharaf.{*, given}
import ba.sake.sharaf.potateh.PotatehSharafServer
import ba.sake.tupson.*
import sttp.model.StatusCode

case class Rating(score: Int, count: Int) derives JsonRW
case class Item(
id: Int,
name: String,
category: String,
price: Long,
quantity: Long,
active: Boolean,
tags: Seq[String],
rating: Rating,
total: Long = 0
) derives JsonRW
case class Payload(items: Seq[Item], count: Int) derives JsonRW

case class SumQuery(a: Long = 0L, b: Long = 0L) derives QueryStringRW

object Main {
private val virtualThreads = Executors.newVirtualThreadPerTaskExecutor()
private val database = Database.fromEnvironment()

def main(args: Array[String]): Unit = {
val datasetPath = sys.env.getOrElse("DATASET_PATH", "/data/dataset.json")
val datasetJson = scala.util.Using.resource(scala.io.Source.fromFile(datasetPath))(_.mkString)
val dataset = datasetJson.parseJson[Seq[Item]]
val routes = Routes {
case GET -> Path("pipeline") => Response.withBody("ok")
case GET -> Path("baseline11") => Response.withBody(querySum.toString)
case POST -> Path("baseline11") =>
val body = Request.current.bodyString.trim.toLongOption.getOrElse(0L)
Response.withBody((querySum + body).toString)
case GET -> Path("json", count) =>
case class JsonQuery(m: Long = 1L) derives QueryStringRW
val multiplier = Request.current.queryParams[JsonQuery].m
val itemCount = count.toIntOption.getOrElse(0).max(0)
val items = dataset.take(itemCount).map { item =>
item.copy(total = item.price * item.quantity * multiplier)
}
val payload = Payload(items, items.size)
Response.withBody(payload)
case GET -> Path("delay", milliseconds) =>
val delay = milliseconds.toLongOption.getOrElse(0L).max(0L)
Thread.sleep(delay)
Response.withBody(delay.toString)
case GET -> Path("async-db") =>
case class DatabaseQuery(min: Int = 10, max: Int = 50, limit: Int = 50) derives QueryStringRW
database match {
case Some(db) =>
val query = Request.current.queryParams[DatabaseQuery]
Response.withBody(db.items(query.min, query.max, query.limit))
case None => databaseUnavailable()
}
case GET -> Path("fortunes") =>
database match {
case Some(db) => Response.withBody(renderFortunes(db.fortunes()))
case None => databaseUnavailable()
}
}
val config = ServerConfig.Default
.withBindAddress(new InetSocketAddress("0.0.0.0", 8080))
.withMaxConnections(32768)
.withBacklog(8192)
val routesHandler = SharafHandler.routes(routes)
val gzipHandler = SharafHandler.gzip(routesHandler, GzipSettings.default.withLevel(1))
val jsonCompression: SharafHandler = context =>
if context.params.path.segments.headOption.contains("json") then gzipHandler.handle(context)
else routesHandler.handle(context)
val handler = SharafHandler.exceptions(jsonCompression, ExceptionMapper.default)
val server = PotatehSharafServer.start(config, handler, virtualThreads)
Runtime.getRuntime.addShutdownHook(
new Thread(
() => {
server.stop()
database.foreach(_.close())
virtualThreads.shutdownNow()
()
},
"sharaf-potateh-shutdown"
)
)
}

private def querySum(using Request): Long = {
val query = Request.current.queryParams[SumQuery]
query.a + query.b
}

private def databaseUnavailable(): Response[String] = {
Response
.withStatus(StatusCode.InternalServerError)
.withBody("database unavailable")
}

private def renderFortunes(rows: Seq[Fortune]) = {
val allRows = rows
.appended(Fortune(0, "Additional fortune added at request time."))
.sortBy(_.message)
val renderedRows = allRows.map { fortune =>
html"<tr><td>${fortune.id}</td><td>${fortune.message}</td></tr>"
}
html"""
<!DOCTYPE html>
<html>
<head><title>Fortunes</title></head>
<body><table>${renderedRows}</table></body>
</html>
"""
}
}
Loading