2
0

ServerProvidingBenchmark.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPC
  17. import GRPCSampleData
  18. import NIOCore
  19. import NIOPosix
  20. class ServerProvidingBenchmark: Benchmark {
  21. private let providers: [CallHandlerProvider]
  22. private let threadCount: Int
  23. private let useNIOTSIfAvailable: Bool
  24. private let useTLS: Bool
  25. private var group: EventLoopGroup!
  26. private(set) var server: Server!
  27. init(
  28. providers: [CallHandlerProvider],
  29. useNIOTSIfAvailable: Bool,
  30. useTLS: Bool,
  31. threadCount: Int = 1
  32. ) {
  33. self.providers = providers
  34. self.useNIOTSIfAvailable = useNIOTSIfAvailable
  35. self.useTLS = useTLS
  36. self.threadCount = threadCount
  37. }
  38. func setUp() throws {
  39. if self.useNIOTSIfAvailable {
  40. self.group = PlatformSupport.makeEventLoopGroup(loopCount: self.threadCount)
  41. } else {
  42. self.group = MultiThreadedEventLoopGroup(numberOfThreads: self.threadCount)
  43. }
  44. if self.useTLS {
  45. #if canImport(NIOSSL)
  46. self.server = try Server.usingTLSBackedByNIOSSL(
  47. on: self.group,
  48. certificateChain: [SampleCertificate.server.certificate],
  49. privateKey: SamplePrivateKey.server
  50. ).withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  51. .withServiceProviders(self.providers)
  52. .bind(host: "127.0.0.1", port: 0)
  53. .wait()
  54. #else
  55. fatalError("NIOSSL must be imported to use TLS")
  56. #endif
  57. } else {
  58. self.server = try Server.insecure(group: self.group)
  59. .withServiceProviders(self.providers)
  60. .bind(host: "127.0.0.1", port: 0)
  61. .wait()
  62. }
  63. }
  64. func tearDown() throws {
  65. try self.server.close().wait()
  66. try self.group.syncShutdownGracefully()
  67. }
  68. func run() throws -> Int {
  69. return 0
  70. }
  71. }