|
| 1 | +import Benchmark |
| 2 | +import NIO |
| 3 | +import AsyncDataLoader |
| 4 | +import DataLoader |
| 5 | + |
| 6 | +let eventLoopGroup = MultiThreadedEventLoopGroup.singleton |
| 7 | + |
| 8 | +let benchmarks: @Sendable () -> Void = { |
| 9 | + |
| 10 | + // MARK: Async |
| 11 | + |
| 12 | + let nonBatchingLoader = DataLoader<Int, Int>( |
| 13 | + options: .init( |
| 14 | + batchingEnabled: true, |
| 15 | + cachingEnabled: false, |
| 16 | + ) |
| 17 | + ) { keys in |
| 18 | + keys.map { DataLoaderValue.success($0) } |
| 19 | + } |
| 20 | + |
| 21 | + let batchingLoader = DataLoader<Int, Int>( |
| 22 | + options: .init( |
| 23 | + batchingEnabled: true, |
| 24 | + cachingEnabled: false, |
| 25 | + maxBatchSize: 10 |
| 26 | + ) |
| 27 | + ) { keys in |
| 28 | + keys.map { DataLoaderValue.success($0) } |
| 29 | + } |
| 30 | + |
| 31 | + Benchmark("loadNonBatching") { _ in |
| 32 | + try await withThrowingTaskGroup { group in |
| 33 | + for i in (0..<1_000) { |
| 34 | + group.addTask { |
| 35 | + try await nonBatchingLoader.load(key: i) |
| 36 | + } |
| 37 | + } |
| 38 | + try await group.waitForAll() |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + Benchmark("loadBatching") { _ in |
| 43 | + try await withThrowingTaskGroup { group in |
| 44 | + for i in (0..<1_000) { |
| 45 | + group.addTask { |
| 46 | + try await batchingLoader.load(key: i) |
| 47 | + } |
| 48 | + } |
| 49 | + try await group.waitForAll() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Benchmark("loadBatchingMany") { _ in |
| 54 | + let result = try await batchingLoader.loadMany(keys: Array(0..<1_000)) |
| 55 | + } |
| 56 | + |
| 57 | + // MARK: NIO |
| 58 | + |
| 59 | + let nioNonBatchingLoader = DataLoader<Int, Int>( |
| 60 | + options: .init( |
| 61 | + batchingEnabled: true, |
| 62 | + cachingEnabled: false, |
| 63 | + ) |
| 64 | + ) { keys in |
| 65 | + return eventLoopGroup.next().makeSucceededFuture( |
| 66 | + keys.map { DataLoaderFutureValue.success($0) } |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + let nioBatchingLoader = DataLoader<Int, Int>( |
| 71 | + options: .init( |
| 72 | + batchingEnabled: true, |
| 73 | + cachingEnabled: false, |
| 74 | + maxBatchSize: 10 |
| 75 | + ) |
| 76 | + ) { keys in |
| 77 | + return eventLoopGroup.next().makeSucceededFuture( |
| 78 | + keys.map { DataLoaderFutureValue.success($0) } |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + Benchmark("nioLoadNonBatching") { _ in |
| 83 | + let futures = try (0..<1_000).map { i in |
| 84 | + try nioNonBatchingLoader.load(key: i, on: eventLoopGroup.next()) |
| 85 | + } |
| 86 | + let result = try EventLoopFuture.whenAllSucceed(futures, on: eventLoopGroup.next()).wait() |
| 87 | + } |
| 88 | + |
| 89 | + Benchmark("nioLoadBatching") { _ in |
| 90 | + let futures = try (0..<1_000).map { i in |
| 91 | + try nioBatchingLoader.load(key: i, on: eventLoopGroup.next()) |
| 92 | + } |
| 93 | + let result = try EventLoopFuture.whenAllSucceed(futures, on: eventLoopGroup.next()).wait() |
| 94 | + } |
| 95 | + |
| 96 | + Benchmark("nioLoadBatchingMany") { _ in |
| 97 | + let result = try nioBatchingLoader.loadMany(keys: Array(0..<1_000), on: eventLoopGroup.next()).wait() |
| 98 | + } |
| 99 | +} |
0 commit comments