Tải bản đầy đủ - 0 (trang)
Chapter 18. Performance and Stress Testing Your Applications

Chapter 18. Performance and Stress Testing Your Applications

Tải bản đầy đủ - 0trang

WhyStressTestYourApplications?

It'snotuncommontobuildacomponentanddiscoverwhenyou

begintestingitthatitsperformanceisnotwhatyouexpected

orwerehopingitwouldbe.Eventually,thishappenstoallofus.

Truthfully,thefactthatthishappensisnotabadthinginandof

itself.Youshouldnotbecompletelydepressedaboutpoor

performancethefirsttimeyoutestyourcomponentsbecause,

duringthedesignandimplementationphase,performance

shouldnotbeyourmainfocus.Thisisnottosaythatyou

shouldbetryingtodesignorbuildslowcomponents,butyou

shouldbefocusingonbuildingyourcomponentsand

applicationsbasedonsoundobject-orientedprinciples.Taking

shortcutsbecauseyouareworryingaboutperformanceearlyin

thecycleis,moreoftenthannot,thewrongapproach.

Focusingtoomuchonperformanceearlyinthedesignor

developmentstageisnotthebestapproachmainlybecauseit's

hardtodeterminewherethebottlenecksorproblemswillbe

withoutactuallytestingthecomponentorapplication.During

designandconstruction,youmightbelieveacertainpieceor

componentoftheapplicationwillbeaproblemandfindoutthat

acompletelydifferentareaactuallyistheproblemwhenthe

applicationisbuilt.

Oneofthebiggestmistakesdevelopersmakewhendoingany

typeofperformanceorloadtestingisnotdoingenoughtesting

beforestartingtooptimizethecode.Forexample,supposeyou

havetwoareasinyourapplicationthatareslow.Unlessyoudo

enoughtestingtoknowwhichoneisworse,youmightworkon

theareathatiscausingtheleastproblem.Youmustdoenough

testingtoknowwhichareatofocusonfirsttogetthebiggest

returnonyourtimeinvestment.

Let'sfaceit;asdevelopers,wearealwaysracingagainstthe

clock.Usually,thisclockisbeingsetandmanagedbysomeone

elsetogetthereleasetoQAorouttoacustomer.There's



hardlyeverenoughtimetodoperformanceorstresstestingof

theentireapplication,soyoumustbewiseenoughtofocus

youravailabletestingtimeintheproperplaces.Thefirstruleis

todoenoughup-fronttestingtoidentifytheproblemareasthat

arecausingthebiggestreductionsinperformance.Ifyouhave

enoughtime,youobviouslywanttogothroughalltheproblem

areas,butifyoudofindyourselfrunningoutoftime,atleast

youcantakecomfortinthefactthatyoufixedthebigones.

Averyimportantdistinctionmustbemadebetween

performancetestingandstressorloadtesting.Performance

testinginvolvesexecutingthefunctionalrequirementsforan

applicationandbasicallytiminghowlongittakesforeachresult

tocomplete.Thisactionshouldbeexecutedforallthepublic

interfacestogetacompletepictureofhowwelltheapplication

performs.So,fortheAuctionexample,wemighttestgetting

thelistofauctionsthatareavailable,viewingthedetailsofone

oftheauctions,submittingabid,andthentesttherestofthe

EJBmethodsthatareexposedtotheremoteclients.Wewould

recordthetimethatittakesforeachactiontocompleteendto

end.Thissometimesisreferredtoasatransactiontime.

Basedonthetimethatittakestocompletetheseactions,you

wouldbeabletocomeupwitharoughthroughputtime.

Throughputinthissensedefinesthenumberoftransactions

thatcanoccurinasetamountoftime.Thesekindsofnumbers

alsocanhelpnetworkengineersunderstandhowmuchnetwork

bandwidththeapplicationmightrequire.

Stresstestingorloadtestingisalittledifferentfrom

performancetestingbutissomewhatrelated.Withperformance

testing,thenumberofusersusedwhentestingperformance

typicallyisone.Withloadtesting,thetestssimulatealarger

numberofusersandevaluatehowtheapplicationreactsasthe

numberofusersgrows.Thingssuchastransactiontime,

throughput,andmemoryusedcanberecordedtogetapicture

ofhowthesystemwillhandleitselfunderaheavierload.



Note

Althoughtherecanbesomesubtledifferences

betweenstresstestingandloadtesting,weare

usingthetermsinterchangeablythroughoutthis

chapter.



YoumustdobothtypesoftestinginyourEJBapplications.Both

testscangiveyouimportantinformationabouthowwellthe

applicationisdesignedandconstructedandcanpointtoparts

oftheapplicationthatmightbecomeabottleneck,under

normalloadsorespeciallyasthenumberofusersincreases.



PerformanceTestingYourBeans

Asalreadyindicated,there'sadifferencebetweenstresstesting

