Skip to content

Commit 60854f3

Browse files
developing in this project breaks code completion some reason...
- corrected some dynamic benchmark implementations - added dynamic unit test
1 parent 2817cef commit 60854f3

File tree

13 files changed

+203
-197
lines changed

13 files changed

+203
-197
lines changed

Benchmarks/Benchmarks/Elementary/Elementary.swift

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,7 @@ package struct ElementaryTests : HTMLGenerator {
1515
StaticView().render()
1616
}
1717
package func dynamicHTML(_ context: HTMLContext) -> String {
18-
html {
19-
body {
20-
h1 { context.heading }
21-
div(attributes: [.id(context.desc_id)]) {
22-
p { context.string }
23-
}
24-
h2 { context.user.details_heading }
25-
h3 { context.user.qualities_heading }
26-
ul(attributes: [.id(context.user.qualities_id)]) {
27-
for quality in context.user.qualities {
28-
li { quality }
29-
}
30-
}
31-
}
32-
}.render()
18+
DynamicView(context: context).render()
3319
}
3420
}
3521

@@ -42,4 +28,28 @@ struct StaticView : HTMLDocument {
4228
var body : some HTML {
4329
h1 { "Swift HTML Benchmarks" }
4430
}
31+
}
32+
33+
struct DynamicView : HTMLDocument {
34+
var title:String = "DynamicView"
35+
36+
let context:HTMLContext
37+
38+
var head : some HTML {
39+
""
40+
}
41+
42+
var body : some HTML {
43+
h1 { context.heading }
44+
div(attributes: [.id(context.desc_id)]) {
45+
p { context.string }
46+
}
47+
h2 { context.user.details_heading }
48+
h3 { context.user.qualities_heading }
49+
ul(attributes: [.id(context.user.qualities_id)]) {
50+
for quality in context.user.qualities {
51+
li { quality }
52+
}
53+
}
54+
}
4555
}

Benchmarks/Benchmarks/Networking/Networking.swift

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// main.swift
3+
//
4+
//
5+
// Created by Evan Anderson on 10/10/24.
6+
//
7+
8+
import HTTPTypes
9+
10+
import Utilities
11+
12+
import TestElementary
13+
import TestPlot
14+
import TestSwiftHTMLBB
15+
import TestSwiftHTMLKit
16+
import TestSwiftHTMLPF
17+
import TestSwim
18+
import TestToucan
19+
import TestVaporHTMLKit
20+
import TestVaux
21+
22+
23+
let libraries:[String:HTMLGenerator] = [
24+
"BinaryBirds" : SwiftHTMLBBTests(),
25+
"Elementary" : ElementaryTests(),
26+
"Plot" : PlotTests(),
27+
"Pointfreeco" : SwiftHTMLPFTests(),
28+
"SwiftHTMLKit" : SwiftHTMLKitTests(),
29+
"Swim" : SwimTests(),
30+
"VaporHTMLKit" : VaporHTMLKitTests(),
31+
"Vaux" : VauxTests()
32+
]
33+
34+
// hummingbird
35+
import Hummingbird
36+
37+
struct HeaderMiddleware<Context: RequestContext> : RouterMiddleware {
38+
let output:String
39+
40+
init(_ output: String = "Content-Type") {
41+
self.output = output
42+
}
43+
func handle(_ request: Request, context: Context, next: (Request, Context) async throws -> Response) async throws -> Response {
44+
var response = try await next(request, context)
45+
response.headers[HTTPField.Name(output)!] = "text/html"
46+
return response
47+
}
48+
}
49+
50+
let router = Hummingbird.Router()
51+
router.middlewares.add(HeaderMiddleware())
52+
for (library, value) in libraries {
53+
router.get(RouterPath(library)) { request, _ -> String in
54+
return value.staticHTML()
55+
}
56+
router.get(RouterPath("d" + library)) { request, _ -> String in
57+
return value.dynamicHTML(HTMLContext())
58+
}
59+
}
60+
let app:Hummingbird.Application = Hummingbird.Application(router: router, configuration: .init(address: .hostname("127.0.0.1", port: 8080)))
61+
try await app.runService()
62+
63+
/*
64+
// vapor
65+
import Vapor
66+
67+
*/

Benchmarks/Benchmarks/Plot/Plot.swift

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,18 @@ package struct PlotTests : HTMLGenerator {
2222
).render()
2323
}
2424
package func dynamicHTML(_ context: Utilities.HTMLContext) -> String {
25-
let context:Context = Context(context)
2625
return HTML(
26+
.head(
27+
.element(named: "title", text: "DynamicView")
28+
),
2729
.body(
28-
.component(context.heading),
29-
.component(context.desc),
30-
.component(context.details_heading),
31-
.component(context.qualities_heading),
32-
.component(context.qualities)
30+
.component(H1(context.heading)),
31+
.component(Div(Paragraph(context.string)).id(context.desc_id)),
32+
.component(H2(context.user.details_heading)),
33+
.component(H3(context.user.qualities_heading)),
34+
.component(List(context.user.qualities).id(context.user.qualities_id))
3335
)
3436
)
3537
.render()
3638
}
37-
}
38-
39-
struct Context {
40-
let heading:any Component
41-
let desc:any Component
42-
let details_heading:any Component
43-
let qualities_heading:any Component
44-
let qualities:any Component
45-
46-
init(_ context: Utilities.HTMLContext) {
47-
heading = H1(context.heading)
48-
desc = Div(Paragraph(context.string).id(context.desc_id))
49-
details_heading = H2(context.user.details_heading)
50-
qualities_heading = H3(context.user.qualities_heading)
51-
qualities = List(context.user.qualities).id(context.user.qualities_id)
52-
}
5339
}

