Sei sulla pagina 1di 6

LearnDooPHP

Login Home About Contact Forum SUBSCRIBETORSS LearnDooPHP:HighperformancePHPframework 6Dec2009

byLeng NoComments Demos&Snippets,Tutorials database,dbprofiling,logging,performance,profiler,profiling,tool

ProfilingandDBProfilingwithDooPHP
IfyouhavereadtheprevioustutorialonloggingyouwillfindthatprofilingwithDooPHPframeworkisrelatively similartothewayyoulogmessages. Performanceprofilingcanbeusedtomeasurethetime&memoryneededforthespecifiedcodeblocksandfindout whattheperformancebottleneckis.Insteadofcallinglog()youchangeittobeginProfile()andendProfile().Weneed tomarkthebeginningandtheendofeachcodeblockbyinsertingthefollowingmethods:
1 . D o o : : l o g g e r ( ) > b e g i n P r o f i l e ( ' b l o c k _ i d _ h e r e ' ) 2 . / / . . . e v e r y t h i n g h e r e w i l l b e p r o f i l e d 3 . D o o : : l o g g e r ( ) > e n d P r o f i l e ( ' b l o c k _ i d _ h e r e ' )

Codeblocksneedtobenestedproperly.Acodeblockcannotintersectwithanother.Itmustbeeitherataparallellevel orbecompletelyenclosedbytheothercodeblock. Alloftheprofiledresultscanbeorganizedincategory,simplypassinanotherparameterattheendofthemethod beginProfile():


1 . 2 . 3 . 4 . / / d e f a u l t c a t e g o r y i s ' a p p l i c a t i o n ' D o o : : l o g g e r ( ) > b e g i n P r o f i l e ( ' i d ' , ' e d i t p o s t ' ) / / . . . e v e r y t h i n g h e r e w i l l b e p r o f i l e d D o o : : l o g g e r ( ) > e n d P r o f i l e ( ' i d ' )

Toretrievetheprofiledresults,youcalledgetProfileResult().YouhavetopassintheblockIDasparameterandyou willgetanassociativearraywhichshowsyouthetimeandmemoryusedwhenprocessingthecodeblock.
1 . 2 . 3 . 4 . D o o : : l o g g e r ( ) > b e g i n P r o f i l e ( ' b l o c k _ i d ' ) / / . . . e v e r y t h i n g h e r e w i l l b e p r o f i l e d D o o : : l o g g e r ( ) > e n d P r o f i l e ( ' b l o c k _ i d ' ) $ r e s u l t = D o o : : l o g g e r ( ) > g e t P r o f i l e R e s u l t ( ' b l o c k _ i d ' )

Toviewtheprofiledresults,youjusthavetocallshowLogs().BydefaultitwillreturnaneatlyformattedXMLwhich canbefilteredbyIDorcategory.Youcangetaplaintextlogifyouneedso:
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . / / d i s p l a y a l l p r o f i l e d r e s u l t s e c h o D o o : : l o g g e r ( ) > s h o w P r o f i l e s ( ) / / d i s p l a y i n p l a i n t e x t e c h o D o o : : l o g g e r ( ) > s h o w P r o f i l e s ( f a l s e ) / / d i s p l a y o n l y i n c a t e g o r y e d i t p o s t e c h o D o o : : l o g g e r ( ) > s h o w P r o f i l e s ( t r u e , ' e d i t p o s t ' ) / / c a t e g o r y e d i t p o s t ( p l a i n t e x t ) e c h o D o o : : l o g g e r ( ) > s h o w P r o f i l e s ( f a l s e , ' e d i t p o s t ' )

Whenyouhavetowritetheprofileresultsintofile,allyouhavetodoiscallwriteProfiles().SimilartoshowProfiles(), itwritestheXMLstringtofilebydefault(profile.xmlasthedefaultfilename).
1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . / / C r e a t e s a f i l e p r o f i l e . x m l D o o : : l o g g e r ( ) > w r i t e P r o f i l e s ( ) / / Y o u c a n w r i t e a s p l a i n t e x t D o o : : l o g g e r ( ) > w r i t e P r o f i l e s ( ' p r o f i l e . t x t ' , f a l s e ) / / Y o u c a n w r i t e c e r t a i n c a t e g o r y a s w e l l D o o : : l o g g e r ( ) > w r i t e P r o f i l e s ( ' p r o f i l e . x m l ' , t r u e , ' e d i t p o s t ' )

