|
| 1 | +//===--- NIOChannelPipeline.swift -----------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import TestsUtils |
| 14 | + |
| 15 | +// Mini benchmark implementing the gist of SwiftNIO's ChannelPipeline as |
| 16 | +// implemented by NIO 1 and NIO 2.[01] |
| 17 | +let t: [BenchmarkCategory] = [.runtime, .refcount] |
| 18 | +let N = 100 |
| 19 | + |
| 20 | +public let NIOChannelPipeline = [ |
| 21 | + BenchmarkInfo( |
| 22 | + name: "NIOChannelPipeline", |
| 23 | + runFunction: runBench, |
| 24 | + tags: t), |
| 25 | +] |
| 26 | + |
| 27 | +public protocol EventHandler: class { |
| 28 | + func event(holder: Holder) |
| 29 | +} |
| 30 | + |
| 31 | +extension EventHandler { |
| 32 | + public func event(holder: Holder) { |
| 33 | + holder.fireEvent() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +public final class Pipeline { |
| 38 | + var head: Holder? = nil |
| 39 | + |
| 40 | + public init() {} |
| 41 | + |
| 42 | + public func addHandler(_ handler: EventHandler) { |
| 43 | + if self.head == nil { |
| 44 | + self.head = Holder(handler) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + var node = self.head |
| 49 | + while node?.next != nil { |
| 50 | + node = node?.next |
| 51 | + } |
| 52 | + node?.next = Holder(handler) |
| 53 | + } |
| 54 | + |
| 55 | + public func fireEvent() { |
| 56 | + self.head!.invokeEvent() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +public final class Holder { |
| 61 | + var next: Holder? |
| 62 | + let node: EventHandler |
| 63 | + |
| 64 | + init(_ node: EventHandler) { |
| 65 | + self.next = nil |
| 66 | + self.node = node |
| 67 | + } |
| 68 | + |
| 69 | + func invokeEvent() { |
| 70 | + self.node.event(holder: self) |
| 71 | + } |
| 72 | + |
| 73 | + @inline(never) |
| 74 | + public func fireEvent() { |
| 75 | + self.next?.invokeEvent() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +public final class NoOpHandler: EventHandler { |
| 80 | + public init() {} |
| 81 | +} |
| 82 | + |
| 83 | +public final class ConsumingHandler: EventHandler { |
| 84 | + var consumed = 0 |
| 85 | + public init() {} |
| 86 | + public func event(holder: Holder) { |
| 87 | + self.consumed += 1 |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +@inline(never) |
| 92 | +func runBench(iterations: Int) { |
| 93 | + let pipeline = Pipeline() |
| 94 | + for _ in 0..<5 { |
| 95 | + pipeline.addHandler(NoOpHandler()) |
| 96 | + } |
| 97 | + pipeline.addHandler(ConsumingHandler()) |
| 98 | + |
| 99 | + for _ in 0 ..< iterations { |
| 100 | + for _ in 0 ..< 1000 { |
| 101 | + pipeline.fireEvent() |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments