From a3631efd382f3597ac80b7c6ade491b86c8338e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E9=93=AD?= Date: Mon, 2 Dec 2024 17:33:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20MetricsManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SwiftPamphletApp.xcodeproj/project.pbxproj | 16 +++- .../App/SwiftPamphletAppApp.swift | 6 +- .../Guide/View/GuideOutline/AppleGuide.swift | 4 +- .../Performance/MetricManager.swift | 80 ++++++++++++------- .../Performance/TaskManager.swift | 23 +++--- 5 files changed, 81 insertions(+), 48 deletions(-) diff --git a/SwiftPamphletApp.xcodeproj/project.pbxproj b/SwiftPamphletApp.xcodeproj/project.pbxproj index 538e2cc1..1642a017 100644 --- a/SwiftPamphletApp.xcodeproj/project.pbxproj +++ b/SwiftPamphletApp.xcodeproj/project.pbxproj @@ -130,6 +130,7 @@ 0851E5702C872A7A00D9463A /* CPU占用率(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 0851E56F2C872A7A00D9463A /* CPU占用率(ap).md */; }; 0851E5722C872AD100D9463A /* CPU核数和类型(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 0851E5712C872AD100D9463A /* CPU核数和类型(ap).md */; }; 0851E5792C87563100D9463A /* 截获网络请求数据(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 0851E5782C87563100D9463A /* 截获网络请求数据(ap).md */; }; + 085E1A562CF60CBA009938E8 /* 人工智能-RAG(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 085E1A552CF60CBA009938E8 /* 人工智能-RAG(ap).md */; }; 0868D00B2BDD37280023C871 /* SMGitHub in Frameworks */ = {isa = PBXBuildFile; productRef = 0868D00A2BDD37280023C871 /* SMGitHub */; }; 086A5F072744E88E00FECE02 /* SwiftPamphletAppApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086A5F062744E88E00FECE02 /* SwiftPamphletAppApp.swift */; }; 086A5F0B2744E89100FECE02 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 086A5F0A2744E89100FECE02 /* Assets.xcassets */; }; @@ -457,7 +458,6 @@ 08D5A7992CECBA710036AAC6 /* 人工智能-MLX(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08D5A7982CECBA710036AAC6 /* 人工智能-MLX(ap).md */; }; 08D5A79B2CECBAA90036AAC6 /* 使用三方大模型接口(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08D5A79A2CECBAA90036AAC6 /* 使用三方大模型接口(ap).md */; }; 08D5A79D2CECBAE40036AAC6 /* 用于开发APP的提示词(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08D5A79C2CECBAE40036AAC6 /* 用于开发APP的提示词(ap).md */; }; - 08D5A79F2CECBAF90036AAC6 /* 人工智能-RAG(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08D5A79E2CECBAF90036AAC6 /* 人工智能-RAG(ap).md */; }; 08D5A7A12CECBB0D0036AAC6 /* 人工智能-技术原理(ap).md in Resources */ = {isa = PBXBuildFile; fileRef = 08D5A7A02CECBB0D0036AAC6 /* 人工智能-技术原理(ap).md */; }; 08D8EFE52BED825E00AA0020 /* BookmarkListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08D8EFE42BED825E00AA0020 /* BookmarkListView.swift */; }; 08D8F0072BEFBAC700AA0020 /* WWDCData.json in Resources */ = {isa = PBXBuildFile; fileRef = 08D8F0062BEFBAC700AA0020 /* WWDCData.json */; }; @@ -645,6 +645,7 @@ 0851E56F2C872A7A00D9463A /* CPU占用率(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "CPU占用率(ap).md"; sourceTree = ""; }; 0851E5712C872AD100D9463A /* CPU核数和类型(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "CPU核数和类型(ap).md"; sourceTree = ""; }; 0851E5782C87563100D9463A /* 截获网络请求数据(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "截获网络请求数据(ap).md"; sourceTree = ""; }; + 085E1A552CF60CBA009938E8 /* 人工智能-RAG(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "人工智能-RAG(ap).md"; sourceTree = ""; }; 0869233F2BF2BF81006779A3 /* AVKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVKit.framework; path = System/Library/Frameworks/AVKit.framework; sourceTree = SDKROOT; }; 086A5F032744E88E00FECE02 /* 戴铭的开发小册子.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "戴铭的开发小册子.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 086A5F062744E88E00FECE02 /* SwiftPamphletAppApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftPamphletAppApp.swift; sourceTree = ""; }; @@ -972,7 +973,6 @@ 08D5A7982CECBA710036AAC6 /* 人工智能-MLX(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "人工智能-MLX(ap).md"; sourceTree = ""; }; 08D5A79A2CECBAA90036AAC6 /* 使用三方大模型接口(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "使用三方大模型接口(ap).md"; sourceTree = ""; }; 08D5A79C2CECBAE40036AAC6 /* 用于开发APP的提示词(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "用于开发APP的提示词(ap).md"; sourceTree = ""; }; - 08D5A79E2CECBAF90036AAC6 /* 人工智能-RAG(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "人工智能-RAG(ap).md"; sourceTree = ""; }; 08D5A7A02CECBB0D0036AAC6 /* 人工智能-技术原理(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "人工智能-技术原理(ap).md"; sourceTree = ""; }; 08D8EFE42BED825E00AA0020 /* BookmarkListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarkListView.swift; sourceTree = ""; }; 08D8F0062BEFBAC700AA0020 /* WWDCData.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = WWDCData.json; sourceTree = ""; }; @@ -1108,7 +1108,7 @@ 0832669D2C97E67A00E27CC5 /* Stable Diffusion(ap).md */, 083266A12C987A0700E27CC5 /* AI辅助开发APP(ap).md */, 08D5A79C2CECBAE40036AAC6 /* 用于开发APP的提示词(ap).md */, - 08D5A79E2CECBAF90036AAC6 /* 人工智能-RAG(ap).md */, + 085E1A542CF60C52009938E8 /* 智能体 */, 08D5A7A02CECBB0D0036AAC6 /* 人工智能-技术原理(ap).md */, ); path = "人工智能"; @@ -1373,6 +1373,14 @@ path = "启动优化"; sourceTree = ""; }; + 085E1A542CF60C52009938E8 /* 智能体 */ = { + isa = PBXGroup; + children = ( + 085E1A552CF60CBA009938E8 /* 人工智能-RAG(ap).md */, + ); + path = "智能体"; + sourceTree = ""; + }; 0869233E2BF2BF81006779A3 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -2525,7 +2533,6 @@ 08BDD5E92C6C8A3F00914412 /* SwiftData基础(ap).md in Resources */, 08BDD5102C6B007A00914412 /* flatMap(ap).md in Resources */, 08BDD58D2C6B007A00914412 /* Sheet(ap).md in Resources */, - 08D5A79F2CECBAF90036AAC6 /* 人工智能-RAG(ap).md in Resources */, 08448FE4279EC7ED00B61353 /* 范围(ap).md in Resources */, 083266892C9722DD00E27CC5 /* Reality Composer Pro(ap).md in Resources */, 08BDD5182C6B007A00914412 /* zip(ap).md in Resources */, @@ -2765,6 +2772,7 @@ 08BDD5A42C6B007A00914412 /* Previews协议(ap).md in Resources */, 08BDD5012C6B007A00914412 /* KeyframeAnimator(ap).md in Resources */, 088F2C322C93284100DCA64F /* 知识管理-介绍(ap).md in Resources */, + 085E1A562CF60CBA009938E8 /* 人工智能-RAG(ap).md in Resources */, 08448FC8279EC54300B61353 /* If(ap).md in Resources */, 08BDD51C2C6B007A00914412 /* Async Sequences(ap).md in Resources */, 08BDD5B82C6B007A00914412 /* List-移动元素(ap).md in Resources */, diff --git a/SwiftPamphletApp/App/SwiftPamphletAppApp.swift b/SwiftPamphletApp/App/SwiftPamphletAppApp.swift index 4c51d5a9..f76a8eb1 100644 --- a/SwiftPamphletApp/App/SwiftPamphletAppApp.swift +++ b/SwiftPamphletApp/App/SwiftPamphletAppApp.swift @@ -75,7 +75,7 @@ struct SwiftPamphletAppApp: App { TaskCase.good() // 任务管理器示例 -// taskgroupDemo() + taskgroupDemo() if let processStartTime = Perf.getProcessRunningTime() { // Post-main @@ -94,10 +94,6 @@ struct SwiftPamphletAppApp: App { break } } - .environment(metricsManager) - .task { - await metricsManager.fetchMetrics() - } // .modelContainer(for: [IOInfo.self, DeveloperModel.self, BookmarkModel.self], isUndoEnabled: true) #endif } diff --git a/SwiftPamphletApp/Guide/View/GuideOutline/AppleGuide.swift b/SwiftPamphletApp/Guide/View/GuideOutline/AppleGuide.swift index cbf09cda..a347b43d 100644 --- a/SwiftPamphletApp/Guide/View/GuideOutline/AppleGuide.swift +++ b/SwiftPamphletApp/Guide/View/GuideOutline/AppleGuide.swift @@ -548,7 +548,9 @@ struct AppleGuide { L(t: "Stable Diffusion"), L(t: "AI辅助开发APP"), L(t: "用于开发APP的提示词"), - L(t: "人工智能-RAG"), + L(t: "智能体", sub: [ + L(t: "人工智能-RAG"), + ]), L(t: "人工智能-技术原理"), ]), L(t: "三方库使用", icon:"tray.2", sub: [ diff --git a/SwiftPamphletApp/Performance/MetricManager.swift b/SwiftPamphletApp/Performance/MetricManager.swift index 00209a89..24d6f962 100644 --- a/SwiftPamphletApp/Performance/MetricManager.swift +++ b/SwiftPamphletApp/Performance/MetricManager.swift @@ -7,46 +7,72 @@ #if os(iOS) import MetricKit -import Observation -@Observable @MainActor class MetricsManager: NSObject, @preconcurrency MXMetricManagerSubscriber { - var latestPayload: MXMetricPayload? - + static let shared = MetricsManager() + override init() { super.init() MXMetricManager.shared.add(self) } - deinit { - MXMetricManager.shared.remove(self) - } - + // 接收性能报告的回调 func didReceive(_ payloads: [MXMetricPayload]) { - Task { @MainActor in - if let latest = payloads.last { - self.latestPayload = latest + for payload in payloads { + if let launchMetrics = payload.applicationLaunchMetrics { + // This represents the time when the first CA commit is finished. + print(launchMetrics.histogrammedTimeToFirstDraw) } } } - - func didReceive(_ payloads: [MXDiagnosticPayload]) { - // Handle diagnostic payloads if needed - } - - func fetchMetrics() async { - // Example async function to fetch metrics - try! await Task.sleep(nanoseconds: 2 * 1_000_000_000) // Simulate network delay - // Update state with fetched metrics + + deinit { + MXMetricManager.shared.remove(self) } } -extension MetricsManager { - func performAsyncOperation() async { - await Task { @Sendable in - // Perform some async operation - }.value - } -} +//import MetricKit +//import Observation + +//@Observable +//@MainActor +//class MetricsManager: NSObject, @preconcurrency MXMetricManagerSubscriber { +// var latestPayload: MXMetricPayload? +// +// override init() { +// super.init() +// MXMetricManager.shared.add(self) +// } +// +// deinit { +// MXMetricManager.shared.remove(self) +// } +// +// func didReceive(_ payloads: [MXMetricPayload]) { +// Task { @MainActor in +// if let latest = payloads.last { +// self.latestPayload = latest +// } +// } +// } +// +// func didReceive(_ payloads: [MXDiagnosticPayload]) { +// // Handle diagnostic payloads if needed +// } +// +// func fetchMetrics() async { +// // Example async function to fetch metrics +// try! await Task.sleep(nanoseconds: 2 * 1_000_000_000) // Simulate network delay +// // Update state with fetched metrics +// } +//} +// +//extension MetricsManager { +// func performAsyncOperation() async { +// await Task { @Sendable in +// // Perform some async operation +// }.value +// } +//} #endif diff --git a/SwiftPamphletApp/Performance/TaskManager.swift b/SwiftPamphletApp/Performance/TaskManager.swift index 5564d44d..2ebe18ee 100644 --- a/SwiftPamphletApp/Performance/TaskManager.swift +++ b/SwiftPamphletApp/Performance/TaskManager.swift @@ -29,29 +29,29 @@ func performLowPriorityTasks(tasks: [@Sendable () async -> Void], withTimeLimit: } func taskgroupDemo() { - @Sendable func wait() async { - try? await Task.sleep(nanoseconds: 2_000_000_000) + @Sendable func doSomething(sec: UInt64 = 2) async { + try? await Task.sleep(nanoseconds: sec * 1_000_000_000) } var tasks = [@Sendable () async -> Void]() tasks.append { - await wait() + await doSomething() print("Task One Completed") } tasks.append { - await wait() + await doSomething() print("Task Two Completed") } tasks.append { - await wait() + await doSomething() print("Task Three Completed") } tasks.append { - await wait() + await doSomething() print("Task Four Completed") } tasks.append { - await wait() + await doSomething() print("Task Five Completed") } @@ -59,25 +59,26 @@ func taskgroupDemo() { var backgroundTasks = [@Sendable () async -> Void]() backgroundTasks.append { - await wait() + await doSomething() print("Background Task One Executed") } backgroundTasks.append { - await wait() + await doSomething(sec: 10) print("Background Task Two Executed") } backgroundTasks.append { - await wait() + await doSomething() print("Background Task Three Executed") } backgroundTasks.append { - await wait() + await doSomething() print("Background Task Four Executed") } // MARK: 开始执行任务 // 低优先级任务 performLowPriorityTasks(tasks: backgroundTasks) + // 分组执行 Task { await executeTasksConcurrently(tasks: tasks) print("first group of tasks completed.")