Skip to content

Commit 348d2e6

Browse files
committed
Add _BenchmarkHost
1 parent 9bd3571 commit 348d2e6

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#if canImport(Darwin)
2+
import Darwin
3+
#elseif canImport(Glibc)
4+
import Glibc
5+
#else
6+
#error("Unsupported Platform")
7+
#endif
8+
9+
enum EnvironmentHelper {
10+
@_transparent
11+
@inline(__always)
12+
static func value(for key: String) -> Bool {
13+
key.withCString { string in
14+
guard let env = getenv(string) else {
15+
return false
16+
}
17+
return atoi(env) != 0
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public protocol _Benchmark: _Test {
2+
func measure(host: _BenchmarkHost) -> [Double]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// _BenchmarkHost.swift
3+
// OpenSwiftUI
4+
//
5+
// Created by Kyle on 2023/1/9.
6+
// Lastest Version: iOS 15.5
7+
// Status: Complete
8+
// ID: 3E629D505F0A70F29ACFC010AA42C6E0
9+
10+
#if canImport(Darwin)
11+
import CoreGraphics
12+
#elseif os(Linux)
13+
import Foundation
14+
#endif
15+
16+
#if canImport(QuartzCore)
17+
import QuartzCore
18+
#endif
19+
20+
private let enableProfiler = EnvironmentHelper.value(for: "OPENSWIFTUI_PROFILE_BENCHMARKS")
21+
22+
public protocol _BenchmarkHost: AnyObject {
23+
func _renderForTest(interval: Double)
24+
func _renderAsyncForTest(interval: Double) -> Bool
25+
func _performScrollTest(startOffset: CGFloat, iterations: Int, delta: CGFloat, length: CGFloat, completion: (() -> Void)?)
26+
}
27+
28+
extension _BenchmarkHost {
29+
public func _renderAsyncForTest(interval _: Double) -> Bool {
30+
false
31+
}
32+
33+
public func _performScrollTest(startOffset _: CoreGraphics.CGFloat, iterations _: Int, delta _: CoreGraphics.CGFloat, length _: CoreGraphics.CGFloat, completion _: (() -> Void)?) {}
34+
35+
public func measureAction(action: () -> Void) -> Double {
36+
#if canImport(QuartzCore)
37+
let begin = CACurrentMediaTime()
38+
if enableProfiler,
39+
let renderHost = self as? ViewRendererHost {
40+
renderHost.startProfiling()
41+
}
42+
action()
43+
let end = CACurrentMediaTime()
44+
if enableProfiler,
45+
let renderHost = self as? ViewRendererHost {
46+
renderHost.stopProfiling()
47+
}
48+
return end - begin
49+
#else
50+
fatalError("Unsupported Platfrom")
51+
#endif
52+
}
53+
54+
public func measureRender(interval: Double = 1.0 / 60.0) -> Double {
55+
measureAction {
56+
_renderForTest(interval: interval)
57+
}
58+
}
59+
60+
public func measureRenders(seconds: Double) -> [Double] {
61+
measureRenders(duration: seconds)
62+
}
63+
64+
public func measureRenders(duration: Double) -> [Double] {
65+
let minutes = duration / 60.0
66+
let value = Int(minutes.rounded(.towardZero)) + 1
67+
let count = max(value, 0)
68+
var results: [Double] = []
69+
for _ in 0 ..< count {
70+
results.append(measureRender())
71+
}
72+
return results
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public protocol _Test {
2+
func setUpTest()
3+
func tearDownTest()
4+
}
5+
6+
extension _Test {
7+
public func setUpTest() {}
8+
public func tearDownTest() {}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
protocol ViewRendererHost {}
2+
3+
extension ViewRendererHost {
4+
func startProfiling() {
5+
fatalError("TODO")
6+
}
7+
8+
func stopProfiling() {
9+
fatalError("TODO")
10+
}
11+
}

0 commit comments

Comments
 (0)