Skip to content

prototype for perf swiftUI trace #15205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions FirebasePerformance/Sources/SwiftUI/PerfTracedView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if canImport(SwiftUI)
import FirebasePerformance
import Foundation
import SwiftUI

// foregroud trace, screen render, refresh times
// background trace, duration
class PerfTraceViewModel {
enum TraceType: String {
case forground
case background
}

let name: String
var trace: Trace?
var traceType: TraceType?

var refreshCount: Int64 = 0

lazy var displayLink: CADisplayLink = .init(target: self, selector: #selector(displayLinkStep))
var previousTimestamp: CFTimeInterval = -1.0
var slowFrameCount: Int64 = 0
var totalFrameCount: Int64 = 0

init(name: String) {
self.name = name
}

func startScreenRenderMonitoring() {
displayLink.add(to: .main, forMode: .common)
}

func stopScreenRenderMonitoring() {
displayLink.remove(from: .main, forMode: .common)
}

// calling perf to start the trace
func startTrace(_ traceType: TraceType) {
if trace != nil {
print("there is active trace already, can't start another one")
}
self.traceType = traceType
trace = Performance().trace(name: "\(name)_\(traceType.rawValue)")
print("trace start \(name)")
trace?.start()
if self.traceType == .forground {
startScreenRenderMonitoring()
}
}

// calling perf to end the trace
func endTrace() {
print("trace end")
if traceType == .forground {
stopScreenRenderMonitoring()
trace?.setValue(totalFrameCount, forMetric: "totalFrameCount")
trace?.setValue(slowFrameCount, forMetric: "slowFrameCount")
}
trace?.stop()
trace = nil
traceType = nil
refreshCount = 0
}

func logChanges() {
refreshCount = refreshCount + 1
print("refresh count changed to: \(refreshCount)")
trace?.setValue(refreshCount, forMetric: "refreshCount")
}

@objc
func displayLinkStep() {
let currentTimestamp = displayLink.timestamp
if previousTimestamp > 0 {
let frameDuration = currentTimestamp - previousTimestamp
if frameDuration > 1.0 / 59.0 {
slowFrameCount += 1
}
}
totalFrameCount += 1
previousTimestamp = currentTimestamp
}
}

public struct PerfTracedView<Content: View>: View {
@State private var viewModel: PerfTraceViewModel

let content: () -> Content

// Init through view builder
public init(_ viewName: String, @ViewBuilder content: @escaping () -> Content) {
self.content = content
// print("Type of this content is: \(content()) ")
viewModel = PerfTraceViewModel(name: viewName)
}

@Environment(\.scenePhase) var scenePhase

public var body: some View {
viewModel.logChanges()

return content()
.onAppear {
print("\(viewModel.name) On appear \(Date().timeIntervalSince1970)")
viewModel.startTrace(.forground)
}
.onDisappear {
print("\(viewModel.name) On disaappear \(Date().timeIntervalSince1970)")
viewModel.endTrace()
}
.onChange(of: scenePhase) { oldValue, newPhase in
if newPhase == .active {
print("\(viewModel.name) Active \(Date().timeIntervalSince1970)")
if viewModel.traceType == .forground {
print("foreground trace already running")
} else {
viewModel.endTrace()
viewModel.startTrace(.forground)
}
} else if newPhase == .inactive {
print("\(viewModel.name) Inactive \(Date().timeIntervalSince1970)")
viewModel.endTrace()
viewModel.startTrace(.background)
} else if newPhase == .background {
print("\(viewModel.name) Background \(Date().timeIntervalSince1970)")
}
}
}
}

public extension View {
func perfTrace(_ viewName: String) -> some View {
return PerfTracedView(viewName) {
self
}
}
}
#endif
14 changes: 13 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,8 @@ let package = Package(
.target(
name: "FirebasePerformanceTarget",
dependencies: [.target(name: "FirebasePerformance",
condition: .when(platforms: [.iOS, .tvOS, .visionOS])),
.target(name: "FirebasePerformanceSwift",
condition: .when(platforms: [.iOS, .tvOS, .visionOS]))],
path: "SwiftPM-PlatformExclude/FirebasePerformanceWrap"
),
Expand All @@ -943,7 +945,12 @@ let package = Package(
.product(name: "GULUserDefaults", package: "GoogleUtilities"),
.product(name: "nanopb", package: "nanopb"),
],
path: "FirebasePerformance/Sources",
path: "FirebasePerformance",
source: [
"Sources/",
],
exclude:
["Sources/SwiftUI/"],
publicHeadersPath: "Public",
cSettings: [
.headerSearchPath("../../"),
Expand All @@ -957,6 +964,11 @@ let package = Package(
.linkedFramework("QuartzCore", .when(platforms: [.iOS, .tvOS])),
]
),
.tartget(
name: "FirebasePerformanceSwift",
dependencies: ["FirebasePerformance"],
path: "FirebasePerformance/Sources/SwiftUI/"
),
.testTarget(
name: "PerformanceUnit",
dependencies: [
Expand Down
Loading