Browse Source

Add SNUnit: Scala Native micro framework (#6225)

* Add SNUnit: Scala Native micro framework

SNUnit is a Scala Native HTTP library built on top of NGINX Unit
external process interface. It allows users to build binaries that
can be run by NGINX Unit to serve HTTP requests.

* Remove unused string interpolator

* Make Unit Nginx a platfrom instead of webserver

This aligns SNUNit to the same convention used by the
php unit benchmark
Lorenzo Gabriele 4 years ago
parent
commit
c96f78d9bc

+ 1 - 0
.travis.yml

@@ -87,6 +87,7 @@ env:
     - 'TESTDIR="Scala/akka-http Scala/blaze Scala/cask Scala/colossus Scala/finagle"'
     - 'TESTDIR="Scala/finatra Scala/finch Scala/http4s"'
     - 'TESTDIR="Scala/play2-scala Scala/scalene Scala/youi"'
+    - 'TESTDIR="Scala/snunit"'
     - 'TESTDIR="Scala/vertx-web-scala"'
     - "TESTLANG=Scheme"
     - "TESTLANG=Swift"

+ 9 - 0
frameworks/Scala/snunit/.dockerignore

@@ -0,0 +1,9 @@
+# Ignore everything
+**
+
+# Allow files and directories
+!/build.sbt
+!/config.json
+!/project/build.properties
+!/project/plugins.sbt
+!/src

+ 2 - 0
frameworks/Scala/snunit/.gitignore

@@ -0,0 +1,2 @@
+.bsp
+/lowered.hnir

+ 25 - 0
frameworks/Scala/snunit/README.md

@@ -0,0 +1,25 @@
+# SNUnit Benchmarking Test
+
+## Test Type Implementation Source Code
+
+* [JSON](src/main/scala/Main.scala)
+* [PLAINTEXT](src/main/scala/Main.scala)
+
+## Infrastructure Software Versions
+
+The tests will be run with:
+
+* [SNUnit](https://github.com/lolgab/snunit)
+* [Scala Native](https://github.com/scala-native/scala-native)
+* [NGINX Unit](https://unit.nginx.org)
+* [upickle](https://github.com/lihaoyi/upickle)
+
+## Test URLs
+
+### JSON
+
+http://localhost:8080/json
+
+### PLAINTEXT
+
+http://localhost:8080/plaintext

+ 26 - 0
frameworks/Scala/snunit/benchmark_config.json

@@ -0,0 +1,26 @@
+{
+  "framework": "snunit",
+  "tests": [
+    {
+      "default": {
+        "json_url": "/json",
+        "plaintext_url": "/plaintext",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Micro",
+        "database": "None",
+        "framework": "SNUnit",
+        "language": "Scala",
+        "flavor": "Native",
+        "orm": "None",
+        "platform": "Unit Nginx",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "SNUnit",
+        "notes": "",
+        "versus": ""
+      }
+    }
+  ]
+}

+ 12 - 0
frameworks/Scala/snunit/build.sbt

@@ -0,0 +1,12 @@
+scalaVersion := "2.11.12"
+
+libraryDependencies ++= Seq(
+  "com.github.lolgab" %%% "snunit" % "0.0.2",
+  "com.github.lolgab" %%% "snunit-async" % "0.0.2",
+  "com.lihaoyi" %%% "upickle" % "1.2.2"
+)
+
+nativeMode := "release-full"
+nativeLTO := "thin"
+
+enablePlugins(ScalaNativePlugin)

+ 15 - 0
frameworks/Scala/snunit/config.json

@@ -0,0 +1,15 @@
+{
+  "listeners": {
+    "*:8080": {
+      "pass": "applications/example"
+    }
+  },
+  "applications": {
+    "example": {
+      "type": "external",
+      "working_directory": "/app",
+      "executable": "example",
+      "processes": 30
+    }
+  }
+}

+ 1 - 0
frameworks/Scala/snunit/project/build.properties

@@ -0,0 +1 @@
+sbt.version=1.4.5

+ 1 - 0
frameworks/Scala/snunit/project/plugins.sbt

@@ -0,0 +1 @@
+addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.0-M2")

+ 25 - 0
frameworks/Scala/snunit/snunit.dockerfile

@@ -0,0 +1,25 @@
+FROM debian:buster as builder
+
+RUN apt-get update && apt-get install -y curl gnupg && \
+  echo "deb https://dl.bintray.com/sbt/debian /" > /etc/apt/sources.list.d/sbt.list && \
+  curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | apt-key add - && \
+  curl -sL https://nginx.org/keys/nginx_signing.key | apt-key add - && \
+  echo "deb https://packages.nginx.org/unit/debian/ buster unit" > /etc/apt/sources.list.d/unit.list && \
+  echo "deb-src https://packages.nginx.org/unit/debian/ buster unit" >> /etc/apt/sources.list.d/unit.list && \
+  apt-get update && apt-get install -y clang unit-dev libuv1-dev openjdk-11-jdk sbt && \
+  apt-get purge -y gnupg
+
+WORKDIR /workdir
+
+COPY . .
+
+RUN sbt nativeLink
+
+FROM nginx/unit:1.21.0-minimal
+
+RUN apt-get update && apt-get install -y libuv1
+
+COPY /config.json /docker-entrypoint.d/
+COPY --from=builder /workdir/target/scala-2.11/workdir-out /app/example
+
+EXPOSE 8080

+ 35 - 0
frameworks/Scala/snunit/src/main/scala/Main.scala

@@ -0,0 +1,35 @@
+import snunit._
+import upickle.default._
+
+case class Message(message: String)
+
+object Message {
+  implicit val messageRW: ReadWriter[Message] = macroRW[Message]
+}
+
+object Main {
+  def main(args: Array[String]): Unit = {
+    AsyncServerBuilder()
+      .withRequestHandler(req =>
+        if (req.method == Method.GET && req.path == "/plaintext")
+          req.send(
+            statusCode = 200,
+            content = "Hello, World!",
+            headers = Seq("Content-Type" -> "text/plain")
+          )
+        else if (req.method == Method.GET && req.path == "/json")
+          req.send(
+            statusCode = 200,
+            content = stream(Message("Hello, World!")),
+            headers = Seq.empty
+          )
+        else
+          req.send(
+            statusCode = 404,
+            content = "Not found",
+            headers = Seq("Content-Type" -> "text/plain")
+          )
+      )
+      .build()
+  }
+}