-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Sources/OpenSwiftUI/Internal/Other/EnvironmentHelper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public protocol _Benchmark: _Test { | ||
func measure(host: _BenchmarkHost) -> [Double] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public protocol _Test { | ||
func setUpTest() | ||
func tearDownTest() | ||
} | ||
|
||
extension _Test { | ||
public func setUpTest() {} | ||
public func tearDownTest() {} | ||
} |
11 changes: 11 additions & 0 deletions
11
Sources/OpenSwiftUI/Views/View/internal/ViewRendererHost.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
protocol ViewRendererHost {} | ||
|
||
extension ViewRendererHost { | ||
func startProfiling() { | ||
fatalError("TODO") | ||
} | ||
|
||
func stopProfiling() { | ||
fatalError("TODO") | ||
} | ||
} |