Эх сурвалжийг харах

Add Swift server framework Hummingbird (#6427)

* Add hummingbird

* Update README
Adam Fowler 4 жил өмнө
parent
commit
682847703c

+ 22 - 0
frameworks/Swift/hummingbird/README.md

@@ -0,0 +1,22 @@
+# Hummingbird Benchmarking Test
+
+Hummingbird is a lightweight, flexible HTTP server framework written in Swift. It is a micro framework so there are no benchmarks for database access
+
+### Test Type Implementation Source Code
+
+* [JSON](src/Sources/server/main.swift)
+* [PLAINTEXT](src/Sources/server/main.swift)
+
+## Important Libraries
+This version of Hummingbird requires
+* [Swift 5.3](https://swift.org)  
+* [SwiftNIO 2.x](https://github.com/apple/swift-nio/)
+
+## Test URLs
+### JSON
+
+http://localhost:8080/json
+
+### PLAINTEXT
+
+http://localhost:8080/plaintext

+ 26 - 0
frameworks/Swift/hummingbird/benchmark_config.json

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

+ 29 - 0
frameworks/Swift/hummingbird/hummingbird.dockerfile

@@ -0,0 +1,29 @@
+# ================================
+# Build image
+# ================================
+FROM swift:5.3 as build
+WORKDIR /build
+
+# Copy entire repo into container
+COPY ./src .
+
+# Compile with optimizations
+RUN swift build \
+	--enable-test-discovery \
+	-c release
+
+# ================================
+# Run image
+# ================================
+FROM swift:5.3-slim
+WORKDIR /run
+
+# Copy build artifacts
+COPY --from=build /build/.build/release /run
+
+ENV SERVER_PORT=8080
+ENV SERVER_HOSTNAME=0.0.0.0
+
+EXPOSE 8080
+
+CMD ["./server"]

+ 28 - 0
frameworks/Swift/hummingbird/src/Package.swift

@@ -0,0 +1,28 @@
+// swift-tools-version:5.3
+// The swift-tools-version declares the minimum version of Swift required to build this package.
+
+import PackageDescription
+
+let package = Package(
+    name: "server",
+    products: [
+        .executable(name: "server", targets: ["server"])
+    ],
+    dependencies: [
+        .package(url: "https://github.com/hummingbird-project/hummingbird.git", .upToNextMinor(from: "0.6.0")),
+    ],
+    targets: [
+        .target(name: "server",
+            dependencies: [
+                .product(name: "Hummingbird", package: "hummingbird"),
+                .product(name: "HummingbirdFoundation", package: "hummingbird"),
+            ],
+            swiftSettings: [
+                // Enable better optimizations when building in Release configuration. Despite the use of
+                // the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
+                // builds. See <https://github.com/swift-server/guides#building-for-production> for details.
+                .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
+            ]
+        ),
+    ]
+)

+ 33 - 0
frameworks/Swift/hummingbird/src/Sources/server/main.swift

@@ -0,0 +1,33 @@
+import Hummingbird
+import HummingbirdFoundation
+
+struct Object: HBResponseEncodable {
+    let message: String
+}
+
+func runApp() {
+    let env = HBEnvironment()
+    let serverHostName = env.get("SERVER_HOSTNAME") ?? "127.0.0.1"
+    let serverPort = env.get("SERVER_PORT", as: Int.self) ?? 8080
+
+    let configuration = HBApplication.Configuration(
+        address: .hostname(serverHostName, port: serverPort),
+        serverName: "Hummingbird"
+    )
+    let app = HBApplication(configuration: configuration)
+    app.encoder = JSONEncoder()
+    app.middleware.add(HBDateResponseMiddleware(application: app))
+    
+    app.router.get("plaintext") { req in
+        "Hello, world!"
+    }
+
+    app.router.get("json") { req in
+        Object(message: "Hello, world!")
+    }
+
+    app.start()
+    app.wait()
+}
+
+runApp()