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 }