Skip to content

Commit 765ff32

Browse files
authored
Add benchmarks for Metadata collection (#1691)
1 parent 1c19d3f commit 765ff32

File tree

25 files changed

+268
-20
lines changed

25 files changed

+268
-20
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ jobs:
108108
timeout-minutes: 20
109109
- name: Run Benchmarks
110110
working-directory: ./Performance/Benchmarks
111-
run: swift package benchmark baseline check --check-absolute-path Thresholds/${{ matrix.swift-version }}/
111+
run: swift package benchmark baseline check --no-progress --check-absolute-path Thresholds/${{ matrix.swift-version }}/
112112
timeout-minutes: 20
113113
integration-tests:
114114
strategy:

Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ extension Product {
467467
name: grpcProductName,
468468
targets: [grpcTargetName]
469469
)
470+
471+
static let grpcCore: Product = .library(
472+
name: "_GRPCCore",
473+
targets: ["GRPCCore"]
474+
)
470475

471476
static let cgrpcZlib: Product = .library(
472477
name: cgrpcZlibProductName,
@@ -490,6 +495,7 @@ let package = Package(
490495
name: grpcPackageName,
491496
products: [
492497
.grpc,
498+
.grpcCore,
493499
.cgrpcZlib,
494500
.protocGenGRPCSwift,
495501
.grpcSwiftPlugin,

Performance/Benchmarks/Benchmarks/GRPCSwiftBenchmark/Benchmarks.swift

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,119 @@
1414
* limitations under the License.
1515
*/
1616
import Benchmark
17-
import Foundation
17+
import GRPCCore
1818

1919
let benchmarks = {
20-
Benchmark.defaultConfiguration = .init(
21-
metrics: [
22-
.mallocCountTotal,
23-
.syscalls,
24-
.readSyscalls,
25-
.writeSyscalls,
26-
.memoryLeaked,
27-
.retainCount,
28-
.releaseCount,
29-
]
30-
)
31-
32-
// async code is currently still quite flaky in the number of retain/release it does so we don't measure them today
33-
var configWithoutRetainRelease = Benchmark.defaultConfiguration
34-
configWithoutRetainRelease.metrics.removeAll(where: { $0 == .retainCount || $0 == .releaseCount })
35-
36-
// Add Benchmarks here
20+
Benchmark.defaultConfiguration = .init(
21+
metrics: [
22+
.mallocCountTotal,
23+
.syscalls,
24+
.readSyscalls,
25+
.writeSyscalls,
26+
.memoryLeaked,
27+
.retainCount,
28+
.releaseCount,
29+
]
30+
)
31+
32+
// async code is currently still quite flaky in the number of retain/release it does so we don't measure them today
33+
var configWithoutRetainRelease = Benchmark.defaultConfiguration
34+
configWithoutRetainRelease.metrics.removeAll(where: { $0 == .retainCount || $0 == .releaseCount })
35+
36+
Benchmark("Metadata_Add_string") { benchmark in
37+
for _ in benchmark.scaledIterations {
38+
var metadata = Metadata()
39+
for i in 0..<1000 {
40+
metadata.addString("\(i)", forKey: "\(i)")
41+
}
42+
}
43+
}
44+
45+
Benchmark("Metadata_Add_binary") { benchmark in
46+
let value: [UInt8] = [1, 2, 3]
47+
for _ in benchmark.scaledIterations {
48+
var metadata = Metadata()
49+
50+
benchmark.startMeasurement()
51+
for i in 0..<1000 {
52+
metadata.addBinary(value, forKey: "\(i)")
53+
}
54+
benchmark.stopMeasurement()
55+
}
56+
}
57+
58+
Benchmark("Metadata_Remove_values_for_key") { benchmark in
59+
for _ in benchmark.scaledIterations {
60+
var metadata = Metadata()
61+
for i in 0..<1000 {
62+
metadata.addString("value", forKey: "\(i)")
63+
}
64+
65+
benchmark.startMeasurement()
66+
for i in 0..<1000 {
67+
metadata.removeAllValues(forKey: "\(i)")
68+
}
69+
benchmark.stopMeasurement()
70+
}
71+
}
72+
73+
Benchmark("Metadata_Iterate_all_values") { benchmark in
74+
for _ in benchmark.scaledIterations {
75+
var metadata = Metadata()
76+
for i in 0..<1000 {
77+
metadata.addString("value", forKey: "key")
78+
}
79+
80+
benchmark.startMeasurement()
81+
for value in metadata["key"] {
82+
blackHole(value)
83+
}
84+
benchmark.stopMeasurement()
85+
}
86+
}
87+
88+
Benchmark("Metadata_Iterate_string_values") { benchmark in
89+
for _ in benchmark.scaledIterations {
90+
var metadata = Metadata()
91+
for i in 0..<1000 {
92+
metadata.addString("\(i)", forKey: "key")
93+
}
94+
95+
benchmark.startMeasurement()
96+
for value in metadata[stringValues: "key"] {
97+
blackHole(value)
98+
}
99+
benchmark.stopMeasurement()
100+
}
101+
}
102+
103+
Benchmark("Metadata_Iterate_binary_values_when_only_binary_values_stored") { benchmark in
104+
for _ in benchmark.scaledIterations {
105+
var metadata = Metadata()
106+
for i in 0..<1000 {
107+
metadata.addBinary([1], forKey: "key")
108+
}
109+
110+
benchmark.startMeasurement()
111+
for value in metadata[binaryValues: "key"] {
112+
blackHole(value)
113+
}
114+
benchmark.stopMeasurement()
115+
}
116+
}
117+
118+
Benchmark("Metadata_Iterate_binary_values_when_only_strings_stored") { benchmark in
119+
for _ in benchmark.scaledIterations {
120+
var metadata = Metadata()
121+
for i in 0..<1000 {
122+
metadata.addString("\(i)", forKey: "key")
123+
}
124+
125+
benchmark.startMeasurement()
126+
for value in metadata[binaryValues: "key"] {
127+
blackHole(value)
128+
}
129+
benchmark.stopMeasurement()
130+
}
131+
}
37132
}

Performance/Benchmarks/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let package = Package(
3030
name: "GRPCSwiftBenchmark",
3131
dependencies: [
3232
.product(name: "Benchmark", package: "package-benchmark"),
33-
.product(name: "GRPC", package: "grpc-swift")
33+
.product(name: "_GRPCCore", package: "grpc-swift")
3434
],
3535
path: "Benchmarks/GRPCSwiftBenchmark",
3636
plugins: [
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mallocCountTotal" : 11,
3+
"memoryLeaked" : 0,
4+
"releaseCount" : 1011,
5+
"retainCount" : 2000,
6+
"syscalls" : 0
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mallocCountTotal" : 11,
3+
"memoryLeaked" : 0,
4+
"releaseCount" : 4012,
5+
"retainCount" : 2000,
6+
"syscalls" : 0
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mallocCountTotal" : 0,
3+
"memoryLeaked" : 0,
4+
"releaseCount" : 1005,
5+
"retainCount" : 1005,
6+
"syscalls" : 0
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mallocCountTotal" : 0,
3+
"memoryLeaked" : 0,
4+
"releaseCount" : 1005,
5+
"retainCount" : 1005,
6+
"syscalls" : 0
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mallocCountTotal" : 2000,
3+
"memoryLeaked" : 0,
4+
"releaseCount" : 4005,
5+
"retainCount" : 2005,
6+
"syscalls" : 0
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"mallocCountTotal" : 0,
3+
"memoryLeaked" : 0,
4+
"releaseCount" : 1005,
5+
"retainCount" : 1005,
6+
"syscalls" : 0
7+
}

0 commit comments

Comments
 (0)