版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】BenchmarkingAndProfiling(性能評(píng)測(cè)和剖析)
Beingabletomeasureperformanceisrequired針對(duì)性能進(jìn)行度量inordertodeterminewhetheroptimizationsareneeded是否需要優(yōu)化,andwhethertheoptimizationsactuallyimprovedanything優(yōu)化后是否有提升.
Performanceinmostcaseswillbemeasuredasafunctionofthetimeittakestocompleteanoperation.Forexample,theperformanceofagamewillveryoftenbemeasuredinhowmanyframespersecondcanberendered每秒渲染多少幀來(lái)度量,whichdirectlydependsonhowmuchtimeittakestorenderframes:toachieveaconstantframerate恒定數(shù)率of60framespersecond,eachframeshouldtakelessthan16.67millisecondstorenderanddisplay.Also,aswediscussedinChapter1,aresponsetimeof100millisecondsisoftendesiredinorderforresultstoappearinstantaneous有瞬時(shí)的感覺.
Inthischapteryoulearnthevariouswaysofmeasuringtimeinyourapplication.Youalsolearnhowtouseaprofilingtool剖析工具,Traceview,totraceJavacodeandnativecodeandeasilyidentifybottlenecks瓶頸inyourapplication.Finally,youlearnabouttheloggingmechanisminAndroidandhowtotakeadvantageofthefilteringcapabilities日志過(guò)濾機(jī)制的方法.6.1MeasuringTime測(cè)量時(shí)間
Howmuchtimeanoperation操作orsequenceofoperations操作序列takestocompleteisacriticalpieceofinformationwhenitistimetooptimizecode.Withoutknowinghowmuchtimeisspentdoingsomething,youroptimizationsareimpossibletomeasure.JavaandAndroidprovidethefollowingsimpleAPIsyourapplicationcanusetomeasuretimeandthereforeperformance:System.currentTimeMillisSystem.nanoTimeDebug.threadCpuTimeNanosSystemClock.currentThreadTimeMillisSystemClock.elapsedRealtimeSystemClock.uptimeMillis
Typically,yourapplicationneedstomaketwocallstothesemethodsasasinglecallishardlymeaningful.Tomeasuretime,yourapplicationneedsastarttimeandanendtime,andperformanceismeasuredasthedifferencebetweenthesetwovalues.Attheriskofsoundingoverlypatronizing,nowisagoodtimetostatethatthereare1,000,000,000nanosecondsinonesecond,orinotherwords,ananosecondisonebillionthofasecond.
NOTE:Eventhoughsomemethodsreturnatimeexpressedinnanoseconds,itdoesnotimplynanosecondaccuracy納秒級(jí)的.Theactualaccuracydependsontheplatformandmaydifferbetweendevices.Similarly,System.currentTimeMillis()returnsanumberofmillisecondsbutdoesnotguaranteemillisecondaccuracy.
AtypicalusageisshowninListing6–1.Listing6–1.MeasuringTimelongstartTime=System.nanoTime();//performoperationyouwanttomeasureherelongduration=System.nanoTime()-startTime;System.out.println(“Duration:”+duration);
AnimportantdetailisthefactthatListing6–1doesnotuseanythingAndroid-specific.Asamatteroffact,thismeasurementcodeisonlyusingthejava.lang.System,java.lang.Stringandjava.io.PrintStreampackages.Consequently,youcouldusesimilarcodeinanotherJavaapplicationthatisnotmeanttorunonanAndroiddevice.TheDebugandSystemClockclassesare,ontheotherhand,Android-specific.WhileSystem.currentTimeMillis()waslistedasamethodtomeasuretime,itisactuallynotrecommendedtousethismethod,fortworeasons:
Itsprecisionandaccuracy精度準(zhǔn)確度maynotbegoodenough.
Changingthesystemtimecanaffecttheresults.
Instead,yourapplicationshoulduseSystem.nanoTime()asitoffersbetterprecisionandaccuracy.6.1.1System.nanoTime()
Becausethereferencetimeisnotdefined,youshouldonlyuseSystem.nanoTime()tomeasuretimeintervals,asshowninListing6–1.Togetthetime(asaclock),useSystem.currentTimeMillis()asitdefinesthereturnvalueasthenumberofmillisecondssinceJanuary1,197000:00:00UTC.Listing6–2showsyouhowtomeasure,roughly,thetimeittakesforSystem.nanoTime()tocomplete.Listing6–2.MeasuringSystem.nanoTime()privatevoidmeasureNanoTime(){finalintITERATIONS=100000;longtotal=0;longmin=Long.MAX_VALUE;longmax=Long.MIN_VALUE;for(inti=0;i<ITERATIONS;i++){longstartTime=System.nanoTime();longtime=System.nanoTime()-startTime;total+=time;if(time<min){min=time;}if(time>max){max=time;}}Log.i(TAG,"Averagetime:"+((float)total/ITERATIONS)+"nanoseconds");Log.i(TAG,"Minimum:"+min);Log.i(TAG,"Maximum:"+max);}OnaSamsungGalaxyTab10.1,theaveragetimeisabout750nanoseconds.
NOTE:HowmuchtimeacalltoSystem.nanoTime()takesdependsontheimplementationandthedevice.
Becausethescheduler調(diào)度器isultimatelyresponsibleforschedulingthreadstorunontheprocessingunits,theoperationyouwanttomeasuremaysometimesbeinterrupted,possiblyseveraltimes,tomakeroomforanotherthread.Therefore,yourmeasurementmayincludetimespentonexecutingsomeothercode,whichcanmakeyourmeasurementincorrect,andthereforemisleading.
Tohaveabetterideaofhowmuchtimeyourowncodeneedstocomplete,youcanusetheAndroid-specificDebug.threadCpuTimeNanos()method.
6.1.2Debug.threadCpuTimeNanos()
Becauseitmeasuresonlythetimespentinthecurrentthread,
Debug.threadCpuTimeNanos()shouldgiveyouabetterideaofhowmuchtimeyourowncodetakestocomplete.However,ifwhatyouaremeasuringisexecutedinmultiplethreads,asinglecalltoDebug.threadCpuTimeNanos()won’tgiveyouanaccurateestimate,andyouwouldhavetocallthismethodfromallthethreadsofinterestandsumtheresults.Listing6–3showsasimpleexampleofhowDebug.threadCpuTimeNanos()canbeused.TheusageisnodifferentfromSystem.nanoTime()’s,anditshouldonlybeusedtomeasureatimeinterval.Listing6–3.UsingDebug.threadCpuTimeNanos()longstartTime=Debug.threadCpuTimeNanos();//warning:thismayreturn-1ifthesystemdoesnotsupportthisoperation//simplysleepforonesecond(otherthreadswillbescheduledtorunduringthattime)try{TimeUnit.SECONDS.sleep(1);//sameasThread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}longduration=Debug.threadCpuTimeNanos()-startTime;Log.i(TAG,"Duration:"+duration+"nanoseconds");
WhilethecodewilltakeaboutonesecondtocompletebecauseofthecalltoTimeUnit.SECONDS.sleep(),theactualtimespentexecutingcodeismuchless.Infact,runningthatcodeonaGalaxyTab10.1showsthatthedurationisonlyabout74microseconds.ThisisexpectedasnothingmuchisdoneinbetweenthetwocallstoDebug.threadCpuTimeNanos()otherthanputtingthethreadtosleepforonesecond.
NOTE:RefertotheTimeUnitclassdocumentation.TimeUnitoffersconvenientmethodsforconvertingtimebetweendifferentunitsandalsoperformingthread-relatedoperationssuchas
Thread.join()andObject.wait().Ofcourse,youcanalsomeasuretimeinyourapplication’sCcodeusing“standard”Ctimefunctions,asshowninListing6–4.Listing6–4.UsingCTimeFunction#include<time.h>voidfoo(){doubleduration;time_ttime=time(NULL);//dosomethinghereyouwanttomeasureduration=difftime(time(NULL),time);//durationinseconds}
6.2Tracing方法調(diào)用跟蹤
Onceyouidentifywhatistakingtoomuchtime,youprobablywanttobeabletoknowinmoredetailwhichmethodsaretheculprits×××.Youcandothisbycreatingtracefileswiththehelpofatracing-specificmethod,andthenanalyzethemwiththeTraceviewtool.6.2.1Debug.startMethodTracing()
AndroidprovidestheDebug.startMethodTracing()methodtocreatetracefilesthatcanthenbeusedwiththeTraceviewtooltodebugandprofileyourapplication.ThereareactuallyfourvariantsoftheDebug.startMethodTracing()method:startMethodTracing()startMethodTracing(StringtraceName)startMethodTracing(StringtraceName,intbufferSize)startMethodTracing(StringtraceName,intbufferSize,intflags)
ThetraceNameparameterspecifiesthenameofthefiletowritethetraceinformationinto寫入跟蹤信息的文件名.(Ifthefilealreadyexistsitwillbetruncated.)Youneedtomakesureyourapplicationhaswriteaccesstothisfile.(Bydefault,thefilewillbecreatedinthesdcarddirectoryunlessanabsolutepathisgiven.)ThebufferSizeparameterspecifiesthemaximumsizeofthetracefile.Traceinformationcanuseafairamountofspaceandyourstoragecapacitymaybelimitedsotrytouseasensiblevalue(defaultis8MB).Androidcurrentlydefinesonlyoneflag,Debug.TRACE_COUNT_ALLOCS,sotheflagsparametershouldbesettoeither0orDebug.TRACE_COUNT_ALLOCS(toaddtheresultsfromDebug.startAllocCounting()tothetrace,thatis,thenumberandaggregatesizeofmemoryallocations).AndroidalsoprovidestheDebug.stopMethodTracing()method,which,youwouldhaveguessed,stopsthemethodtracing.Theusageisverysimilartotimemeasurementsseenearlier,asshowninListing6–5.Debug.startMethodTracing(“/sdcard/awesometrace.trace”);//performoperationyouwanttotracehereBigIntegerfN=FputeRecursivelyWithCache(100000);Debug.stopMethodTracing();//nowthereshouldbeafilenamedawesometrace.tracein/mnt/sdcard,getitinEclipseDDMS
ThetracefilewillbesavedontoyourAndroiddevice(ortheemulator),soyouwillneedtocopythefilefromthedevicetoyourhostmachine,forexampleinEclipse(usingDDMS)orwiththe“adbpull”command.6.2.2UsingtheTraceviewTool
TheAndroidSDKcomeswithatoolnamedTraceview,whichcanusethesetracefilesandgiveyouagraphicalrepresentationofthetrace,asshowninFigure6–1.YoucanfindtheTraceviewtoolintheSDK’stoolsdirectoryandsimplytypetraceviewawesometrace.traceonacommandlinetostartTraceview.Figure6–1.Traceviewwindow
Thetracebasicallycontainsthelistofallthefunctioncallstogetherwithhowmuchtimethesecallstookandhowmanyweremade.Sevencolumnsaredisplayed:Name:thenameofthemethodIncl%:thepercentageoftimespentinthatmethod(includingchildrenmethods)此方法中占的時(shí)間百分比Inclusive:thetimeinmillisecondsspentinthatmethod(includingchildrenmethods)Excl%:thepercentageoftimespentinthatmethod(excludingchildrenmethods)Exclusive獨(dú)家新聞:thetimeinmillisecondsspentinthatmethod(excludingchildrenmethods)Calls+RecurCalls/Total:thenumberofcallsandrecursivecallsTime/Call:theaveragetimepercallinmillisecondsForexample,atotalof14callstoBigInteger.multiply()weremadeforatotalof10.431milliseconds,or745microsecondspercall.BecausetheVMwillrunmoreslowlywhentracingisenabled,youshouldnotconsiderthetimevaluesasdefinitivenumbers.Instead,usethesetimevaluessimplytodeterminewhichmethodorrunisfaster.Ifyouclickonamethodname,Traceviewwillshowyoumoredetailedinformationforthatspecificmethod,asshowninFigure6–2.Thisincludes:Parents(themethodscallingthismethod)Children(themethodscalledbythismethod)Parentswhilerecursive(ifmethodisrecursive)Childrenwhilerecursive(ifmethodisrecursive)AsyoucanseeinFigure6–2,mostofthetimeisspentonfourmethods:BigInteger.multiply()BigInteger.pow()BigInteger.add()BigInteger.shiftLeft()
EventhoughweestablishedwherethebottleneckswereinChapter1already,Traceviewallowsyoutoveryquicklydeterminewheretheycanbewithouthavingtoperformanyexpansiveresearch進(jìn)一步花精力研究.Inthisparticularcase,youcanquicklyseethatBigInteger.multiply()iswheremostofthetimeisspent,followedbyBigInteger.pow().Thisisnotsurprisingasmultiplicationsareintuitively直觀的morecomplicatedthanadditionsandshiftsdonebyBigInteger.add()andBigInteger.shiftLeft().Figure6–2.Amethod’sdetailedinformation
Atthetopofthewindow,youcanseethetimelineforthemainthread.Youcanzoom急速上升inbyselectingacertainregioninthistimeline,andzoomoutbydouble-clickingonthetimescale.FamiliarizeyourselfwiththeTraceviewtoolandlearnhowtonavigatefromonemethodtoanother.
Hint:it’seasy.Justclickonamethod’sname!
BecausetheJust-In-Timecompilerisdisabledwhentracingisenabled,theresultsyougetcanbesomewhatmisleading.Infact,youmaythinkamethodtakesacertaintimewheninrealityitcanbemuchfastersinceitcanbecompiledintonativecodebytheDalvikJust-In-Timecompiler.Also,thetracewon’tshowyouhowmuchtimeisspentinnativefunctions.Forexample,Figure6–1showscallstoNativeBN.BN_mul()andNativeBN.BN_exp(),butifyouclickonthesemethods,youwon’tseewhatothermethodstheymaycall.6.2.3TraceviewinDDMS
AnotherwaytotracecallsanduseTraceviewistogenerateatracefiledirectlyfromtheEclipseDDMSperspective觀點(diǎn)(視圖).Afteryouselectaspecificprocess,youcanclickontheStartMethodProfilingicon,andthenclickagaintostopprofiling.Onceyoustopprofiling,thetracewillbevisibleintheDebugperspectiveinEclipseanditwillbelikeusingTraceview.Figure6–3showshowtostartmethodprofilingfromtheDDMSperspective,andFigure6–4showsyouthemethodprofilingviewintheDebugperspective.Figure6–3.StartingmethodprofilingfromtheDDMSperspectiveFigure6–4.MethodprofilingintheDebugperspectiveAsyoucanseeinFigure6–4,timelinesformultiplethreadscanbeshown.
Traceviewisnotperfect,howeveritcangiveyougreatinsightintowhatcodeisactuallyexecutedandwherethebottlenecksmaybe.Whenitistimetoachievebetterperformance,thisshouldbeoneofyourpreferredtoolstodetectwhereyoushouldfocusyourattention.
TIP:Remembertodeletethetracefileswhenyouaredonewithyourdebuggingandprofiling.YoucanusetheEclipseDDMSperspectivetodeletefilesfromyourdevice.6.2.4NativeTracing本地方法追蹤
InadditiontoprofilingJavamethodswiththestartMethodTracing()APIs,Androidalsosupportsnativetracing(includingkernelcode).NativetracingisalsoreferredtoasQEMUtracing.InthissectionyoulearnhowtogeneratetheQEMUtracefilesandhowtoconvertthemintoafileTraceviewcaninterpret..TogenerateQEMUtraces,youhavetodotwothings:Starttheemulatorusingthe–traceoption(forexample,“emulator–tracemytrace–avdmyavd”).Startandthenstopnativetracing,eitherbycallingDebug.startNativeTracing()andDebug.stopNativeTracing(),orbypressingtheF9key(thefirsttimewillstarttracing,thesecondtimewillstoptracing).IntheAVD’stracesdirectoryonyourhostmachine,youwillthenfindamytracedirectorycontainingseveralQEMUemulatortracefiles:qtrace.bbqtrace.excqtrace.insnqtrace.methodqtrace.pidqtrace.staticNOTE:QEMUisanopen-sourceemulator.Refertoformoreinformation.GeneratingTraceFileForTraceviewTousethetracesinTraceviewlikewedidforJavamethods,youneedtogenerateatracefilethatTraceviewcanunderstand.Todothis,youwillusethetracedmdumpcommand(nottobeconfusedwiththedmtracedumpSDKtool,whichisatoolusedtocreatethecallstackasatreediagram).ThetracedmdumpcommandisdefinedintheAndroidsourcecode,inbuild/envsetup.sh.Foraccesstothiscommand,youhavetodownloadtheAndroidsourcecodeandcompileAndroid.TodownloadthefullAndroidcode,followtheinstructionson:/source/downloading.html.TocompileAndroid,followtheinstructionson/source/building.html
YoucanalsocompileyourownemulatorfromtheAndroidsourcecodeinsteadofrelyingontheonefromtheSDK.OnceAndroidiscompiled,youshouldhaveallthetoolsyouneedtocreatethetracefileTraceviewneeds.
IntheAVD’stracesdirectory,youcannowsimplyruntracedmdumpmytrace,whichwillcreateatracefileyoucanopenwithTraceview,asshowninFigure6–5.Makesureyourpathissetsothatallthecommandsexecutedbytracedmdumpcansucceed.Iftracedmdumpfailswitha“commandnotfound”errormessage,itislikelyyourpathisnotsetproperly.Forexample,tracedmdumpwillcallpost_trace,whichislocatedintheout/host/linux-x86/bindirectory.
Figure6–5.NativetracingwithTraceviewWhiletheuserinterfacestaysthesame,whatyouactuallyseeinFigure6–5isthelistofnativefunctionsbeingcalled,suchasvsnprintf()andsys_open(),respectively#10and#13.
Twofilesrepresentingthesamedataareactuallycreatedbytracedmdump:dmtrace
dmtrace.html
ThefirstfileistobeusedwithTraceviewwhilethesecondcanbeopenedwithanywebbrowser,includingLynx.
NOTE:Manyusersreportproblemswhenusingtracedmdump,anderrormessagesarenotalwaysveryclear.Ifyouencounteranerror,searchforasolutionontheInternetasitisverylikelysomeonehadthesameproblemandpublishedasolution.Sometimessimplyhavingareal-time,human-readabledescriptionofwhatishappeninginyourapplicationcanhelpyoutremendously.Loggingmessageshavebeenusedforaverylongtimebeforesophisticateddebuggingtoolswereinvented,andmanydeveloperswillheavilyrelyonlogstodebugorprofileapplications日志調(diào)試或者剖析應(yīng)用.6.3Logging日志
Aswehaveseeninmanylistingsalready,youcanusetheLogclasstoprintoutmessagestoLogCat.InadditiontotheJavatraditionalloggingmechanismsuchasSystem.out.println(),Androiddefinessixloglevels,eachhavingitsownmethods:verbose(Log.v)debug(Log.d)info(Log.i)warning(Log.w)error(Log.e)assert(Log.wtf)Forexample,acalltoLog.v(TAG,“mymessage”)isequivalenttoacalltoLog.println(Log.VERBOSE,TAG,“mymessage”).
NOTE:TheLog.wtf()methodswereintroducedinAPIlevel8,butLog.ASSERTexistssinceAPIlevel1.IfyouwanttousetheASSERTloglevelbutwanttoguaranteecompatibilitywitholderAndroiddevices,useLog.println(Log.ASSERT,…)insteadofLog.wtf(…).YoucanthenuseLogCatinEclipse(Window
ShowView
LogCat)and/orinaterminal(adblogcat,orsimplylogcatfromanadbshell)andseethemessagesgeneratedwhileyourapplicationruns.Sincemanymessagesmaybedisplayed,manyofthemnotcomingfromyourapplication,youmaywanttocreatefilterssoyoucanfocusontheoutputthatisrelevanttoyou.Youcanfiltermessagesbasedontheirtags,prioritylevels,andPIDs.InEclipse,youcanusetheCreateFilterfeature,asshowninFigure6–6.Figure6–6.CreatingLogCatfilterwithEclipseEclipsecurrentlydoesnotsupportcreatingafilteronmultipletags,soyouwillhavetouseadblogcatinsteadifyouwanttodo
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度SPF豬飼養(yǎng)產(chǎn)業(yè)鏈大數(shù)據(jù)分析與決策支持合同
- 系統(tǒng)集成課程設(shè)計(jì)日志
- 2025版高端商務(wù)辦公租賃合同范本2篇
- 2025版彩鋼瓦屋頂安裝與建筑節(jié)能改造評(píng)估合同3篇
- 自動(dòng)控制的課程設(shè)計(jì)
- 老馬養(yǎng)殖課程設(shè)計(jì)
- 2024年股權(quán)買賣保證書
- 油壺蓋課程設(shè)計(jì)
- 二零二五年度個(gè)人融資租賃合同示范文本下載3篇
- 美甲教學(xué)課程設(shè)計(jì)方案
- 期末+(試題)+-2024-2025學(xué)年重大版英語(yǔ)五年級(jí)上冊(cè)
- DL∕ Z 860.2-2006 變電站通信網(wǎng)絡(luò)和系統(tǒng) 第2部分:術(shù)語(yǔ)
- 斷親協(xié)議書模板
- 技能成才強(qiáng)國(guó)有我課件模板
- “雙減”背景下小學(xué)數(shù)學(xué)“教、學(xué)、評(píng)”一體化的思考與實(shí)踐
- 中外美術(shù)評(píng)析與欣賞智慧樹知到期末考試答案章節(jié)答案2024年湖南大學(xué)
- 事業(yè)單位考試《綜合知識(shí)和能力測(cè)試》試卷
- 福利住房與購(gòu)房補(bǔ)貼制度
- 康師傅烏龍茗茶營(yíng)銷策劃書
- 【川教版】《生命 生態(tài) 安全》四上第13課《預(yù)防凍瘡》課件
- 工廠籌建方案
評(píng)論
0/150
提交評(píng)論