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