Skip to content

Add benchmarks for Metadata collection #1691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
timeout-minutes: 20
- name: Run Benchmarks
working-directory: ./Performance/Benchmarks
run: swift package benchmark baseline check --check-absolute-path Thresholds/${{ matrix.swift-version }}/
run: swift package benchmark baseline check --no-progress --check-absolute-path Thresholds/${{ matrix.swift-version }}/
timeout-minutes: 20
integration-tests:
strategy:
Expand Down
6 changes: 6 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ extension Product {
name: grpcProductName,
targets: [grpcTargetName]
)

static let grpcCore: Product = .library(
name: "_GRPCCore",
targets: ["GRPCCore"]
)

static let cgrpcZlib: Product = .library(
name: cgrpcZlibProductName,
Expand All @@ -490,6 +495,7 @@ let package = Package(
name: grpcPackageName,
products: [
.grpc,
.grpcCore,
.cgrpcZlib,
.protocGenGRPCSwift,
.grpcSwiftPlugin,
Expand Down
131 changes: 113 additions & 18 deletions Performance/Benchmarks/Benchmarks/GRPCSwiftBenchmark/Benchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,119 @@
* limitations under the License.
*/
import Benchmark
import Foundation
import GRPCCore

let benchmarks = {
Benchmark.defaultConfiguration = .init(
metrics: [
.mallocCountTotal,
.syscalls,
.readSyscalls,
.writeSyscalls,
.memoryLeaked,
.retainCount,
.releaseCount,
]
)

// async code is currently still quite flaky in the number of retain/release it does so we don't measure them today
var configWithoutRetainRelease = Benchmark.defaultConfiguration
configWithoutRetainRelease.metrics.removeAll(where: { $0 == .retainCount || $0 == .releaseCount })

// Add Benchmarks here
Benchmark.defaultConfiguration = .init(
metrics: [
.mallocCountTotal,
.syscalls,
.readSyscalls,
.writeSyscalls,
.memoryLeaked,
.retainCount,
.releaseCount,
]
)

// async code is currently still quite flaky in the number of retain/release it does so we don't measure them today
var configWithoutRetainRelease = Benchmark.defaultConfiguration
configWithoutRetainRelease.metrics.removeAll(where: { $0 == .retainCount || $0 == .releaseCount })

Benchmark("Metadata_Add_string") { benchmark in
for _ in benchmark.scaledIterations {
var metadata = Metadata()
for i in 0..<1000 {
metadata.addString("\(i)", forKey: "\(i)")
}
}
}

Benchmark("Metadata_Add_binary") { benchmark in
let value: [UInt8] = [1, 2, 3]
for _ in benchmark.scaledIterations {
var metadata = Metadata()

benchmark.startMeasurement()
for i in 0..<1000 {
metadata.addBinary(value, forKey: "\(i)")
}
benchmark.stopMeasurement()
}
}

Benchmark("Metadata_Remove_values_for_key") { benchmark in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I had originally (files with underscores, benchmark names with spaces) but the check against the thresholds would always pass even when it shouldn't - I could only make it fail once I started using underscores in the names too.

for _ in benchmark.scaledIterations {
var metadata = Metadata()
for i in 0..<1000 {
metadata.addString("value", forKey: "\(i)")
}

benchmark.startMeasurement()
for i in 0..<1000 {
metadata.removeAllValues(forKey: "\(i)")
}
benchmark.stopMeasurement()
}
}

Benchmark("Metadata_Iterate_all_values") { benchmark in
for _ in benchmark.scaledIterations {
var metadata = Metadata()
for i in 0..<1000 {
metadata.addString("value", forKey: "key")
}

benchmark.startMeasurement()
for value in metadata["key"] {
blackHole(value)
}
benchmark.stopMeasurement()
}
}

Benchmark("Metadata_Iterate_string_values") { benchmark in
for _ in benchmark.scaledIterations {
var metadata = Metadata()
for i in 0..<1000 {
metadata.addString("\(i)", forKey: "key")
}

benchmark.startMeasurement()
for value in metadata[stringValues: "key"] {
blackHole(value)
}
benchmark.stopMeasurement()
}
}

Benchmark("Metadata_Iterate_binary_values_when_only_binary_values_stored") { benchmark in
for _ in benchmark.scaledIterations {
var metadata = Metadata()
for i in 0..<1000 {
metadata.addBinary([1], forKey: "key")
}

benchmark.startMeasurement()
for value in metadata[binaryValues: "key"] {
blackHole(value)
}
benchmark.stopMeasurement()
}
}

Benchmark("Metadata_Iterate_binary_values_when_only_strings_stored") { benchmark in
for _ in benchmark.scaledIterations {
var metadata = Metadata()
for i in 0..<1000 {
metadata.addString("\(i)", forKey: "key")
}

benchmark.startMeasurement()
for value in metadata[binaryValues: "key"] {
blackHole(value)
}
benchmark.stopMeasurement()
}
}
}
2 changes: 1 addition & 1 deletion Performance/Benchmarks/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let package = Package(
name: "GRPCSwiftBenchmark",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "GRPC", package: "grpc-swift")
.product(name: "_GRPCCore", package: "grpc-swift")
],
path: "Benchmarks/GRPCSwiftBenchmark",
plugins: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 11,
"memoryLeaked" : 0,
"releaseCount" : 1011,
"retainCount" : 2000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 11,
"memoryLeaked" : 0,
"releaseCount" : 4012,
"retainCount" : 2000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1005,
"retainCount" : 1005,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1005,
"retainCount" : 1005,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 2000,
"memoryLeaked" : 0,
"releaseCount" : 4005,
"retainCount" : 2005,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1005,
"retainCount" : 1005,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 2002000,
"retainCount" : 1999000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 11,
"memoryLeaked" : 0,
"releaseCount" : 1011,
"retainCount" : 2000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 11,
"memoryLeaked" : 0,
"releaseCount" : 4012,
"retainCount" : 2000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1002,
"retainCount" : 1001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1002,
"retainCount" : 1001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 2000,
"memoryLeaked" : 0,
"releaseCount" : 4002,
"retainCount" : 2001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1002,
"retainCount" : 1001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 2002000,
"retainCount" : 1999000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 11,
"memoryLeaked" : 0,
"releaseCount" : 1011,
"retainCount" : 2000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 11,
"memoryLeaked" : 0,
"releaseCount" : 4012,
"retainCount" : 2000,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1002,
"retainCount" : 1001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1002,
"retainCount" : 1001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 2000,
"memoryLeaked" : 0,
"releaseCount" : 4002,
"retainCount" : 2001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 1002,
"retainCount" : 1001,
"syscalls" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mallocCountTotal" : 0,
"memoryLeaked" : 0,
"releaseCount" : 2002000,
"retainCount" : 1999000,
"syscalls" : 0
}