---
title: "JProfiler+jemter线上数据压测经验"
author: "sugongp"
date: "2013-07-15T16:28:47Z"
tags: []
categories: ["互联网"]
slug: "jprofiler-jemter"
draft: false
lastmod: "2013-07-18T14:48:11Z"
id: 1906563
url: "https://sugongp.iteye.com/blog/1906563"
---

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&lt;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指定线程启动时间，如果线程启动比较耗时，最好将Ramp-Up的时间设置长一点，防止不必要的错误而设置的过长可能会影响你的TPS数值。

 

3.然后说说JProfiler7.2.3的安装使用：

1）以普通的JavaApp为例子（Tomcat和JBoss等例子网上也很多）：

如果是windows作为服务端，直接点击JProfiler.exe进入图形界面，引导安装即可，比较简单

如果是linux作为服务端，直接执行.sh的文件即可。可能需要授权：chomd +x jpro\*.sh,虚拟机在加上

（我的licensekey是到官网临时申请的）

 2）windows的步骤，最好配一下环境变量 path ： C:\\Program Files\\jprofiler7\\bin\\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的步骤：

首先到/ect下早点profile文件，在最下面追加：

JPROFILER\_HOME=/opt/jprofiler7/bin/linux-x64  
export LD\_LIBRARY\_PATH=$LD\_LIBRARY\_PATH:$JPROFILER\_HOME  
export LD\_LIBRARY\_PATH=$JPROFILER\_HOME

#此处为必须，不加上会提示找不到agent.jar，如果还是提示的话 就断开重连一次

启动脚本示例：

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  com.jd.saf.thrift.ThriftServer 

7.2.3的版本，写启动脚本需要特别注意，和之前的版本比不需要其他复杂麻烦的操作。

然后启动客户端的连接即可了。