diff --git a/Sources/OpenSwiftUI/Internal/Other/EnvironmentHelper.swift b/Sources/OpenSwiftUI/Internal/Other/EnvironmentHelper.swift new file mode 100644 index 00000000..defccec9 --- /dev/null +++ b/Sources/OpenSwiftUI/Internal/Other/EnvironmentHelper.swift @@ -0,0 +1,20 @@ +#if canImport(Darwin) +import Darwin +#elseif canImport(Glibc) +import Glibc +#else +#error("Unsupported Platform") +#endif + +enum EnvironmentHelper { + @_transparent + @inline(__always) + static func value(for key: String) -> Bool { + key.withCString { string in + guard let env = getenv(string) else { + return false + } + return atoi(env) != 0 + } + } +} diff --git a/Sources/OpenSwiftUI/Internal/Test/_Benchmark.swift b/Sources/OpenSwiftUI/Internal/Test/_Benchmark.swift new file mode 100644 index 00000000..8ec926c3 --- /dev/null +++ b/Sources/OpenSwiftUI/Internal/Test/_Benchmark.swift @@ -0,0 +1,3 @@ +public protocol _Benchmark: _Test { + func measure(host: _BenchmarkHost) -> [Double] +} diff --git a/Sources/OpenSwiftUI/Internal/Test/_BenchmarkHost.swift b/Sources/OpenSwiftUI/Internal/Test/_BenchmarkHost.swift new file mode 100644 index 00000000..b9cf32bb --- /dev/null +++ b/Sources/OpenSwiftUI/Internal/Test/_BenchmarkHost.swift @@ -0,0 +1,74 @@ +// +// _BenchmarkHost.swift +// OpenSwiftUI +// +// Created by Kyle on 2023/1/9. +// Lastest Version: iOS 15.5 +// Status: Complete +// ID: 3E629D505F0A70F29ACFC010AA42C6E0 + +#if canImport(Darwin) +import CoreGraphics +#elseif os(Linux) +import Foundation +#endif + +#if canImport(QuartzCore) +import QuartzCore +#endif + +private let enableProfiler = EnvironmentHelper.value(for: "OPENSWIFTUI_PROFILE_BENCHMARKS") + +public protocol _BenchmarkHost: AnyObject { + func _renderForTest(interval: Double) + func _renderAsyncForTest(interval: Double) -> Bool + func _performScrollTest(startOffset: CGFloat, iterations: Int, delta: CGFloat, length: CGFloat, completion: (() -> Void)?) +} + +extension _BenchmarkHost { + public func _renderAsyncForTest(interval _: Double) -> Bool { + false + } + + public func _performScrollTest(startOffset _: CoreGraphics.CGFloat, iterations _: Int, delta _: CoreGraphics.CGFloat, length _: CoreGraphics.CGFloat, completion _: (() -> Void)?) {} + + public func measureAction(action: () -> Void) -> Double { + #if canImport(QuartzCore) + let begin = CACurrentMediaTime() + if enableProfiler, + let renderHost = self as? ViewRendererHost { + renderHost.startProfiling() + } + action() + let end = CACurrentMediaTime() + if enableProfiler, + let renderHost = self as? ViewRendererHost { + renderHost.stopProfiling() + } + return end - begin + #else + fatalError("Unsupported Platfrom") + #endif + } + + public func measureRender(interval: Double = 1.0 / 60.0) -> Double { + measureAction { + _renderForTest(interval: interval) + } + } + + public func measureRenders(seconds: Double) -> [Double] { + measureRenders(duration: seconds) + } + + public func measureRenders(duration: Double) -> [Double] { + let minutes = duration / 60.0 + let value = Int(minutes.rounded(.towardZero)) + 1 + let count = max(value, 0) + var results: [Double] = [] + for _ in 0 ..< count { + results.append(measureRender()) + } + return results + } +} diff --git a/Sources/OpenSwiftUI/Internal/Test/_Test.swift b/Sources/OpenSwiftUI/Internal/Test/_Test.swift new file mode 100644 index 00000000..3d312b6a --- /dev/null +++ b/Sources/OpenSwiftUI/Internal/Test/_Test.swift @@ -0,0 +1,9 @@ +public protocol _Test { + func setUpTest() + func tearDownTest() +} + +extension _Test { + public func setUpTest() {} + public func tearDownTest() {} +} diff --git a/Sources/OpenSwiftUI/Views/View/internal/ViewRendererHost.swift b/Sources/OpenSwiftUI/Views/View/internal/ViewRendererHost.swift new file mode 100644 index 00000000..82d4dd38 --- /dev/null +++ b/Sources/OpenSwiftUI/Views/View/internal/ViewRendererHost.swift @@ -0,0 +1,11 @@ +protocol ViewRendererHost {} + +extension ViewRendererHost { + func startProfiling() { + fatalError("TODO") + } + + func stopProfiling() { + fatalError("TODO") + } +}