name: Mobile Performance Profiling description: 移动应用性能分析与优化 version: 1.0.0 category: 性能 slug: mobile-perf status: 活跃
移动性能剖析技能
概述
本技能提供移动应用性能分析与优化能力。它支持使用 Xcode Instruments、Android Profiler、Flipper 和 Flutter DevTools 进行性能剖析,以识别和修复性能问题。
允许使用的工具
bash- 执行剖析工具和构建命令read- 分析性能报告和剖析文件write- 生成优化配置edit- 更新性能相关代码glob- 搜索性能文件grep- 搜索模式
能力
Xcode Instruments
-
时间剖析器
- 剖析 CPU 使用率
- 识别热点路径
- 分析调用树
- 测量函数持续时间
-
内存分配
- 跟踪内存分配
- 识别内存泄漏
- 分析循环引用
- 监控堆增长
-
核心动画
- 剖析渲染性能
- 识别离屏渲染
- 测量帧率
- 检测图层问题
Android Profiler
-
CPU 剖析器
- 记录方法跟踪
- 分析线程活动
- 识别 CPU 瓶颈
- 剖析启动时间
-
内存剖析器
- 跟踪内存分配
- 捕获堆转储
- 识别内存泄漏
- 分析垃圾回收事件
-
网络剖析器
- 监控网络请求
- 分析响应时间
- 剖析带宽使用
React Native
-
Flipper 性能
- 监控 React 渲染
- 剖析 JavaScript 线程
- 分析桥接通信
- 跟踪原生模块
-
Hermes 剖析器
- 剖析 Hermes 引擎
- 分析字节码执行
- 优化包大小
Flutter DevTools
- 性能叠加层
- 监控帧渲染
- 识别卡顿
- 剖析组件重建
- 分析时间线
指标
-
应用启动
- 冷启动优化
- 温启动测量
- 热启动剖析
- 延迟初始化
-
内存管理
- 峰值内存使用
- 内存警告处理
- 图片缓存策略
- 对象池化
-
渲染性能
- 帧率维持
- 滚动性能
- 动画流畅度
- 布局优化
目标流程
mobile-performance-optimization.js- 性能调优mobile-testing-strategy.js- 性能测试jetpack-compose-ui.js- Compose 优化swiftui-app-development.js- SwiftUI 优化
依赖项
- Xcode Instruments
- Android Studio Profiler
- Flipper
- Flutter DevTools
使用示例
Instruments 命令行
# 记录时间剖析器跟踪
xcrun xctrace record --device "iPhone 15 Pro" \
--template "Time Profiler" \
--attach "MyApp" \
--time-limit 30s \
--output ~/Desktop/profile.trace
# 导出跟踪数据
xcrun xctrace export --input ~/Desktop/profile.trace \
--xpath '/trace-toc/run/tracks/track[@name="Time Profiler"]' \
--output ~/Desktop/profile_data.xml
Android Profiler 命令
# 捕获 CPU 跟踪
adb shell am profile start com.example.myapp /data/local/tmp/profile.trace
# 停止并拉取跟踪文件
adb shell am profile stop com.example.myapp
adb pull /data/local/tmp/profile.trace ./profile.trace
# 捕获堆转储
adb shell am dumpheap com.example.myapp /data/local/tmp/heap.hprof
adb pull /data/local/tmp/heap.hprof ./heap.hprof
React Native 性能优化
// 优化列表渲染
import { FlashList } from '@shopify/flash-list';
const OptimizedList = () => {
const renderItem = useCallback(({ item }) => (
<MemoizedListItem item={item} />
), []);
return (
<FlashList
data={items}
renderItem={renderItem}
estimatedItemSize={100}
keyExtractor={item => item.id}
/>
);
};
// 记忆化昂贵组件
const MemoizedListItem = memo(({ item }) => {
return (
<View style={styles.item}>
<Text>{item.title}</Text>
</View>
);
}, (prev, next) => prev.item.id === next.item.id);
// 优化图片
import FastImage from 'react-native-fast-image';
const OptimizedImage = ({ uri }) => (
<FastImage
source={{ uri, priority: FastImage.priority.normal }}
resizeMode={FastImage.resizeMode.cover}
style={styles.image}
/>
);
SwiftUI 性能
// 优化视图更新
struct OptimizedView: View {
@State private var items: [Item] = []
var body: some View {
List {
ForEach(items) { item in
ItemRow(item: item)
.equatable() // 防止不必要的重绘
}
}
}
}
// 为自定义视图使用 Equatable
struct ItemRow: View, Equatable {
let item: Item
static func == (lhs: ItemRow, rhs: ItemRow) -> Bool {
lhs.item.id == rhs.item.id
}
var body: some View {
Text(item.title)
}
}
// 懒加载
struct LazyImageView: View {
let url: URL
var body: some View {
AsyncImage(url: url) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image.resizable().aspectRatio(contentMode: .fit)
case .failure:
Image(systemName: "photo")
@unknown default:
EmptyView()
}
}
}
}
Compose 性能
// 优化重组
@Composable
fun OptimizedList(items: List<Item>) {
LazyColumn {
items(
items = items,
key = { it.id }
) { item ->
// 稳定的键可防止不必要的重组
ItemCard(item = item)
}
}
}
// 使用 derivedStateOf 处理计算值
@Composable
fun SearchableList(items: List<Item>, query: String) {
val filteredItems by remember(items, query) {
derivedStateOf {
items.filter { it.title.contains(query, ignoreCase = true) }
}
}
LazyColumn {
items(filteredItems, key = { it.id }) { ItemCard(it) }
}
}
// 使用 remember 处理昂贵计算
@Composable
fun ExpensiveView(data: ComplexData) {
val processedData = remember(data) {
expensiveCalculation(data)
}
Text(processedData.result)
}
质量门限
- 应用启动 < 2秒 (冷启动), < 1秒 (温启动)
- 帧率 >= 60fps (ProMotion 设备 >= 120fps)
- 内存使用在平台限制内
- 未检测到内存泄漏
- 滚动性能流畅
相关技能
mobile-testing- 性能测试react-native-dev- RN 优化flutter-dart- Flutter 优化kotlin-compose- Compose 优化
版本历史
- 1.0.0 - 初始版本