Nova
语言:英语 | 中文
Nova是一个嵌入到App中的性能监控SDK。您可以实时监控App中是否有问题出现。主要包括以下功能:
开发和测试阶段
- 问题提醒:应用中弹出问题通知,检查问题日志。
- 内存泄漏
- ANR
- 子线程更新UI
- 浮动窗口:显示CPU/内存/帧率指标
生产阶段
- 问题回调:通过代理回调通知应用。应用可以将这些问题报告给后端。
如何使用
Nova包括了核心层、UI层和多个独立的插件。Nova核心是Nova的核心部分,负责管理Nova和插件的整个生命周期,以及事件的分布。Nova UI主要是用于调试的工具,负责问题的通知弹窗和Nova页面的渲染。建议在所有构建中集成Nova核心,在内部测试构建中集成Nova UI。
Podfile
Nova核心是Nova的默认子规格。只要插件在Podfile中连接,Nova核心将默认连接。
pod 'NovaMetrics', subspecs: [
'AnrMonitorPlugin',
'PageMonitorPlugin',
'MemoryLeaksPlugin',
'UIThreadMonitorPlugin',
]
NovaUI是一个调试工具,建议仅在测试构建中集成。可以通过Podfile中的配置进行配置。
pod 'NovaUI', configurations: %w[Debug]
启动Nova
首先,你需要创建一个插件实例,然后调用Nova的启动接口,并传入插件数组。
public class Nova {
/// Launch Nova with plugins.
/// - Parameter plugins: Plugins must be subclasses of NovaPlugin.
public func launch(plugins: [NovaPlugin])
}
示例
let plugins = [
AnrMonitorPlugin(),
MemoryLeakMonitorPlugin(),
UIThreadMonitorPlugin(),
]
Nova.shared.launch(plugins: plugins)
for plugin in plugins {
plugin.start()
}
Nova日志
Nova默认不输出日志到控制台。如果需要在Nova内部获取日志,首先要设置App的Nova日志代理属性,然后实现NovaLogDelegate协议。
public class Nova {
/// Nova does not output logs by default. If you need to get the log information in the Nova, you need to set the logDelegate and implement the corresponding method.
public weak var logDelegate: NovaLogDelegate?
}
示例
Nova.shared.logDelegate = self
extension NovaManager: NovaLogDelegate {
public func shouldLog(level: NovaLogLevel) -> Bool {
level == .warn || level == .error
}
public func novaLog(level: NovaLogLevel, module: String, file: String, function: String, line: Int, message: String) {
// print log
}
}
监控问题
应用层可以监控插件是否生成问题,并决定是否报告问题。
public class Nova {
/// Add Nova listener. For example, when an issue occurs, the `onReport` callback will be called
/// - Parameter listener: listening instance
public func addListener(_ listener: NovaDelegate)
}
示例
Nova.shared.addListener(self)
extension NovaManager: NovaDelegate {
public func onReport(_ issue: NovaIssue) {
reporter.report(issue: issue)
}
}
启动 NovaUI
public struct NovaUIConfig {
/// If the log is enabled, when an issue occurs, SDK will write the issue log to the local file.
public var isIssueLogEnabled: Bool
/// If issue notification is enabled, when an issue occurs, an issue notification will pop up in the App.
public var isIssueNotificationEnabled: Bool
/// If the floating window is enabled, the floating window related to performance indicators (cpu, memory, FPS) will be displayed in the App.
public var isFloatingWindowEnabled: Bool
public init(isIssueLogEnabled: Bool, isIssueNotificationEnabled: Bool, isFloatingWindowEnabled: Bool)
}
public class NovaUI {
public func launch(defaultConfig: NovaUIConfig)
}
示例
let uiConfig = NovaUIConfig(isIssueLogEnabled: true, isIssueNotificationEnabled: true, isFloatingWindowEnabled: true)
NovaUI.shared.launch(defaultConfig: uiConfig)
浮动窗口
实时显示当前应用性能指标,包括
- 内存使用
- CPU 使用
- 帧率信息
浮动窗口的默认开关可以通过 NovaUIConfig 中的 isFloatingWindowEnabled
属性来控制。
插件介绍
插件 | 描述 | 第三方依赖 |
---|---|---|
AnrMonitorPlugin | 监控主线程的运行状态,如果主线程的阻塞超过了阈值 | |
PageMonitorPlugin | 页面创建耗时统计,纯粹的统计插件,不会报告问题 | VCProfiler |
MemoryLeakMonitorPlugin | 通过观察对象创建和回收来确认是否有内存泄漏 | MLeaksFinder |
UIThreadMonitorPlugin | 监控在子线程中是否有操作更新 UI |