Skip to content
8 changes: 8 additions & 0 deletions Benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
129 changes: 129 additions & 0 deletions Benchmarks/Benchmarks/MemcacheBenchmarks/MemcacheBenchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-memcache-gsoc open source project
//
// Copyright (c) 2023 Apple Inc. and the swift-memcache-gsoc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-memcache-gsoc project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import Foundation
import Memcache
import NIOCore
import NIOPosix

let benchmarks = {
let defaultMetrics: [BenchmarkMetric] = [
.mallocCountTotal,
.mallocCountLarge,
.mallocCountTotal,
.memoryLeaked,
.allocatedResidentMemory,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think for now only mallocCountTotal is fine

]

Benchmark("Set Request", configuration: .init(metrics: defaultMetrics)) { benchmark in
try await withThrowingTaskGroup(of: Void.self) { group in
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we create a separate file where we have those benchmarks as methods. Similar to what we do in certificates and nio?


let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's pass the EL to those new functions and let's statically initialize an EL outside of the let benchmarks. Take a look at what we do in NIO for it.

let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup)

group.addTask { try await memcacheConnection.run() }

let setValue = "bar"
try await memcacheConnection.set("foo", value: setValue)

for _ in benchmark.scaledIterations {
let getValue: String? = try await memcacheConnection.get("foo")
assert(getValue == setValue, "Value retrieved from Memcache does not match the set value")
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have to assert here.

}

group.cancelAll()
}
}

Benchmark("Set with TTL Request", configuration: .init(metrics: defaultMetrics)) { benchmark in
try await withThrowingTaskGroup(of: Void.self) { group in

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup)

group.addTask { try await memcacheConnection.run() }

let setValue = "foo"
let now = ContinuousClock.Instant.now
let expirationTime = now.advanced(by: .seconds(90))
let timeToLive = TimeToLive.expiresAt(expirationTime)
try await memcacheConnection.set("bar", value: setValue, timeToLive: timeToLive)

for _ in benchmark.scaledIterations {
let getValue: String? = try await memcacheConnection.get("foo")
assert(getValue == setValue, "Value retrieved from Memcache does not match the set value")
}

group.cancelAll()
}
}

Benchmark("Delete Request", configuration: .init(metrics: defaultMetrics)) { benchmark in
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comments for the other benchmarks

try await withThrowingTaskGroup(of: Void.self) { group in
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup)

group.addTask { try await memcacheConnection.run() }
let setValue = "foo"
try await memcacheConnection.set("bar", value: setValue)

for _ in benchmark.scaledIterations {
try await memcacheConnection.delete("bar")
}

group.cancelAll()
}
}

Benchmark("Increment Request", configuration: .init(metrics: defaultMetrics)) { benchmark in
try await withThrowingTaskGroup(of: Void.self) { group in
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup)

group.addTask { try await memcacheConnection.run() }
let initialValue = 1
try await memcacheConnection.set("increment", value: initialValue)

for _ in benchmark.scaledIterations {
let incrementAmount = 100
try await memcacheConnection.increment("increment", amount: incrementAmount)

let newValue: Int? = try await memcacheConnection.get("increment")
assert(newValue == initialValue + incrementAmount, "Incremented value is incorrect")
}
group.cancelAll()
}
}

Benchmark("Decrement Request", configuration: .init(metrics: defaultMetrics)) { benchmark in
try await withThrowingTaskGroup(of: Void.self) { group in
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup)

group.addTask { try await memcacheConnection.run() }
let initialValue = 100
try await memcacheConnection.set("decrement", value: initialValue)

for _ in benchmark.scaledIterations {
let decrementAmount = 10
try await memcacheConnection.decrement("decrement", amount: decrementAmount)

let newValue: Int? = try await memcacheConnection.get("decrement")
assert(newValue == initialValue - decrementAmount, "decrement value is incorrect")
}
group.cancelAll()
}
}
}
52 changes: 52 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// swift-tools-version: 5.7
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCertificates open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import PackageDescription

let package = Package(
name: "Benchmarks",
platforms: [
.macOS(.v13),
],
dependencies: [
.package(name: "swift-memcache-gsoc", path: "../"),
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.11.1"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.56.0"),
],
targets: [
.target(
name: "Benchmarks"),
.testTarget(
name: "BenchmarksTests",
dependencies: ["Benchmarks"]
),
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need those two targets

]
)
// Benchmark of MemcacheBenchmarks
package.targets += [
.executableTarget(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you just put this above instead of doing the += dance

name: "MemcacheBenchmarks",
dependencies: [
.product(name: "Memcache", package: "swift-memcache-gsoc"),
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
],
path: "Benchmarks/MemcacheBenchmarks",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark"),
]
),
]
13 changes: 13 additions & 0 deletions Benchmarks/Sources/Benchmarks/Benchmarks.swift
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need this

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-memcache-gsoc open source project
//
// Copyright (c) 2023 Apple Inc. and the swift-memcache-gsoc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-memcache-gsoc project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
26 changes: 26 additions & 0 deletions Benchmarks/Tests/BenchmarksTests/BenchmarksTests.swift
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-memcache-gsoc open source project
//
// Copyright (c) 2023 Apple Inc. and the swift-memcache-gsoc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-memcache-gsoc project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

@testable import Benchmarks
import XCTest

final class BenchmarksTests: XCTestCase {
func testExample() throws {
// XCTest Documentation
// https://developer.apple.com/documentation/xctest

// Defining Test Cases and Test Methods
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 148242431,
"mallocCountLarge" : 3,
"mallocCountTotal" : 68,
"memoryLeaked" : 516351
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 162267135,
"mallocCountLarge" : 3,
"mallocCountTotal" : 64,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 147849215,
"mallocCountLarge" : 3,
"mallocCountTotal" : 69,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 160432127,
"mallocCountLarge" : 3,
"mallocCountTotal" : 69,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 165019647,
"mallocCountLarge" : 3,
"mallocCountTotal" : 75,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 141950975,
"mallocCountLarge" : 3,
"mallocCountTotal" : 65,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 161480703,
"mallocCountLarge" : 3,
"mallocCountTotal" : 62,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 149422079,
"mallocCountLarge" : 3,
"mallocCountTotal" : 64,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 170393599,
"mallocCountLarge" : 3,
"mallocCountTotal" : 62,
"memoryLeaked" : 520447
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 167772159,
"mallocCountLarge" : 3,
"mallocCountTotal" : 63,
"memoryLeaked" : 520447
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 149159935,
"mallocCountLarge" : 3,
"mallocCountTotal" : 73,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 169476095,
"mallocCountLarge" : 3,
"mallocCountTotal" : 62,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 152436735,
"mallocCountLarge" : 3,
"mallocCountTotal" : 86,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 162660351,
"mallocCountLarge" : 3,
"mallocCountTotal" : 71,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 168296447,
"mallocCountLarge" : 3,
"mallocCountTotal" : 64,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 147062783,
"mallocCountLarge" : 3,
"mallocCountTotal" : 73,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 167772159,
"mallocCountLarge" : 3,
"mallocCountTotal" : 65,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 147849215,
"mallocCountLarge" : 3,
"mallocCountTotal" : 84,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 161742847,
"mallocCountLarge" : 3,
"mallocCountTotal" : 74,
"memoryLeaked" : 528895
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"allocatedResidentMemory" : 166330367,
"mallocCountLarge" : 3,
"mallocCountTotal" : 66,
"memoryLeaked" : 528895
}
Loading