在使用jmeter進行性能測試的時候,有可能會須要經過一些腳本去測試性能,(好比經過sokeeper的api去測試sokeeper的讀寫性能)。這時,咱們能夠經過Java Request實現,如下是操做步驟。java
首先建立一個maven project。apache
因Jmeter的Java Request腳本依賴Jmeter的一些核心jar包,因此咱們要maven項目的pom文件中添加Jmeter的一些依賴包:ApacheJMeter_core、ApacheJMeter_java:api
編寫Java Sample,繼承AbstractJavaSample,並重寫如下方法:session
public Arguments getDefaultParameters();可選,定義可用參數及默認值; · public void setupTest(JavaSamplerContext arg0):可選,測試前執行,作一些初始化工做; · public SampleResult runTest(JavaSamplerContext arg0);必選,實現自定義請求; · public void teardownTest(JavaSamplerContext arg0):可選,測試結束時調用;
示例代碼:maven
import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.utils.EnsurePath; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; public class CuratorRequestSample extends AbstractJavaSamplerClient { protected CuratorFramework client = null; protected long subCount = 1l; protected long TOTAL_SUB_COUNT = 100000l; /** * 定義可用參數及默認值 */ public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("connect_str", "192.168.103.136:2181,192.168.103.154:2181"); // 設置參數,並賦予默認值0 return params; } /** * 可選,測試前執行,作一些初始化工做,在Jmeter執行的時候,一個Thread只執行一次setupTest方法 */ public void setupTest(JavaSamplerContext context) { String connect_str = context.getParameter("connect_str"); RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 4); client = CuratorFrameworkFactory.builder().connectString(connect_str) .connectionTimeoutMs(3000).namespace("curator_sample") .sessionTimeoutMs(5000).retryPolicy(retryPolicy).build(); client.start(); EnsurePath ensure = new EnsurePath("/curator_sample/setData"); try { ensure.ensure(client.getZookeeperClient()); } catch (Exception e) { e.printStackTrace(); } } /** * 可選,測試結束時調用,用於資源釋放,一個Thread只執行一次 */ public void teardownTest(JavaSamplerContext context) { client.close(); } /** * 必選,實現自定義請求 */ public SampleResult runTest(JavaSamplerContext arg0) { SampleResult resuts = new SampleResult(); resuts.sampleStart(); try { byte[] bytes = client.getData().forPath("/setData"); resuts.setSuccessful(true); } catch (Exception e) { resuts.setSuccessful(false); } resuts.sampleEnd(); return resuts; } }
而後,把項目打成jar包,並將包放到${jmeter_home}/lib/ext/目錄下。如還須要依賴第三方jar,須要將第三方的jar也放到${jmeter_home}/lib/ext/目錄下。性能
啓動Jmeter。測試
建立線程組:優化
建立Java Request Sample:ui
此時,會在Java請求頁面的「類名稱」下拉列表中看到咱們以前寫的Java Request腳本:spa
選中其中一個要測試的腳本,能夠看到配置的參數:
而後再添加Summary Report便可,保存測試計劃。設置線程組屬性,執行測試計劃。便可。
優化技巧一
不少狀況下咱們在編寫Java Sample時,須要編寫多個Java Sample,且這些Java Sample之間,擁有共同的資源初始化操做、資源釋放操做,只是在核心業務不一樣而已,這時,咱們能夠封裝一個基類。例:
import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.utils.EnsurePath; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; /** * Curator的Java請求測試基類 * * @author majian * @version 1.0, 2016-2-22 * @since 1.0, 2016-2-22 */ public abstract class CuratorBaseRequest extends AbstractJavaSamplerClient { protected CuratorFramework client = null; protected long subCount = 1l; protected long TOTAL_SUB_COUNT = 100000l; /** * 定義可用參數及默認值 */ public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("connect_str", "192.168.103.136:2181,192.168.103.154:2181");// 設置參數,並賦予默認值0 return params; } /** * 可選,測試前執行,作一些初始化工做,在Jmeter執行的時候,一個Thread只執行一次setupTest方法 */ public void setupTest(JavaSamplerContext context) { String connect_str = context.getParameter("connect_str"); RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 4); client = CuratorFrameworkFactory.builder().connectString(connect_str) .connectionTimeoutMs(3000).namespace("curator_sample") .sessionTimeoutMs(5000).retryPolicy(retryPolicy).build(); client.start(); EnsurePath ensure = new EnsurePath("/curator_sample/setData"); try { ensure.ensure(client.getZookeeperClient()); } catch (Exception e) { e.printStackTrace(); } } /** * 可選,測試結束時調用,用於資源釋放,一個Thread只執行一次 */ public void teardownTest(JavaSamplerContext context) { client.close(); } /** * 必選,實現自定義請求 */ public SampleResult runTest(JavaSamplerContext arg0) { SampleResult resuts = new SampleResult(); resuts.sampleStart(); try { testHandler(); resuts.setSuccessful(true); } catch (Exception e) { resuts.setSuccessful(false); } resuts.sampleEnd(); return resuts; } /** * 核心業務執行功能實現,只需實現此接口便可 * @throws Exception */ public abstract void testHandler() throws Exception; }
而後咱們能夠建立不一樣的Java請求,繼承這個基類,只需實現testHandler()方法便可。如:
curator獲取數據的Java Request:
public class GetDataRequest extends CuratorBaseRequest { public void testHandler() throws Exception { byte[] bytes = client.getData().forPath("/setData"); } }
Curaotr設置數據的Java Request:
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; public class SetDataRequest extends CuratorBaseRequest { private byte[] a100 = new byte[100]; public void setupTest(JavaSamplerContext context) { for (int i = 0; i < a100.length; i++) { a100[i] = 'A'; } super.setupTest(context); } public void testHandler() throws Exception { client.setData().forPath("/setData", a100); } }
這樣,能夠避免大量的冗餘代碼。
優化技巧二
在編寫完Java Sample後,每次都須要咱們打成jar包,而後再複製jar包到${jmeter_home}\lib\ext下,而後再重起jmeter,這樣操做比較繁瑣。這時,咱們能夠經過在maven項目中添加一個maven插件來幫助咱們簡化操做。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>copy-files</id> <phase>package</phase> <configuration> <tasks> <!--將生成的項目jar拷貝到jmeter的lib/ext目錄下 --> <copy file="${project.build.directory}/yss_curator-0.0.1-SNAPSHOT.jar" tofile="D:\apache-jmeter-2.13\lib\ext\yss_curator-0.0.1-SNAPSHOT.jar" overwrite="true" /> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
如此,咱們即可在執行mvn package命令的時候,自動將項目打包,並將包複製到指定的目錄下了,咱們只需再重啓jmeter就能夠了。