跳到主要内容
本页目录

性能分析

没有 Instruments 也能分析性能。kxdevice 直接从真机采集 CPU、内存分配、网络、耗电、掉帧和调用栈,命令行或网页界面都可以。

准备

1

安装 Release 包

Debug 包没有优化,带着断言和调试信息,测出的数据和用户手中的不同。

> kxbuild build .\MyApp.xcodeproj -c Release --install
2

启动 App 并获取 PID

多数采样命令按进程号过滤。用 kxdevice 启动会直接打印 PID,App 已在运行时用 ps 查找。

> kxdevice apps launch com.example.myapp
> kxdevice apps ps
3

选择采样器

下表按问题类型列出对应命令。energy 采样一次就结束,其余命令默认持续采样到 Ctrl-C,加 -n 限定次数,多台手机时加 --udid。


Instruments 模板对照

问题Instruments 模板命令
CPU 占用高、耗电Activity Monitorinstruments sysmon -i 1
热点函数、卡顿位置Time Profilerinstruments stackshot
内存增长、疑似泄漏Allocations / Leaksinstruments allocations --pid <pid> --backtrace
滚动掉帧、动画不流畅Core Animation / Animation Hitchesinstruments fluidity --pid <pid>
请求慢、流量大Networkinstruments netmon / instruments har --pid <pid> -o a.har
耗电Energy Loginstruments energy <pid>
os_log / signpost 打点os_signpost / Logginginstruments activitytrace --pid <pid>
系统调用、线程调度System Traceinstruments coreprofile
弱网、高温环境Condition inducerinstruments condition list,再 enable …

命令前面都要加 kxdevice。例如记录某个进程的内存分配和回溯

> kxdevice instruments allocations --pid 1234 --backtrace -n 20

常见问题的排查方法

列表滑动卡顿

先用 fluidity 监控掉帧事件并复现滑动,再用 stackshot 查看掉帧时段主线程停在哪个函数。Release 包默认剥离符号,stackshot 中只有地址时,构建时加 -D STRIP_INSTALLED_PRODUCT=NO。

内存持续增长

用 allocations --backtrace 记录一段时间,反复进出同一页面几次,查看哪些分配只增不减、回溯落在哪个类,总内存趋势在下面网页界面的内存曲线里看。

弱网下加载慢

用 condition enable SlowNetworkCondition SlowNetwork3GGood 把手机切到模拟 3G,再用 har 抓取请求,查看哪个接口最慢、是否串行等待、有无重复请求。导出的 .har 用浏览器开发者工具打开。测完执行 condition disable。

获取崩溃日志

测试机上的崩溃报告可以直接拉取。

> kxdevice crash pull .\crashes

网页界面

不想记命令可以启动本地网页,CPU 和内存曲线、进程列表、网络监控、崩溃报告、系统日志都在其中,浏览器打开 http://localhost:8780。

> kxdevice web

接下来