接口性能测试--JMeter

发表于:2015-04-20来源:uml.org.cn作者:不详点击数: 标签:性能测试
1.JMeter Java Sampler介绍 setupTest做些初始化的工作,每个线程只执行一次 teardownTest做些清理工作,每个线程只执行一次

  1.JMeter Java Sampler介绍

  setupTest做些初始化的工作,每个线程只执行一次

  teardownTest做些清理工作,每个线程只执行一次

  runTest具体的测试执行工作,每个并发每次循环都将执行一次

  SampleResult记录测试结果,result.sampleStart()一个事务开始,result.sampleEnd()一个事务结束

  main方法用于调试

01  package  com . xxx . yyy . perf;
02
03  import  org.apache.jmeter.config.Argument;
04  import  org.apache.jmeter.config.Arguments;
05  import  org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
06  import  org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
07  import  org.apache.jmeter.samplers.SampleResult;
08  import  org.springframework.context.ApplicationContext;
09  import  org.springframework.context.support.ClassPathXmlApplicationContext;
10
11  import  com.xxx.udb.client.PersonService;
12  import  com.xxx.udb.client.result.PersonResult;
13
14  /**
15  *
16  * @author flynewton
17  */
18  public  class  GetPersonByLongId  extends  AbstractJavaSamplerClient  {
19      private  static  final  ApplicationContext  ctx  =  new ClassPathXmlApplicationContext(
20              "spring-udb.xml");
21      private  static  PersonService  personService  =  null;
22      private  static  final  String  loginId  =  "00000sb";
23      private  static  final  String  siteId  =  "CN";
24
25
26      @Override
27      public  void  setupTest( JavaSamplerContext  context)  {
28          super . setupTest( context);
29          personService  = ( PersonService)  ctx . getBean( "personServiceClient");
30      }
31
32      @Override
33      public  void  teardownTest( JavaSamplerContext  context)  {
34          super . teardownTest( context);
35      }
36
37      public  SampleResult  runTest( JavaSamplerContext  arg0)  {
38          SampleResult  result  =  new  SampleResult();
39          PersonResult  personResult  =  null;
40
41          result . setSampleLabel( "result");
42
43          try  {
44              result . sampleStart();
45              personResult  =  personService . getPersonByLongId( siteId ,  loginId);
46              result . sampleEnd();
47          }  catch ( Throwable  t)  {
48              this . getLogger (). error( "Exception:"  +  t);
49              return  null;
50          }
51
52          if ( personResult  !=  null  &&  personResult . getCode()  ==  0)  {
53              result . setSamplerData( personResult . toString());
54              result . setSuccessful( true);
55          }  else  {
56              result . setSuccessful( false);
57          }
58
59          return  result;
60      }
61
62      static  void  printResult( SampleResult  res)  {
63          System . out . println( "test is success:"  +  res . isSuccessful()  +  " used:"
64                  + ( res . getEndTime()  -  res . getStartTime())  +  "ms "  +  " result:"
65                  +  res . getSampleLabel()  +  ":"  +  res . getSamplerData());
66      }
67
68      public  static  void  main( String []  args)  {
69          GetPersonByLongId  service  =  new  GetPersonByLongId();
70          JavaSamplerContext  context  =  new  JavaSamplerContext( null);
71          service . setupTest( context);
72          SampleResult  res1  =  service . runTest( context);
73          printResult( res1);
74          service . teardownTest( context);
75          System . exit( 0);
76      }
77
78  }

  2.多接口性能测试

  按照上面的方法进行测试,每个需要测试的接口和场景都需要写一个这样的测试类,对于有些具有相似初始化,清理等工作,只有具体的那行事务代码不一样的情况,会有很多重复的工作。如下:可以考虑采用反射的方式来解决这个问题,尤其是业务非常类似的接口和场景。当然,反射会带来压力机的性能消耗,但是这个可以通过调整JMeter的JVM参数和增加JMeter实例来解决。

  1)利用反射后的结构如下:

  2)AbstractServiceClient是一个抽象基类

package  com . xxx . yyy . perf . base;
import  java.lang.reflect.Method;
import  org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import  org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import  org.apache.jmeter.samplers.SampleResult;
/**
* Comment of AbstractServiceClient
*  @author flynewton
*/
public  abstract  class  AbstractServiceClient  extends  AbstractJavaSamplerClient {
public  Object  invokeTest( String  testName ,  JavaSamplerContext  context ,SampleResult  sample)  {
Method []  methods  =  this . getClass (). getMethods();
for ( Method  m  :  methods)  {
if ( m . getName (). equalsIgnoreCase( testName))  {
try  {
return  m . invoke( this ,  context , sample);
}  catch ( Throwable  t)  {
this . getLogger (). error( "execute method:"  +  testName  +  " falied" ,  t);
}
}
}
return  null;
}
}

原文转自:http://www.uml.org.cn/Test/201401163.asp

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
  • 回忆未来
    2016-07-14 20:55:31发表

    Jmeter性能测试实战视频教程 课程观看地址:http://www.xuetuwuyou.com/course/39 课程出自学途无忧网:http://www.xuetuwuyou.com 本课程从实战角度,由浅入深讲解Jmeter性