1. 性能分類
性能主要分為:系統(tǒng)資源使用情況以及快速響應(yīng)給用戶。前者由于系統(tǒng)資源是有限的,包括cpu、內(nèi)存、存儲(chǔ)空間等,后者是程序快速響應(yīng)用戶操作,比如啟動(dòng)時(shí)間。
CPU time:盡可能地將線程移出CPU,優(yōu)化CPU、優(yōu)化繪制代碼。
內(nèi)存:減少程序的內(nèi)存以及內(nèi)存頁(yè)的占用。
存儲(chǔ)空間:無(wú)論是本地文件系統(tǒng)和網(wǎng)絡(luò)文件系統(tǒng)都是性能的最大瓶頸之一。優(yōu)化目標(biāo)是消除不必要的文件操作。
2. 性能測(cè)試的大致過程
1)建立基準(zhǔn)線:基準(zhǔn)線的建立方法有競(jìng)爭(zhēng)對(duì)手、之前的build,另外基準(zhǔn)線的建立要保持一致性且盡量使用最慢的硬件配置;
2)盡早測(cè)試、頻繁測(cè)試;
3)分析測(cè)試結(jié)果,將測(cè)試結(jié)果視圖化;
4)算法分析和優(yōu)化;
3. 性能測(cè)試的方法
instruments, watch code in the debugger,logger checkpoints,編碼替代解決方案;
4. 性能測(cè)試的幾個(gè)關(guān)鍵測(cè)試點(diǎn)
1)關(guān)鍵流程代碼
2)界面繪制代碼
3)啟動(dòng)時(shí)間:預(yù)先計(jì)算庫(kù)地址范圍并且存儲(chǔ)在binary里
4)讀取文件:理解系統(tǒng)cache文件、利用系統(tǒng)cache;順序讀寫文件;避免不必要的文件關(guān)閉、重打開;
5)程序占用:code pages包含不需要使用的代碼;靜態(tài)變量、常量存儲(chǔ)在可寫頁(yè);程序輸出一些不必要的symbols;代碼沒有優(yōu)化;包含了過多frameworks。
6)內(nèi)存分配:盡量使用ARC、注意內(nèi)存泄露
5. 性能優(yōu)化的幾點(diǎn)建議
1) be lazy 盡可能偷懶
2)注意盡快響應(yīng)用戶操作,有一些任務(wù)放到后臺(tái)執(zhí)行、使用調(diào)度隊(duì)列或者在合適的時(shí)間執(zhí)行
3)使用事件驅(qū)動(dòng)處理方式:好處有盡快響應(yīng)用戶操作、減少cpu使用率、減少應(yīng)用的工作集、減少電量的使用。
4)提高程序的并發(fā)任務(wù):使用調(diào)度隊(duì)列,主線程主要用于處理用戶操作更新界面。
5)使用Accelerate Framework
6)更新應(yīng)用,盡可能地使用最新的framework、api
6. 性能測(cè)試工具
1)Instrument:跟蹤性能、靜態(tài)采樣、并且圖形化展示,文件系統(tǒng)讀寫、垃圾回收、對(duì)象和內(nèi)存分配、內(nèi)存泄露、進(jìn)程和系統(tǒng)級(jí)的活動(dòng)等等。
2)分析工具(注意這些工具不適用于分析iOS app)
分析工具位置:/Applications/Performance Tools,cmd工具位置:/usr/bin.
MallocDebugTracks and analyzes memory allocated in an application. You can use this tool to find memory leaks or analyze memory allocation patterns.
OpenGL Driver MonitorGathers 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 ProfilerCreates a runtime profile of your OpenGL-based application. You can view function statistics and the call-trace history of your application’s OpenGL calls.
SaturnInstruments 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.
heapLists all?malloc-allocated buffers in the heap of a specified process
leaksSearches the memory space of a process for any allocated but unreferenced blocks of memory.
vmmapDisplays the virtual memory regions allocated to a specified process. You can use this tool to analyze the memory usage of your process
3)監(jiān)視工具(注意這些工具不適用于分析iOS app)
Activity MonitorDisplays 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.
BigTopDisplays 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 DebugShows 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 ControlSamples programs that cause the spinning cursor to appear. Leave this program running in the background to catch unresponsive applications at critical moments.
Thread ViewerGraphically 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_usageDisplays 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_usageDisplays an ongoing list of system call and page fault statistics.
topDisplays 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 SELets you examine and modify CPU and PCI configuration registers.
acidA 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.
simg4A 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.)
simg5A 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工具
atosConverts back and forth between a symbol name and the numeric address of that symbol in a running executable.
c2phDisplays the C-structures from an object file along with their member offset values.
gprofProduces execution profiles based on an execution analysis of a program.
malloc_historyShows the?malloc?allocations performed by a specified process.
nmDisplays the symbol table information for one or more object files.
otoolDisplays the contents of a Mach-O executable in a more human-readable form
pagestuffDisplays information about the logical pages of a Mach-O executable file.
pstructParses the C structures from an object file and displays them along with their member offset values.
sampleProduces an execution profile based on the execution analysis of a program.
vm_statDisplays 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. 性能評(píng)估
1)使用top:top工具會(huì)顯示各個(gè)進(jìn)程的CPU使用率、內(nèi)存使用、資源使用情況等。BigTop會(huì)跟蹤cpu等隨著時(shí)間的變化。
2)instrument工具:他會(huì)圖像化顯示性能數(shù)據(jù)。
3)quartz debug:用于查看界面繪制代碼的性能
4)Spin Control:應(yīng)用程序響應(yīng)慢的時(shí)候,自動(dòng)采樣數(shù)據(jù)
文/chichimei(簡(jiǎn)書作者)
轉(zhuǎn)載原文鏈接:http://www.itdecent.cn/p/5315ecff69a2
著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡(jiǎn)書作者”。