andperformancetesting;however,theyusuallyareconnected

becauseoneusuallycanhaveanimpactontheother.

Performancetestingdealswithareasofanapplicationthatare

sluggish,evenwithasingleuser.Bettercodingmethodsor

algorithmsusuallyfixtheproblemsyoufindwhenyoudo

performancetesting.Forexample,youmightuseanObject[]

andaforlooptoiteratethroughafinitecollection,ratherthan

anIteratororEnumeration.

Afteryou'veidentifiedtheperformancebottlenecks,manual

codereviewsusuallyarenecessarytospotthecausesofthe

problems.Insomecases,youmightfindthatyoucanchange

thewaythecodeiswrittenandgetbetterperformance.In

othercases,itjustmightbeacomplicatedalgorithmand

there'snothingyoucandoaboutit.Theseperformance

problemscanusuallybefoundeasierbyusingperformancetestingtools.Sometimes,thesetoolscaneasilyindicatethreads

thatarewaitingforotherthreadstocomplete,orwherealarge

numberofobjectsarebeingcreatedneedlessly.Table18.1

providesasmalllistofcommerciallyavailabletoolsthatcan

helpyouconductperformancetesting.

Table18.1.SeveralPerformance-TestingTools

Company



Product



URL



Rational



Quantify



www.rational.com



Sitraka



JProbe



www.jprobe.com



IntuitiveSystems,Inc.



OptimizeIt



www.optimizeit.com



ApacheGroup



JMeter



jakarta.apache.org



Althoughthesetoolscanreducetheamountoftimeittakesto

identifytheperformanceproblems,theyareratherexpensive,

andsomeofthemtakesometimetolearntouse.Ifyouare

limitedbytimeandabudget,youcanusesomesimpleteststo

getanideaofwhichcomponentsoroperationsarefineand

whichonesareslow.Oneoftheeasiestwaysofdoingthisisto

buildaJavaclientthatexerciseseachremotemethodinyour

EJB.Beforeyoucallthemethod,recordthetimeandthen

recorditagainafterthemethodisfinished.Ifyoutakethe

differenceofthestartandendtimes,youcangetanideaof

howlongeachmethodtakestoperform.Listing18.1illustrates

thisbasicapproach.

Listing18.1SimpleClientTesttoGetBeanPerformance

Times



importjava.util.Collection;

importjavax.naming.Context;

importjavax.naming.InitialContext;

importjavax.naming.NamingException;

importjavax.rmi.PortableRemoteObject;

importcom.que.ejb20.auction.controller.AuctionHouse;

importcom.que.ejb20.auction.controller.AuctionHouseHom