Ifyouareworriedthatthefilesgetbiggerovertime,allyouhavetodoistospecifyafilesizeforfilerotation.When thisfileisreached,thefilesareautomaticallypurgedandnewmessageswillbewritten.
1 . / / m a x i m u m f i l e s i z e i s 2 0 k b 2 . D o o : : l o g g e r ( ) > r o t a t e F i l e ( 2 0 ) 3 . D o o : : l o g g e r ( ) > w r i t e P r o f i l e s ( )

Theprofilerwritesthefiletothewhereyourapplocatedbydefault(SITE_PATH).Ifyouwouldliketochangewhere thelogfileswillbestored,simplyaddtheLOG_PATHsettingincommon.conf.php:
1 . / / t h i s w i l l s t o r e l o g f i l e s a t / v a r / l o g s / , e g . / v a r / l o g s / l o g . x m l 2 . $ c o n f i g [ ' L O G _ P A T H ' ] = ' / v a r / l o g s / '

Now,forDatabaseprofiling,itsthesameasprofilingbutyouuseanothermethodinstead(beginDbProfile(id)). DBprofilingallowsinspectionofthequeriesthathavebeenperformedwithoutneedingtoaddextradebuggingcode toclasses.Tohaveaquicktestonthis,youcandownloadtheblogdemointheloggingtutorial.Simplyopenupthe bootstrapindex.phpanduncommenttheselines:


1 . 2 . 3 . 4 . 5 . 6 . # U n c o m m e n t f o r D B p r o f i l i n g D o o : : l o g g e r ( ) > b e g i n D b P r o f i l e ( ' t e s t ' ) D o o : : a p p ( ) > r u n ( ) D o o : : l o g g e r ( ) > e n d D b P r o f i l e ( ' t e s t ' ) D o o : : l o g g e r ( ) > r o t a t e F i l e ( 2 0 ) D o o : : l o g g e r ( ) > w r i t e D b P r o f i l e s ( )

ThiswilllogallthequeriesexecutedintheblogapplicationsincethecodebetweenbeginDbProfileandendDbProfile istheapplicationcode.Note,theDBprofilesareloggedinafilenameddb.profile.xmlbydefault,youcanchangethis behaviorjustlikewriteProfiles().

Thatsnotall!Afteryouhaveyourprofilesstored,DooPHPhasahandylittletoolforyoutomonitortheresults.Ifyou havegonethroughthedefaultapplicationApp,theresaroutecalldebugwhichlookslikethis:
1 . $ r o u t e [ ' * ' ] [ ' / d e b u g / : f i l e n a m e ' ] = a r r a y ( ' M a i n C o n t r o l l e r ' , ' d e b u g ' )

Letsviewtheresultsprofile.xmlanddb.profile.xmlwiththelogviewertool.Allyouneedtodoisvisit http://localhost/amfdemo/blog/tools/logviewer.html.Justenterthedebugroute+thefilenameasaddress: http://localhost/amfdemo/blog/debug/profile http://localhost/amfdemo/blog/debug/db.profile Therearebarsonthedurationandmemoryfields,processmarkedwithdifferentcolorsindicatesrelativedifference fromaverageprocesses.

FilterbyID(db.profile)orcategory+ID(profile):

FilterbyURI:

Search...

Random

Share

JoinChat

Ifyouwishtodiscusfurther,theresathreadonthistutorialintheforumhere.

PreviousPost

NextPost
Search...

OurSponsors ChooseLanguage
English Russian Deutsch Espaol Arabic Portugese

PopularTags

FindTutorial
Easy Normal Difficult

Categories
Demos&Snippets(4) News&Updates(2) Screencast(1) Tutorials(17)

RecentComments
kukatonIntrotoDooPHPslides(PHPMalaysiameetup2011) MilosKovackionUsingDooTranslatorfortranslation WebdevGreeceonUsingDooTranslatorfortranslation MilosKovackionHandlingsessionswithoutApachesessions TonCreateasimpleToDoListinDooPHPPart2

Blogroll
DevelopmentBlog Documentation Forum GoogleCode Home About Contact Forum AllcontentscopyrightLearnDooPHP.Allrightsreserved.ThemedesignbyWebKreation.

Potrebbero piacerti anche