Skip to content

Commit

Permalink
更多性能例子
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Nov 14, 2024
1 parent ce3889b commit 895b4b1
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 88 deletions.
22 changes: 21 additions & 1 deletion SwiftPamphletApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@
08F14B422BBDA3EA005B46CC /* NukeVideo in Frameworks */ = {isa = PBXBuildFile; productRef = 08F14B412BBDA3EA005B46CC /* NukeVideo */; };
08F14B442BBE2865005B46CC /* ViewComponentImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F14B432BBE2865005B46CC /* ViewComponentImage.swift */; };
08F51BC527A374A500693AB6 /* footer_js.html in Resources */ = {isa = PBXBuildFile; fileRef = 08F51BC427A374A500693AB6 /* footer_js.html */; };
3A05B7C92CE57A5C00B2B30F /* TaskCaseLoadFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A05B7C82CE57A5C00B2B30F /* TaskCaseLoadFile.swift */; };
3A05B7CB2CE57D6F00B2B30F /* TaskCaseSemaphore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A05B7CA2CE57D6F00B2B30F /* TaskCaseSemaphore.swift */; };
3A05B7CD2CE5829C00B2B30F /* TaskCaseJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A05B7CC2CE5829C00B2B30F /* TaskCaseJSON.swift */; };
3A30EFD62CDA818B0029CB2F /* WeatherKit(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 3A30EFD52CDA818B0029CB2F /* WeatherKit(ap).md */; };
3A3168932CE0FD53004DFC5C /* HomeiOSView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A3168922CE0FD53004DFC5C /* HomeiOSView.swift */; };
3A3168952CE0FFCE004DFC5C /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3A3168942CE0FFCE004DFC5C /* Launch Screen.storyboard */; platformFilter = ios; };
Expand Down Expand Up @@ -974,6 +977,9 @@
08F0F8C02C959B0600DC659B /* 开源-时间(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "开源-时间(ap).md"; sourceTree = "<group>"; };
08F14B432BBE2865005B46CC /* ViewComponentImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewComponentImage.swift; sourceTree = "<group>"; };
08F51BC427A374A500693AB6 /* footer_js.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = footer_js.html; sourceTree = "<group>"; };
3A05B7C82CE57A5C00B2B30F /* TaskCaseLoadFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskCaseLoadFile.swift; sourceTree = "<group>"; };
3A05B7CA2CE57D6F00B2B30F /* TaskCaseSemaphore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskCaseSemaphore.swift; sourceTree = "<group>"; };
3A05B7CC2CE5829C00B2B30F /* TaskCaseJSON.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskCaseJSON.swift; sourceTree = "<group>"; };
3A30EFD52CDA818B0029CB2F /* WeatherKit(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "WeatherKit(ap).md"; sourceTree = "<group>"; };
3A3168922CE0FD53004DFC5C /* HomeiOSView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeiOSView.swift; sourceTree = "<group>"; };
3A3168942CE0FFCE004DFC5C /* Launch Screen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2309,14 +2315,25 @@
path = Category;
sourceTree = "<group>";
};
3A05B7C72CE57A4100B2B30F /* TaskCase */ = {
isa = PBXGroup;
children = (
3AE3F9522CE4532D007EFDFA /* TaskCase.swift */,
3A05B7C82CE57A5C00B2B30F /* TaskCaseLoadFile.swift */,
3A05B7CC2CE5829C00B2B30F /* TaskCaseJSON.swift */,
3A05B7CA2CE57D6F00B2B30F /* TaskCaseSemaphore.swift */,
);
path = TaskCase;
sourceTree = "<group>";
};
3ADBA84D2CE209C000B0050B /* Performance */ = {
isa = PBXGroup;
children = (
3ADBA84E2CE209FB00B0050B /* MetricManager.swift */,
3ADBA8502CE216E900B0050B /* Perf.swift */,
3AA462352CE228CA00774B59 /* NotificationPreheat.swift */,
3AA462372CE261CC00774B59 /* TaskManager.swift */,
3AE3F9522CE4532D007EFDFA /* TaskCase.swift */,
3A05B7C72CE57A4100B2B30F /* TaskCase */,
3AE3F9602CE49FA2007EFDFA /* TaskCaseViews */,
);
path = Performance;
Expand Down Expand Up @@ -2921,6 +2938,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3A05B7C92CE57A5C00B2B30F /* TaskCaseLoadFile.swift in Sources */,
0871C61B2BA04D23000B620D /* CategoryRowView.swift in Sources */,
08F14B442BBE2865005B46CC /* ViewComponentImage.swift in Sources */,
08069CAB2BDE01E800D48E24 /* GuideListView.swift in Sources */,
Expand All @@ -2932,6 +2950,7 @@
3AF2A2E52BE231BD00F3BE1B /* StarInfosView.swift in Sources */,
3AF2A2E32BE2317300F3BE1B /* StarInfoListView.swift in Sources */,
3AE0D59A2BAB0A0600D6D925 /* DeveloperListView.swift in Sources */,
3A05B7CD2CE5829C00B2B30F /* TaskCaseJSON.swift in Sources */,
3A3168932CE0FD53004DFC5C /* HomeiOSView.swift in Sources */,
084417772B99BA3F0049297D /* SidebarView.swift in Sources */,
3AA462362CE228CA00774B59 /* NotificationPreheat.swift in Sources */,
Expand All @@ -2949,6 +2968,7 @@
08A9E1A22BC25D0700A73764 /* ViewComponentMarkdown.swift in Sources */,
0887A59A2BA28F6D00131359 /* CSGuideView.swift in Sources */,
08D8EFE52BED825E00AA0020 /* BookmarkListView.swift in Sources */,
3A05B7CB2CE57D6F00B2B30F /* TaskCaseSemaphore.swift in Sources */,
3AF2A2DF2BE22A8C00F3BE1B /* UnCategoryInfoListView.swift in Sources */,
3AE3F9622CE4A0C5007EFDFA /* TaskCaseUIUpdateView.swift in Sources */,
086A5F362744ED9600FECE02 /* RepoView.swift in Sources */,
Expand Down
28 changes: 15 additions & 13 deletions SwiftPamphletApp/App/SwiftPamphletAppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,37 @@ struct SwiftPamphletAppApp: App {
#elseif os(iOS)
HomeiOSView()
.onAppear {
#if DEBUG
// background fetch
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.starming.fetch", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
scheduleAppRefresh()
// 任务示例
// TaskCase().bad()
TaskCase().good()

// 任务管理器示例
// taskgroupDemo()

#if DEBUG
// 查看整体从进程创建到主界面加载完成时间,只在开发环境下执行
// 查看整体从进程创建到主界面加载完成时间
if let processStartTime = Perf.getProcessRunningTime() {
// 主界面加载完成,记录终点
let launchEndTime = DispatchTime.now()
let launchTime = Double(launchEndTime.uptimeNanoseconds - launchStartTime.uptimeNanoseconds) / 1_000_000_000

// Pre-main
print("Pre-main : \(String(format: "%.2f", (processStartTime - launchTime)))")
// Post-main
print("进程创建到主界面显示时间: \(String(format: "%.2f", processStartTime))")
} else {
print("无法获取进程创建时间")
}
#endif

// 任务示例
// TaskCase.bad()
TaskCase.good()

// 任务管理器示例
// taskgroupDemo()

if let processStartTime = Perf.getProcessRunningTime() {
// Post-main
print("进程创建到进入主界面时间: \(String(format: "%.2f", processStartTime))")
}
#endif

// 记录启动结束
os_signpost(.end, log: log, name: "Launch", signpostID: signpostID)
Expand All @@ -102,8 +106,6 @@ struct SwiftPamphletAppApp: App {

}



#if os(iOS)
// MARK: - Background Task
func scheduleAppRefresh() {
Expand Down
1 change: 1 addition & 0 deletions SwiftPamphletApp/HomeUI/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct HomeView: View {
.onOpenURL(perform: { url in
// 处理外部链接
})

#endif
}
}
Expand Down
8 changes: 4 additions & 4 deletions SwiftPamphletApp/HomeUI/HomeiOSView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ struct TaskCaseView: View {
ScrollView {
TaskCaseUIUpdateView(isBad: false)
.onAppear {
Perf.showTime(des: "UI更新")
Perf.showTime("UI更新视图")
}
TaskCaseAnimationView(isBad: false)
.onAppear {
Perf.showTime(des: "动画视图")
Perf.showTime("动画视图")
}
TaskCaseBigImageView(isBad: false)
.onAppear {
Perf.showTime(des: "大图处理视图")
Perf.showTime("大图处理视图")
}
TaskCaseCacheView()
// 异步执行,计算量大会影响主线程
// TaskCasePriorityView(isBad: true)
// TaskCasePriorityView(isBad: false)
// .onAppear {
// Perf.showTime(des: "优先级视图")
// }
Expand Down
3 changes: 2 additions & 1 deletion SwiftPamphletApp/Performance/Perf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

// 性能工具
struct Perf {
static func showTime(des: String = "") {
static func showTime(_ des: String = "") {
if let processStartTime = Perf.getProcessRunningTime() {
print("进程创建到\(des)时间: \(String(format: "%.2f", processStartTime))")
}
Expand All @@ -33,6 +33,7 @@ struct Perf {
let startTimeSec = kinfo.kp_proc.p_starttime.tv_sec
let startTimeUsec = kinfo.kp_proc.p_starttime.tv_usec
let startTime = TimeInterval(startTimeSec) + TimeInterval(startTimeUsec) / 1_000_000

let currentTime = Date().timeIntervalSince1970
return currentTime - startTime
}
Expand Down
62 changes: 0 additions & 62 deletions SwiftPamphletApp/Performance/TaskCase.swift

This file was deleted.

23 changes: 23 additions & 0 deletions SwiftPamphletApp/Performance/TaskCase/TaskCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// BadCase.swift
// SwiftPamphletApp
//
// Created by Ming on 2024/11/13.
//

import Foundation

struct TaskCase {
static func bad() {
TaskCase.badLoadFile() // 读取文件
TaskCase.badSemaphore() // 信号量
TaskCase.badJSONDecode() // JSON 解析
}

static func good() {
TaskCase.goodLoadFile() // 读取文件
TaskCase.goodSemaphore() // 信号量
TaskCase.goodJSONDecode() // JSON 解析
}

}
58 changes: 58 additions & 0 deletions SwiftPamphletApp/Performance/TaskCase/TaskCaseJSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// TaskCaseJSON.swift
// SwiftPamphletApp
//
// Created by Ming on 2024/11/14.
//

import Foundation

// 数据模型
struct TCItem: Codable, Identifiable {
let id: Int
let title: String
let description: String
}

extension TaskCase {
static func badJSONDecode() {
let jsonData = TaskCase.generateLargeJSON()
do {
_ = try JSONDecoder().decode([TCItem].self, from: jsonData)

} catch {
print("解析失败: \(error)")
}
Perf.showTime("未优化JSON解析")
}

static func goodJSONDecode() {
Task.detached(priority: .background) {
do {
_ = try await parseJSON()
Perf.showTime("异步优化JSON解析")
} catch {
print("解析失败: \(error)")
}
}
}

// 异步解析JSON
@Sendable
static func parseJSON() async throws -> [TCItem] {
let jsonData = TaskCase.generateLargeJSON()
return try JSONDecoder().decode([TCItem].self, from: jsonData)
}

static func generateLargeJSON() -> Data {
var items: [[String: Any]] = []
for i in 0...10000 {
items.append([
"id": i,
"title": "标题 \(i)",
"description": "这是一段很长的描述文本,用来模拟实际场景中的数据量 \(i)"
])
}
return try! JSONSerialization.data(withJSONObject: items)
}
}
48 changes: 48 additions & 0 deletions SwiftPamphletApp/Performance/TaskCase/TaskCaseLoadFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// TaskCaseLoadFile.swift
// SwiftPamphletApp
//
// Created by Ming on 2024/11/14.
//

import Foundation

extension TaskCase {
// 同步读取方式 - 会阻塞主线程
static func badLoadFile() {
// 模拟耗时操作,减少循环次数并添加延迟
var content = ""
for i in 1...10 {
content += "这是第\(i)行内容\n"
Thread.sleep(forTimeInterval: 0.3) // 每次循环暂停0.3秒
}
Perf.showTime("未优化文件读取")
}

// 异步读取方式 - 推荐使用
static func goodLoadFile() {
Task {
do {
_ = try await withCheckedThrowingContinuation { continuation in
DispatchQueue.global().async {
var content = ""
for i in 1...10 {
content += "这是第\(i)行内容\n"
Thread.sleep(forTimeInterval: 0.3) // 每次循环暂停0.3秒
}
continuation.resume(returning: content)
}
}

// 更新UI要在主线程
await MainActor.run {
Perf.showTime("异步优化文件读取")
}
} catch {
await MainActor.run {
print("读取文件失败")
}
}
}
}
}
Loading

0 comments on commit 895b4b1

Please sign in to comment.