forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarks.swift
More file actions
191 lines (161 loc) · 4.52 KB
/
Benchmarks.swift
File metadata and controls
191 lines (161 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import JavaScriptKit
import JavaScriptFoundationCompat
import Foundation
class Benchmark {
init(_ title: String) {
self.title = title
}
let title: String
func testSuite(_ name: String, _ body: @escaping () -> Void) {
let jsBody = JSClosure { arguments -> JSValue in
body()
return .undefined
}
try! benchmarkRunner("\(title)/\(name)", jsBody)
}
}
@JS enum APIResult {
case success(String)
case failure(Int)
case flag(Bool)
case rate(Float)
case precise(Double)
case info
}
@JS class EnumRoundtrip {
@JS init() {}
@JS func take(_ value: APIResult) {}
@JS func makeSuccess() -> APIResult {
return .success("Hello, world")
}
@JS func makeFailure() -> APIResult {
return .failure(42)
}
@JS func makeFlag() -> APIResult {
return .flag(true)
}
@JS func makeRate() -> APIResult {
return .rate(0.5)
}
@JS func makePrecise() -> APIResult {
return .precise(0.5)
}
@JS func makeInfo() -> APIResult {
return .info
}
@JS func roundtrip(_ result: APIResult) -> APIResult {
return result
}
}
@JS
enum ComplexResult {
case success(String)
case error(String, Int)
case location(Double, Double, String)
case status(Bool, Int, String)
case coordinates(Double, Double, Double)
case comprehensive(Bool, Bool, Int, Int, Double, Double, String, String, String)
case info
}
@JS class ComplexResultRoundtrip {
@JS init() {}
@JS func take(_ value: ComplexResult) {}
@JS func makeSuccess() -> ComplexResult {
return .success("Hello, world")
}
@JS func makeError() -> ComplexResult {
return .error("Network timeout", 503)
}
@JS func makeLocation() -> ComplexResult {
return .location(37.7749, -122.4194, "San Francisco")
}
@JS func makeStatus() -> ComplexResult {
return .status(true, 200, "OK")
}
@JS func makeCoordinates() -> ComplexResult {
return .coordinates(10.5, 20.3, 30.7)
}
@JS func makeComprehensive() -> ComplexResult {
return .comprehensive(true, false, 42, 100, 3.14, 2.718, "Hello", "World", "Test")
}
@JS func makeInfo() -> ComplexResult {
return .info
}
@JS func roundtrip(_ result: ComplexResult) -> ComplexResult {
return result
}
}
@JS class StringRoundtrip {
@JS init() {}
@JS func take(_ value: String) {}
@JS func make() -> String {
return "Hello, world"
}
}
@JS func run() {
let call = Benchmark("Call")
call.testSuite("JavaScript function call through Wasm import") {
for _ in 0..<20_000_000 {
try! benchmarkHelperNoop()
}
}
call.testSuite("JavaScript function call through Wasm import with int") {
for _ in 0..<10_000_000 {
try! benchmarkHelperNoopWithNumber(42)
}
}
let propertyAccess = Benchmark("Property access")
do {
let swiftInt: Double = 42
let object = JSObject()
object.jsNumber = JSValue.number(swiftInt)
propertyAccess.testSuite("Write Number") {
for _ in 0..<1_000_000 {
object.jsNumber = JSValue.number(swiftInt)
}
}
}
do {
let object = JSObject()
object.jsNumber = JSValue.number(42)
propertyAccess.testSuite("Read Number") {
for _ in 0..<1_000_000 {
_ = object.jsNumber.number
}
}
}
do {
let swiftString = "Hello, world"
let object = JSObject()
object.jsString = swiftString.jsValue
propertyAccess.testSuite("Write String") {
for _ in 0..<1_000_000 {
object.jsString = swiftString.jsValue
}
}
}
do {
let object = JSObject()
object.jsString = JSValue.string("Hello, world")
propertyAccess.testSuite("Read String") {
for _ in 0..<1_000_000 {
_ = object.jsString.string
}
}
}
do {
let conversion = Benchmark("Conversion")
let data = Data(repeating: 0, count: 10_000)
conversion.testSuite("Data to JSTypedArray") {
for _ in 0..<1_000_000 {
_ = data.jsTypedArray
}
}
let uint8Array = data.jsTypedArray
conversion.testSuite("JSTypedArray to Data") {
for _ in 0..<1_000_000 {
_ = Data.construct(from: uint8Array)
}
}
}
}