本文整理于iOS develop library的performance overview
1. 性能分类
性能主要分为:系统资源使用情况以及快速响应给用户。前者由于系统资源是有限的,包括cpu、内存、存储空间等,后者是程序快速响应用户操作,比如启动时间。
CPU time:尽可能地将线程移出CPU,优化CPU、优化绘制代码。
内存:减少程序的内存以及内存页的占用。
存储空间:无论是本地文件系统和网络文件系统都是性能的最大瓶颈之一。优化目标是消除不必要的文件操作。
2. 性能测试的大致过程
1)建立基准线:基准线的建立方法有竞争对手、之前的build,另外基准线的建立要保持一致性且尽量使用最慢的硬件配置;
2)尽早测试、频繁测试;
3)分析测试结果,将测试结果视图化;
4)算法分析和优化;
3. 性能测试的方法
instruments, watch code in the debugger,logger checkpoints,编码替代解决方案;
4. 性能测试的几个关键测试点
1)关键流程代码
2)界面绘制代码
3)启动时间:预先计算库地址范围并且存储在binary里
4)读取文件:理解系统cache文件、利用系统cache;顺序读写文件;避免不必要的文件关闭、重打开;
5)程序占用:code pages包含不需要使用的代码;静态变量、常量存储在可写页;程序输出一些不必要的symbols;代码没有优化;包含了过多frameworks。
6)内存分配:尽量使用ARC、注意内存泄露
5. 性能优化的几点建议
1) be lazy 尽可能偷懒
2)注意尽快响应用户操作,有一些任务放到后台执行、使用调度队列或者在合适的时间执行
3)使用事件驱动处理方式:好处有尽快响应用户操作、减少cpu使用率、减少应用的工作集、减少电量的使用。
4)提高程序的并发任务:使用调度队列,主线程主要用于处理用户操作更新界面。
5)使用Accelerate Framework
6)更新应用,尽可能地使用最新的framework、api
6. 性能测试工具
1)Instrument:跟踪性能、静态采样、并且图形化展示,文件系统读写、垃圾回收、对象和内存分配、内存泄露、进程和系统级的活动等等。
2)分析工具(注意这些工具不适用于分析iOS app)
分析工具位置:<Xcode>/Applications/Performance Tools,cmd工具位置:/usr/bin.
MallocDebug
Tracks and analyzes memory allocated in an application. You can use
this tool to find memory leaks or analyze memory allocation patterns.
OpenGL Driver Monitor
Gathers GPU-related performance data, including data related to VRAM
usage, video bus traffic, and hardware stalls among others. You can use
this information to identify the cause of temporary slowdowns or
sporadic hesitations in your OpenGL application.
OpenGL Profiler
Creates a runtime profile of your OpenGL-based application. You can
view function statistics and the call-trace history of your
application’s OpenGL calls.
Saturn
Instruments your code to provide function-level profiling and
displays the resulting data graphically. You can use this tool to count
events, such as how many times a function is called or an event is sent.
heap
Lists all malloc-allocated buffers in the heap of a specified process
leaks
Searches the memory space of a process for any allocated but unreferenced blocks of memory.
vmmap Displays the virtual memory regions allocated to a specified process. You can use this tool to analyze the memory usage of your process
3)监视工具(注意这些工具不适用于分析iOS app)
Activity Monitor Displays common usage statistics relating to memory and CPU usage
for the currently running processes. You can also initiate the sampling
of a process from this application. This tool provides information that
is similar to that of the top tool.
BigTop
Displays system statistics, such as CPU, disk, network and memory
usage graphically over time. You can monitor a single process or all
processes. This tool provides information that is similar to that of the top and vm_stat tools.
Quartz Debug
Shows screen updates in real time by briefly flashing the areas
being redrawn. You can use this tool to analyze your application’s
drawing behavior.
Spin Control
Samples programs that cause the spinning cursor to appear. Leave
this program running in the background to catch unresponsive
applications at critical moments.
Thread Viewer
Graphically displays activity across a range of threads. It provides
color-coded timeline views of thread activity and can display
backtraces of activity at specific points in time. (Available in OS X
v10.5 and earlier.)
fs_usage
Displays an ongoing list of file-system activity, as generated by
page faults and calls to file-system functions. You can use this tool to
understand the file access patterns of your program.
sc_usage
Displays an ongoing list of system call and page fault statistics.
top Displays common system usage statistics relating to memory and CPU usage for the currently running processes. This tool updates the information dynamically so that you can see trends at runtime.
4)硬件分析工具(注意这些工具不适用于分析iOS app)
Reggie SE
Lets you examine and modify CPU and PCI configuration registers.
acid
A command-line tool that analyzes TT6E instruction traces and
presents detailed analyses and histograms. You can use this tool to
detect bad instruction sequences, such as misaligned operands, data
dependency stalls, and spilled loads.
simg4
A command-line tool that is a cycle-accurate simulator of the
Motorola 7400 processor. This tool takes TT6 traces as input. (Available
in OS X v10.5 and earlier.)
simg5 A command-line tool that is a cycle-accurate simulator of the IBM 970 processor. This tool takes TT6 traces as input. (Available in OS X v10.5 and earlier.)
5)一些cmd工具
atos
Converts back and forth between a symbol name and the numeric address of that symbol in a running executable.
c2ph
Displays the C-structures from an object file along with their member offset values.
gprof
Produces execution profiles based on an execution analysis of a program.
malloc_history
Shows the malloc allocations performed by a specified process.
nm
Displays the symbol table information for one or more object files.
otool
Displays the contents of a Mach-O executable in a more human-readable form
pagestuff
Displays information about the logical pages of a Mach-O executable file.
pstruct
Parses the C structures from an object file and displays them along with their member offset values.
sample
Produces an execution profile based on the execution analysis of a program.
vm_stat Displays Mach virtual memory statistics, including the number of active, inactive, wired, and free pages. This tool also displays page fault and other activity information.
7. 性能评估
1)使用top:top工具会显示各个进程的CPU使用率、内存使用、资源使用情况等。BigTop会跟踪cpu等随着时间的变化。
2)instrument工具:他会图像化显示性能数据。
3)quartz debug:用于查看界面绘制代码的性能
4)Spin Control:应用程序响应慢的时候,自动采样数据
欢迎访问我的博客: www.dzwanli.com.cn