JProfiler7.2.3装配和使用
1.目前使用了JProfiler最新版本7.2.3,JMeter使用了2.7的版本
因为项目组要求对某些方法进行性能监控,所以用到了JProfiler和JMeter等测试工具。
2.先说说JMeter,JMeter作为客户端模拟请求:
1)软件都自己下载吧,官网上都有。解压缩包,点击bin目录下的jmeter.bat即可(JavaSwing的界面),在启动界面上面看到‘测试计划’,
在‘测试计划’上面右键-》Threads(user)-》线程组,在‘线程组’右键-》Sampler-》添加java请求,
在‘Java请求’右键-》添加‘聚合报告’,其他的视图一般不需要添加,因为没什么用处,还占系统资源,一般我们比较关注的是聚合报告中的Throughout,也就是传说中的TPS。
2)把你的jar包和依赖都拷贝到lib的ext目录下面,点击‘java请求’节点,右面的类名称选择你的Main方法所在类,这里面有几个地方需要注意:你的Main类需要继承AbstractJavaSamplerClient,当然你的工程中需要引入依赖ApacheJMeter_components.jar
ApacheJMeter_core.jar
ApacheJMeter_ftp.jar
ApacheJMeter_functions.jar
ApacheJMeter_http.jar
ApacheJMeter_java.jar
ApacheJMeter_jdbc.jar
ApacheJMeter_jms.jar
ApacheJMeter_junit.jar
ApacheJMeter_ldap.jar
ApacheJMeter_mail.jar
ApacheJMeter_monitors.jar
ApacheJMeter_native.jar
ApacheJMeter_report.jar
ApacheJMeter_tcp.jar
继承了AbstractJavaSamplerClient后,一般需要实现它的2个方法:
public void setupTest(JavaSamplerContext context) :这个是启动前的准备工作,比如初始化spring容器;
public SampleResult runTest(JavaSamplerContext context) :真正调用的测试方法,写在这里。
下面附上一个简单例子:
package com.jd.saf.thrift;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jd.gp.test.impl.Hello;
public class JMeterThriftTest extends AbstractJavaSamplerClient {
static String testStr = null;
static int num = 500;
static Hello.Iface hello = ThriftClient.instance.getHello();
//private ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:spring-thrift-comsumer.xml");
//private Hello.Iface hello = null;
public void setupTest(JavaSamplerContext context) {
super.setupTest(context);
if(testStr == null) {
StringBuilder sb = new StringBuilder();
for(int k=0;k<num; k++){
sb.append("ABCDEFG***");
}
testStr = "参数= " + sb.toString() + ",参数长度="+ (sb.length() + 24);
}
//hello = (Hello.Iface) appContext.getBean("gpHelloIface");
}
@Override
public SampleResult runTest(JavaSamplerContext context) {
SampleResult result = new SampleResult();
result.setSuccessful(false);
result.sampleStart();
try {
hello.helloString(testStr);//只有这个地方需要修改,写你的具体业务方法
result.setSuccessful(true);
} catch (Exception e) {
} finally {
result.sampleEnd();
}
return result;
}
}
补充,在线程熟悉中,线程数:一般是模拟客户端的数量,Ramp-Up指定线程启动时间
3.然后说说JProfiler7.2.3的安装使用:
1)以普通的JavaApp为例子(Tomcat和JBoss等例子网上也很多):
如果是windows作为服务端,直接点击JProfiler.exe进入图形界面,引导安装即可,比较简单
如果是linux作为服务端,直接执行.sh的文件即可。可能需要授权:chomd +x jpro*.sh,虚拟机在加上
(我的licensekey是到官网临时申请的)
2)windows的启动脚本示例:@echo off
title Prop-Main
java -classpath ..\conf;..\lib\* -agentpath:jprofilerti=port=8849,config=C:\Users\Administrator\.jprofiler7\config.xml -Xbootclasspath/a:C:\PROGRA~1\JPROFI~1\bin\WINDOW~1s\agent.jar com.jd.saf.thrift.ThriftServer
pause
3)linux的启动脚本示例:
nohup java -classpath ../conf/:../lib/* -server -Xms1024m -Xmx1024m -XX:PermSize=512m -XX:MaxNewSize=512m -XX:MaxPermSize=512m -Djava.awt.headless=true -agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler/jprofiler7/bin/agent.jar com.jd.saf.thrift.ThriftServer com.jd.saf.thrift.ThriftServer
7.2.3的版本,写启动脚本需要特别注意,和之前的版本比不需要其他复杂麻烦的操作