|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022-2023 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 Benchmark |
| 14 | +import func Benchmark.blackHole |
| 15 | + |
| 16 | +#if os(macOS) && USE_PACKAGE |
| 17 | +import FoundationEssentials |
| 18 | +import FoundationInternationalization |
| 19 | +#else |
| 20 | +import Foundation |
| 21 | +#endif |
| 22 | + |
| 23 | +#if FOUNDATION_FRAMEWORK |
| 24 | +// FOUNDATION_FRAMEWORK has a scheme per benchmark file, so only include one benchmark here. |
| 25 | +let benchmarks = { |
| 26 | + timeZoneBenchmarks() |
| 27 | +} |
| 28 | +#endif |
| 29 | + |
| 30 | +let testDates = { |
| 31 | + var now = Date.now |
| 32 | + var dates: [Date] = [] |
| 33 | + for i in 0...10000 { |
| 34 | + dates.append(Date(timeInterval: Double(i * 3600), since: now)) |
| 35 | + } |
| 36 | + return dates |
| 37 | +}() |
| 38 | + |
| 39 | +func timeZoneBenchmarks() { |
| 40 | + |
| 41 | + Benchmark.defaultConfiguration.maxIterations = 1_000 |
| 42 | + Benchmark.defaultConfiguration.maxDuration = .seconds(3) |
| 43 | + Benchmark.defaultConfiguration.scalingFactor = .kilo |
| 44 | + Benchmark.defaultConfiguration.metrics = [.cpuTotal, .mallocCountTotal, .throughput, .peakMemoryResident] |
| 45 | + |
| 46 | + guard let t = TimeZone(identifier: "America/Los_Angeles") else { |
| 47 | + fatalError("unexpected failure when creating time zone") |
| 48 | + } |
| 49 | + |
| 50 | + Benchmark("secondsFromGMT", configuration: .init(scalingFactor: .mega)) { benchmark in |
| 51 | + for d in testDates { |
| 52 | + let s = t.secondsFromGMT(for: d) |
| 53 | + blackHole(s) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + Benchmark("creatingTimeZones", configuration: .init(scalingFactor: .mega)) { benchmark in |
| 58 | + for name in NSTimeZone.knownTimeZoneNames { |
| 59 | + let t = TimeZone(identifier: name) |
| 60 | + blackHole(t) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + Benchmark("secondsFromGMT_manyTimeZones", configuration: .init(scalingFactor: .mega)) { benchmark in |
| 65 | + for name in NSTimeZone.knownTimeZoneNames { |
| 66 | + let t = TimeZone(identifier: name)! |
| 67 | + for d in testDates { |
| 68 | + let s = t.secondsFromGMT(for: d) |
| 69 | + blackHole(s) |
| 70 | + } |
| 71 | + blackHole(t) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | + |
0 commit comments