Skip to content

Commit

Permalink
Add _BenchmarkHost
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Jan 9, 2024
1 parent 9bd3571 commit 348d2e6
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sources/OpenSwiftUI/Internal/Other/EnvironmentHelper.swift
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
}
}
}
3 changes: 3 additions & 0 deletions Sources/OpenSwiftUI/Internal/Test/_Benchmark.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public protocol _Benchmark: _Test {
func measure(host: _BenchmarkHost) -> [Double]
}
74 changes: 74 additions & 0 deletions Sources/OpenSwiftUI/Internal/Test/_BenchmarkHost.swift
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
}
}
9 changes: 9 additions & 0 deletions Sources/OpenSwiftUI/Internal/Test/_Test.swift
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 Sources/OpenSwiftUI/Views/View/internal/ViewRendererHost.swift
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")
}
}

0 comments on commit 348d2e6

Please sign in to comment.