/src/grpc-swift/Sources/GRPC/Stopwatch.swift
Line | Count | Source |
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 Foundation |
17 | | |
18 | | internal class Stopwatch { |
19 | | private let dateProvider: () -> Date |
20 | | private let start: Date |
21 | | |
22 | 0 | init(provider: @escaping () -> Date = { Date() }) { |
23 | 0 | self.dateProvider = provider |
24 | 0 | self.start = provider() |
25 | 0 | } |
26 | | |
27 | 0 | static func start() -> Stopwatch { |
28 | 0 | return Stopwatch() |
29 | 0 | } |
30 | | |
31 | 0 | func elapsed() -> TimeInterval { |
32 | 0 | return self.dateProvider().timeIntervalSince(self.start) |
33 | 0 | } |
34 | | |
35 | 0 | func elapsedMillis() -> Int64 { |
36 | 0 | return Int64(self.elapsed() * 1000) |
37 | 0 | } |
38 | | } |