Skip to content

Commit

Permalink
Added everything else
Browse files Browse the repository at this point in the history
  • Loading branch information
theLastOfCats committed Jul 4, 2024
1 parent 41de3e2 commit 473cc79
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 5 deletions.
3 changes: 3 additions & 0 deletions frameworks/Swift/vapor/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
"plaintext_url": "/plaintext",
"json_url": "/json",
"db_url": "/db",
"query_url": "/queries?queries=",
"update_url": "/updates?queries=",
"fortune_url": "/fortunes",
"port": 8080,
"approach": "Realistic",
"classification": "Fullstack",
Expand Down
3 changes: 3 additions & 0 deletions frameworks/Swift/vapor/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ versus = "None"
urls.plaintext = "/plaintext"
urls.json = "/json"
urls.db = "/db"
urls.query = "/queries?queries="
urls.update = "/updates?queries="
urls.fortune = "/fortunes"
approach = "Realistic"
classification = "Fullstack"
database = "Postgres"
Expand Down
2 changes: 2 additions & 0 deletions frameworks/Swift/vapor/vapor-swifql-ikiga/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let package = Package(
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "4.99.3"),
.package(url: "https://github.com/vapor/leaf.git", from: "4.0.0"),
// 🔵 Non-blocking, event-driven networking for Swift. Used for custom executors
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
// json encoder/decoder
Expand All @@ -27,6 +28,7 @@ let package = Package(
name: "App",
dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "Leaf", package: "leaf"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "VaporBridges", package: "VaporBridges"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
import Vapor

extension World: Content {}
extension Fortune: Content {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extension Int {
func bounded(to range: ClosedRange<Int>) -> Int {
switch self {
case ...range.lowerBound:
return range.lowerBound
case range.upperBound...:
return range.upperBound
default:
return self
}
}
}

extension Int: Sequence {
public func makeIterator() -> CountableRange<Int>.Iterator {
return (0..<self).makeIterator()
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import Bridges
import SwifQL

final class Fortune: Table {
final class Fortune: Table, Schemable {
@Column("id")
var id: Int32?

@Column("message")
var message: String

init() {}
init(id: Int32, message: String) {
self.id = id
self.message = message
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head><title>Fortunes</title></head>
<body>
<table>
<tr><th>id</th><th>message</th></tr>
#for(planet in planets):<tr><td>#(fortune.id)</td><td>#(fortune.message)</td></tr>
#endfor</table>
</body>
</html>
50 changes: 47 additions & 3 deletions frameworks/Swift/vapor/vapor-swifql-ikiga/Sources/configure.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//import IkigaJSON

import FoundationPreview
import Leaf
import PostgresBridge
import Vapor
import VaporBridges
Expand All @@ -8,7 +9,7 @@ import VaporBridges
extension DatabaseHost {
public static var DbHost: DatabaseHost {
return .init(
hostname: "tfb-database",
hostname: "localhost",
port: 5432,
username: "benchmarkdbuser",
password: "benchmarkdbpassword",
Expand Down Expand Up @@ -37,6 +38,8 @@ public func configure(_ app: Application) throws {
app.logger.logLevel = .notice
app.logger.notice("💧 VAPOR")
app.logger.notice("System.coreCount: \(System.coreCount)")

app.views.use(.leaf)

try routes(app)

Expand Down Expand Up @@ -65,12 +68,53 @@ public func routes(_ app: Application) throws {
}

app.get("queries") { req async throws -> [World] in
let queries: Int = (req.query["queries"] ?? 1).bounded(to: 1...500)

var worlds: [World] = []

for _ in queries {
guard let world: World = try await req.postgres.connection(to: .Db, { conn in
World.select
.where(\World.$id == Int.random(in: 1...10_000))
.execute(on: conn)
.first(decoding: World.self)
}).get() else {
throw Abort(.notFound)
}

worlds.append(world)
}
return worlds
}

app.get("updates") { req async throws -> [World] in
let queries = (req.query["queries"] ?? 1).bounded(to: 1...500)

var worlds: [World] = []

for _ in queries {

let world = try await req.postgres.connection(to: .Db, { conn in
World.select.where(\World.$id == Int.random(in: 1...10_000)).execute(on: conn).first(decoding: World.self).flatMap { world in
world!.randomnumber = .random(in: 1...10_000)
return world!.update(on: \.$id, on: conn)
}
}).get()

worlds.append(world)
}

return worlds

}

app.get("fortunes") { req async throws -> View in
var fortunes: [Fortune] = try await req.postgres.connection(to: .Db, {conn in
Fortune.select.execute(on: conn).all(decoding: Fortune.self)
}).get()

fortunes.append(Fortune(id: 0, message: "Additional fortune added at request time."))

return try await req.view.render("fortune", fortunes)
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Vapor
import PostgresBridge
import IkigaJSON
import Logging

@main
Expand Down

0 comments on commit 473cc79

Please sign in to comment.