publicclassAuctionHouseTest{

//Constructor

publicAuctionHouseTest(){

super();

}



//ReturnsaremoteinterfacetotheAuctionHouseBean

privateAuctionHousegetAuctionHouse(){



ContextinitCtx=null;

AuctionHouseauctionHouse=null;

try{

//Createtheinitialcontext

initCtx=newInitialContext();

Objectobj=

initCtx.lookup("AuctionHouse");



//Narrowandcasttheobject

AuctionHouseHomeauctionHouseHome=

(AuctionHouseHome)PortableRemoteObject.narrow(

Auct

//Createaremotereferenceandstoreitintoth

auctionHouse=auctionHouseHome.create();

}catch(Exceptionex){

ex.printStackTrace();

}finally{

try{

if(initCtx!=null){

initCtx.close();

}

}catch(Exceptionex){

ex.printStackTrace();

}

}

//Returntheremotereference

returnauctionHouse;

}

/**

*Testtheenterprisebean'smethods

*/



privatevoidrunTest(AuctionHousehouse){

try{

//recordthestarttime

longbeginTime=System.currentTimeMillis();



//Invokethemethod

Collectionauctions=house.getAllAuctions(1,10

//Recordtheendtime

longendTime=System.currentTimeMillis();



//Printoutthetimethatthemethodtook

System.out.println("Time:"+(endTime-beginTi

}catch(Exceptionex){

System.out.println("Problemrunningthetest!")

}finally{

//Cleanuptheremotereference

try{

if(house!=null)

house.remove();

}catch(Exceptionex){

ex.printStackTrace();

}

}

}

//Thestartupmethod

publicstaticvoidmain(String[]args){

AuctionHouseTesttest=newAuctionHouseTest();

AuctionHousehouse=test.getAuctionHouse();

if(house!=null){

test.runTest(house);

}else{



//Iftherewasaproblem,justexit

System.out.println("ErrorcreatingAuctionHouse

}

}

}

Thisisaverysimpleapproach,butthat'sthepoint.Youcanget

someveryhelpfulresultsjustbytestinghowlongeachbean

methodtakestoexecute.Asyoumakechangestothebeanor

theenvironmentconfiguration,youcanexecutethesetests

iterativelytoseehowthechangesaffectexecutiontimes.The

exampleinListing18.1onlyteststhegetAllAuctions

methodandideally,youwouldwanttotesteachmethod

availableintheremoteinterfaceoftheEJB.

Tip

It'sagoodideatoexecutethetestsmanytimesand

takeanaverageoftheexecutiontimes,primarily

becauseoftimeraccuracy.Althoughthemethod

currentTimeMillsmightreturnmillisecond

resolution,thePCclocktheusercanaccessisnot

thataccurate.Byrunningmanyloopiterations,you

caneliminatesomeoftheaccuracyproblems.

Resultsalsocanvarydependingonwhichdataset

youareexecutingagainstandeventhenetwork

trafficatthetimeofthetests.Thebestapproachis

touseadatasamplethatistypicalofwhatthe

actualenduserswilluse.Besurethedatainthe

databaseisanaccuratesampleofwhatitwillbein

theproductionenvironment,oryou'lllikelymiss

someproblems.



StressTestingYourBeans

Theideaofperformingstressorloadtestsistobeableto

determinehowmanyusersthesystemcansupportwithout

degradingandalsohowthesystemwilldegradeasthenumber

ofusersincrease.Withloadtesting,youkeeprunningtests,

modifyingyourconfiguration,andthenre-runningthetests.

Youwillstarttoseehowdifferentconfigurationshelporhinder

yourapplication'scapabilitytohandleincreaseduserloads.All

applicationshaveabreakingpointwheretoomanyuserscan

bringthesystemtoitsknees.Thepointofconductingloadtests

istodeterminewhatthatnumberis.

Similartoperformancetools,thereareseveralgoodloadtestingtoolsonthemarket.Table18.2listsafewofthemost

popular.

Table18.2.SeveralLoad-TestingTools

Company



Product



URL



SegueSoftwareInc.



SilkPerformer



www.segue.com



MercuryInteractive



LoadRunner



www.mercuryinteractive.com



Rational



SiteLoad



www.rational.com



RadView



WebLoad



www.radview.com



RSWSoftware



ETestSuite



www.rswsoftware.com



Caution

Mostoftheload-testingtoolsinTable18.2require

WindowsNT,Windows2000,orUnixsystems.For

mostdevelopmentshops,thisisnotaproblem.



However,ifyouplantoevaluatetheseathome,this

issomethingtokeepinmind.



Becauseloadtestingisattemptingtosimulatemultipleusers,

it'salittlemorecomplicatedthanjusttryingtosimulatea

singleuser.ThisiswhyusingoneofthetoolslistedinTable

18.2isprobablygoingtogiveyouamoreaccurateideaofhow

yoursystemwillperformunderheavyloadsthanyoucanget

onyourown.MostofthemaredesignedtotestfromaWeb

site,althoughafewofthemcantestenterprisebeansdirectly.

Again,ifyouarepressedfortimeandwouldliketogetanidea

oftheresults,youcanbuildasimpleJavaprogramthat

simulatesmultipleusers.

Note

Inareal-worldscenario,applicationuserswouldbe

usingdifferentmachinestoaccesstheapplication.

Thissimpleapproachshouldbenosubstitutefor

usingaqualityprogramtosimulaterealloads.The

commercialproductsofferagreatdealofflexibility

andconfigurationoptionsandusuallycanrecord

scriptstakenfromrealuseofyourapplicationand

beplayedback.Thissimpleprogramshouldbeused

merelytogiveyouaroughimpressionofthe

application'scapabilityuntilyoucanuseamuch

morecomplexsetoftools.



Listing18.2showsaverybasicexampleofhowtousethreads

tosimulatemorethanoneclientaccessinganEJB.

Listing18.2SimpleExamplethatUsesThreadsto



SimulateMultipleClients



importjava.util.Collection;

importjavax.naming.Context;

importjavax.naming.InitialContext;

importjavax.naming.NamingException;

importjavax.rmi.PortableRemoteObject;

importcom.que.ejb20.auction.controller.AuctionHouse;

importcom.que.ejb20.auction.controller.AuctionHouseHom



publicclassAuctionHouseMultipleTestimplementsRunnab

//Thenumberofclientstosimulate

publicstaticfinalintNUMBER_OF_CLIENTS=5;

//Constructor

publicAuctionHouseMultipleTest(){

super();

}



//ReturnsaremoteinterfacetotheAuctionHouseBean

privateAuctionHousegetAuctionHouse(){

ContextinitCtx=null;

AuctionHousehouse=null;

try{

//Createtheinitialcontext

initCtx=newInitialContext();

Objectobj=

initCtx.lookup("AuctionHouse");



//Narrowandcasttheobject

AuctionHouseHomeauctionHouseHome=

(AuctionHouseHome)PortableRemoteObject.narrow(



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 18. Performance and Stress Testing Your Applications

Tải bản đầy đủ ngay(0 tr)

×