使用JMeter的Java请求功能测试Hetty性能
- - BlogJava-qileilove JMeter是Apache组织的开放源代码项目,它是功能和. 性能测试的工具,100%的用. JMeter可以用于测试静态或者动态资源的性能(文件、Servlets、Perl脚本、java对象、. 数据库和查询、ftp服务器或者其他的资源). JMeter用于模拟在服务器、网络或者其他对象上附加高负载以测试他们提供服务的受压能力,或者分析他们提供的服务在不同负载条件下的总性能情况.
public class RpcHessianClient { public static void main(String[] args) { String url = "http://localhost:8081/apis/hello"; HessianProxyFactory factory = new HessianProxyFactory(); ExecutorService es = Executors.newFixedThreadPool(10); int size = 1000000; final CountDownLatch cdl = new CountDownLatch(size); try { long start = System.currentTimeMillis(); factory.setUser("client1"); factory.setPassword("client1"); factory.setOverloadEnabled(true); final Hello basic = (Hello) factory.create(Hello.class, url); for (int i = 0; i < size; i++) { es.submit(new Runnable() { @Override public void run() { String u=basic.hello("guolei"); //System.out.println(u); cdl.countDown(); } }); } cdl.await(); long time = System.currentTimeMillis() - start; System.out.println("SayHello:"); System.out.println("耗时:" + (double) time / 1000 + " s"); System.out.println("平均:" + ((double) time) / size +" ms"); System.out.println("TPS:" + (double) size / ((double) time / 1000)); // System.out.println("Hello, " + s.getMail()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }finally{ es.shutdown(); } } |
public class HettyTest extends AbstractJavaSamplerClient { private static String label = "hettyTest"; /** * 执行runTest()方法前会调用此方法,可放一些初始化代码 */ public void setupTest(JavaSamplerContext arg0) { } /** * JMeter测试用例入口 */ public SampleResult runTest(JavaSamplerContext arg0) { SampleResult sr = new SampleResult(); sr.setSampleLabel(label); try { // 这里调用我们要测试的java类,这里我调用的是一个Test类 Map<String,String> map = getDefaultParameters().getArgumentsAsMap(); sr.sampleStart(); // 记录程序执行时间,以及执行结果 Test.execute(map.get("ip"),map.get("port")); sr.sampleEnd(); sr.setSuccessful(true); } catch (Throwable e) { sr.setSamplerData(e.getMessage()); e.printStackTrace(); sr.setSuccessful(false); // 用于设置运行结果的成功或失败,如果是"false"则表示结果失败,否则则表示成功 } return sr; } /** * JMeter界面中可手工输入参数,代码里面通过此方法获取 */ public Arguments getDefaultParameters() { Arguments args = new Arguments(); args.addArgument("ip", "localhost"); args.addArgument("port", "8081"); return args; } /** * 执行runTest()方法后会调用此方法. */ public void teardownTest(JavaSamplerContext arg0) { } } |
public class Test { public static void execute(String ip,String port) throws MalformedURLException { String url = "http://"+ip+":"+port+"/apis/hello"; HessianProxyFactory factory = new HessianProxyFactory(); factory.setUser("client1"); factory.setPassword("client1"); factory.setOverloadEnabled(true); final Hello basic = (Hello) factory.create(Hello.class, url); //System.out.println("SayHello:" + basic.hello("guolei")); } } |