接口性能测试--JMeter
1.JMeter Java Sampler介绍setupTest做些初始化的工作,每个线程只执行一次teardownTest做些清理工作,每个线程只执行一次runTest具体的测试执行工作,每个并发每次循环都将执行一次SampleResult记录测试结果,result.sampleStart()一个事务开始,result.sampleEnd()一个事务结束main方法用于调试01packagecom . xxx . yyy . perf;0203importorg.apache.jmeter.config.Argument;04importorg.apache.jmeter.config.Arguments;05importorg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;06importorg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;07importorg.apache.jmeter.samplers.SampleResult;08importorg.springframework.context.ApplicationContext;09importorg.springframework.context.support.ClassPathXmlApplicationContext;1011importcom.xxx.udb.client.PersonService;12importcom.xxx.udb.client.result.PersonResult;1314/**15*16* @Author flynewton17*/18publicclassGetPersonByLongIdextendsAbstractJavaSamplerClient{19 privatestaticfinalApplicationContextctx=new ClassPathXmlApplicationContext(20 "spring-udb.xml");21 privatestaticPersonServicepersonService=null;22 privatestaticfinalStringloginId="00000sb";23 privatestaticfinalStringsiteId="CN";242526 @Override27 publicvoidsetupTest( JavaSamplerContextcontext){28 super . setupTest( context);29 personService= ( PersonService)ctx . getBean( "personServiceClient");30 }3132 @Override33 publicvoidteardownTest( JavaSamplerContextcontext){34 super . teardownTest( context);35 }3637 publicSampleResultrunTest( JavaSamplerContextarg0){38 SampleResultresult=newSampleResult();39 PersonResultpersonResult=null;4041 result . setSampleLabel( "result");4243 try{44 result . sampleStart();45 personResult=personService . getPersonByLongId( siteId ,loginId);46 result . sampleEnd();47 }catch ( Throwablet){48 this . getLogger (). error( "Exception:"+t);49 returnnull;50 }5152 if ( personResult!=null&&personResult . getCode()==0){53 result . setSamplerData( personResult . toString());54 result . setSuccessful( true);55 }else{56 result . setSuccessful( false);57 }5859 returnresult;60 }6162 staticvoidprintResult( SampleResultres){63 System . out . println( "test is success:"+res . isSuccessful()+" used:"64 + ( res . getEndTime()-res . getStartTime())+"ms "+" result:"65 +res . getSampleLabel()+":"+res . getSamplerData());66 }6768 publicstaticvoidmain( String []args){69 GetPersonByLongIdservice=newGetPersonByLongId();70 JavaSamplerContextcontext=newJavaSamplerContext( null);71 service . setupTest( context);72 SampleResultres1=service . runTest( context);73 printResult( res1);74 service . teardownTest( context);75 System . exit( 0);76 }7778}
2.多接口性能测试按照上面的方法进行测试,每个需要测试的接口和场景都需要写一个这样的测试类,对于有些具有相似初始化,清理等工作,只有具体的那行事务代码不一样的情况,会有很多重复的工作。如下:可以考虑采用反射的方式来解决这个问题,尤其是业务非常类似的接口和场景。当然,反射会带来压力机的性能消耗,但是这个可以通过调整JMeter的JVM参数和增加JMeter实例来解决。1)利用反射后的结构如下:2)AbstractServiceClient是一个抽象基类
packagecom . xxx . yyy . perf . base;importjava.lang.reflect.Method;importorg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;importorg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;importorg.apache.jmeter.samplers.SampleResult;/*** Comment of AbstractServiceClient*@author flynewton*/publicabstractclassAbstractServiceClientextendsAbstractJavaSamplerClient {publicObjectinvokeTest( StringtestName ,JavaSamplerContextcontext ,SampleResultsample){Method []methods=this . getClass (). getMethods();for ( Methodm:methods){if ( m . getName (). equalsIgnoreCase( testName)){try{returnm . invoke( this ,context , sample);}catch ( Throwablet){this . getLogger (). error( "execute method:"+testName+" falied" ,t);}}}returnnull;}}
3)GetPersonTest为某一类具有相似业务场景的测试类其中 getDefaultParameters方法设置默认参数:test传入具体要执行的测试方法;info传入日志级别,利用main方法进行调试时设置为true,真正进行性能测试的时候设置为false
packagecom . xxx . yyy . perf;importorg.apache.jmeter.config.Argument;importorg.apache.jmeter.config.Arguments;importorg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;importorg.apache.jmeter.samplers.SampleResult;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.xxx.udb.client.PersonService;importcom.xxx.udb.client.result.MultiPersonResult;importcom.xxx.udb.client.result.PersonResult;importcom.xxx.udb.perf.base.AbstractServiceClient;/*** Test get person* @author flynewton*/publicclassGetPersonTestextendsAbstractServiceClient{privatestaticfinalApplicationContextctx=new ClassPathXmlApplicationContext("spring-udb.xml");privatestaticPersonServicepersonService=null;privatestaticStringloginIdCache="00000sb";privatestaticStringloginIdWithoutCache="01haytham";privatestaticStringsiteIdCache="CN";privatestaticStringsiteIdWithoutCache="IN";privatestaticStringalimailCn="gchg2008@zzgcchen.cn";privatestaticStringalimailUdb="000001@lvcuinm.com.cn";privatestaticStringuidCache="300113578";privatestaticStringuidWithoutCache="in5002997";privatestaticStringemailCache="00000sb@alibaba-test.com";privatestaticStringemailWithoutCache="5002997@alibaba-inc.com";@OverridepublicvoidsetupTest( JavaSamplerContextcontext){super . setupTest( context);personService= ( PersonService)ctx . getBean( "personServiceClient");}publicArgumentsgetDefaultParameters(){Argumentsparams=newArguments();params . addArgument( "test" ,"testMethod");params . addArgument( "info" ,"false");returnparams;}@OverridepublicvoidteardownTest( JavaSamplerContextcontext){super . teardownTest( context);}publicObjecttestGetPersonByLongIdViaCache( JavaSamplerContextarg0 ,SampleResultsample){PersonResultpersonResult=null;try{sample . sampleStart();personResult=personService . getPersonByLongId( siteIdCache , loginIdCache);sample . sampleEnd();}catch ( Throwablet){this . getLogger (). error( "Exception:"+t);returnnull;}returnpersonResult;}publicObjecttestGetPersonByLongIdWithoutCache( JavaSamplerContextarg0 ,SampleResultsample){PersonResultpersonResult=null;try{sample . sampleStart();personResult=personService . getPersonByLongId( siteIdWithoutCache , loginIdWithoutCache);sample . sampleEnd();}catch ( Throwablet){this . getLogger (). error( "Exception:"+t);returnnull;}returnpersonResult;}publicObjecttestGetPersonByAlimailCN( JavaSamplerContextarg0 ,SampleResultsample){PersonResultpersonResult=null;try{sample . sampleStart();
页:
[1]