Benchmarks/Benchmarks/Run/main.swift

Lines changed: 0 additions & 19 deletions
This file was deleted.

Benchmarks/Benchmarks/SwiftHTMLBB/SwiftHTMLBB.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ package struct SwiftHTMLBBTests : HTMLGenerator {
3030
package func dynamicHTML(_ context: HTMLContext) -> String {
3131
renderer.render(Document(.html) {
3232
Html {
33+
Head {
34+
Title("DynamicView")
35+
}
3336
Body {
3437
H1(context.heading)
3538
Div {

Benchmarks/Benchmarks/SwiftHTMLKit/SwiftHTMLKit.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ package struct SwiftHTMLKitTests : HTMLGenerator {
1212
package init() {}
1313

1414
package func staticHTML() -> String {
15-
#html(
16-
#head(
17-
#title("StaticView")
18-
),
19-
#body(
20-
#h1("Swift HTML Benchmarks")
21-
)
22-
)
15+
#html([
16+
#head([
17+
#title(["StaticView"])
18+
]),
19+
#body([
20+
#h1(["Swift HTML Benchmarks"])
21+
])
22+
])
2323
}
2424
// performance notes
2525
// - maping makes unneccessary copies and hurts throughput
@@ -30,20 +30,23 @@ package struct SwiftHTMLKitTests : HTMLGenerator {
3030
package func dynamicHTML(_ context: HTMLContext) -> String {
3131
var qualities:String = ""
3232
for quality in context.user.qualities {
33-
qualities += #li("\(quality)")
33+
qualities += #li(["\(quality)"])
3434
//qualities += "<li>" + quality + "</li>"
3535
}
3636
//return "<!DOCTYPE html><html><body><h1>" + context.heading + "</h1><div id=\"" + context.desc_id + "\"><p>" + context.string + "</p></div><h2>" + context.user.details_heading + "</h2><h3>" + context.user.qualities_heading + "</h3><ul id=\"" + context.user.qualities_id + "\">" + qualities + "</ul></body></html>"
37-
return #html(
38-
#body(
39-
#h1("\(context.heading)"),
40-
#div(attributes: [.id(context.desc_id)],
41-
#p("\(context.string)")
42-
),
43-
#h2("\(context.user.details_heading)"),
44-
#h3("\(context.user.qualities_heading)"),
45-
#ul(attributes: [.id(context.user.qualities_id)], "\(qualities)")
46-
)
47-
)
37+
return #html([
38+
#head([
39+
#title(["DynamicView"])
40+
]),
41+
#body([
42+
#h1(["\(context.heading)"]),
43+
#div(attributes: [.id(context.desc_id)], [
44+
#p(["\(context.string)"])
45+
]),
46+
#h2(["\(context.user.details_heading)"]),
47+
#h3(["\(context.user.qualities_heading)"]),
48+
#ul(attributes: [.id(context.user.qualities_id)], ["\(qualities)"])
49+
])
50+
])
4851
}
4952
}

Benchmarks/Benchmarks/SwiftHTMLPF/SwiftHTMLPF.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ package struct SwiftHTMLPFTests : HTMLGenerator {
2828
render(
2929
.document(
3030
.html(
31+
.head(
32+
.title("DynamicView")
33+
),
3134
.body(
3235
.h1(.raw(context.heading)),
3336
.div(attributes: [.id(context.desc_id)], .p(.raw(context.string))),

Benchmarks/Benchmarks/Swim/Swim.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ extension Node {
1212
var rendered : String {
1313
switch self {
1414
case .element(let name, let attributes, let child):
15-
let attributes_string:String = attributes.isEmpty ? "" : " " + attributes.map({ $0 + "=\"" + $1 + "\"" }).joined(separator: " ")
15+
var attributes_string:String = ""
16+
for (key, value) in attributes {
17+
attributes_string += " " + key + "=\"" + value + "\""
18+
}
1619
return (name == "html" ? "<!DOCTYPE html>" : "") + "<" + name + attributes_string + ">" + (child?.rendered ?? "") + "</" + name + ">"
1720
case .text(let string): return string
1821
case .raw(let string): return string
1922
case .comment(_): return ""
2023
case .documentType(let string): return string
21-
case .fragment(let children): return children.map({ $0.rendered }).joined()
24+
case .fragment(let children):
25+
var string:String = ""
26+
for child in children {
27+
string += child.rendered
28+
}
29+
return string
2230
case .trim: return ""
2331
}
2432
}
@@ -45,6 +53,9 @@ package struct SwimTests : HTMLGenerator {
4553
test.append(li { quality } )
4654
}
4755
return html {
56+
head {
57+
title { "DynamicView" }
58+
}
4859
body {
4960
h1 { context.heading }
5061
div(id: context.desc_id) {

0 commit comments

Comments
 (0)