|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the swift-memcache-gsoc open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the swift-memcache-gsoc project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of swift-memcache-gsoc project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Benchmark |
| 16 | +import Memcache |
| 17 | +import NIOCore |
| 18 | +import NIOPosix |
| 19 | + |
| 20 | +func runSetRequest(iterations: Int, eventLoopGroup: EventLoopGroup) async throws { |
| 21 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 22 | + let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup) |
| 23 | + group.addTask { try await memcacheConnection.run() } |
| 24 | + |
| 25 | + let setValue = "bar" |
| 26 | + |
| 27 | + for _ in 0..<iterations { |
| 28 | + try await memcacheConnection.set("foo", value: setValue) |
| 29 | + } |
| 30 | + group.cancelAll() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func runSetWithTTLRequest(iterations: Int, eventLoopGroup: EventLoopGroup) async throws { |
| 35 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 36 | + |
| 37 | + let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup) |
| 38 | + group.addTask { try await memcacheConnection.run() } |
| 39 | + |
| 40 | + let setValue = "foo" |
| 41 | + let now = ContinuousClock.Instant.now |
| 42 | + let expirationTime = now.advanced(by: .seconds(90)) |
| 43 | + let timeToLive = TimeToLive.expiresAt(expirationTime) |
| 44 | + |
| 45 | + for _ in 0..<iterations { |
| 46 | + try await memcacheConnection.set("bar", value: setValue, timeToLive: timeToLive) |
| 47 | + } |
| 48 | + group.cancelAll() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func runDeleteRequest(iterations: Int, eventLoopGroup: EventLoopGroup) async throws { |
| 53 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 54 | + |
| 55 | + let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup) |
| 56 | + group.addTask { try await memcacheConnection.run() } |
| 57 | + |
| 58 | + let setValue = "foo" |
| 59 | + try await memcacheConnection.set("bar", value: setValue) |
| 60 | + |
| 61 | + for _ in 0..<iterations { |
| 62 | + try await memcacheConnection.delete("bar") |
| 63 | + } |
| 64 | + group.cancelAll() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func runIncrementRequest(iterations: Int, eventLoopGroup: EventLoopGroup) async throws { |
| 69 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 70 | + |
| 71 | + let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup) |
| 72 | + group.addTask { try await memcacheConnection.run() } |
| 73 | + |
| 74 | + let initialValue = 1 |
| 75 | + try await memcacheConnection.set("increment", value: initialValue) |
| 76 | + |
| 77 | + let incrementAmount = 100 |
| 78 | + for _ in 0..<iterations { |
| 79 | + try await memcacheConnection.increment("increment", amount: incrementAmount) |
| 80 | + } |
| 81 | + group.cancelAll() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func runDecrementRequest(iterations: Int, eventLoopGroup: EventLoopGroup) async throws { |
| 86 | + try await withThrowingTaskGroup(of: Void.self) { group in |
| 87 | + |
| 88 | + let memcacheConnection = MemcacheConnection(host: "memcached", port: 11211, eventLoopGroup: eventLoopGroup) |
| 89 | + group.addTask { try await memcacheConnection.run() } |
| 90 | + |
| 91 | + let initialValue = 100 |
| 92 | + try await memcacheConnection.set("decrement", value: initialValue) |
| 93 | + |
| 94 | + let decrementAmount = 10 |
| 95 | + for _ in 0..<iterations { |
| 96 | + try await memcacheConnection.decrement("decrement", amount: decrementAmount) |
| 97 | + } |
| 98 | + group.cancelAll() |
| 99 | + } |
| 100 | +} |
0 commit comments