JMeter脚本的Beanshell和JSR223和Java原生请求的对比

Stella981
• 阅读 859

前言

结合之前一篇介绍JMeter测试Kafka的文章,遇到了JSR223,Beanshell等概念,这篇文章有讲解,转帖出来。

正文

Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!

There are several options to perform custom JMeter scripting and extend baseline JMeter functionality. Check out the most popular extension mechanisms, compare performance and analyze which one is the best. 

It's Battle of the Bands, JMeter style.

Beanshell V. JSR223 V. Java Request Sampler

In our previous post,  JMeter Performance and Tuning Tips (written by the fantastik Philipe Mouwad) we recommended using  JSR 223 + Groovy for Scripting. It’s the best option as Groovy scripts can be compiled into native Java code (assuming some requirements are met) so Groovy script execution performance can be almost as fast as Java code. 

Groovy脚本是一个进行测试很好的选择,因为它可以被编译成原生的Java代码,它执行效率可以和Java代码一样快。

So if you’re about to use scripts for something once, quick 'n dirty (e.g. reading configuration files at the beginning of the test) you’re welcome to use Beanshell/Javascript/whatever you’re comfortable with.

如果你只是想要使用脚本做一次什么事情,quick 'n dirty是啥意思,例如在测试开始时读取配置,你可以使用任何脚本。

But, if you’re about to do some extensive load testing through scripting (i.e. constructing massive HTTP requests from calculated data) you need to consider either Groovy or custom Java requests or a JMeter Sampler.

但是,如果您要通过脚本进行一些广泛的负载测试(即从计算数据构建大量HTTP请求),则需要考虑Groovy或自定义Java请求或JMeter Sampler。

For comparison purposes, we’re going to use the same simple code which generates a 1Mb string of random alphanumeric characters. 

处于对比的目的,我们将要使用同一个简单的代码,生成1M的随机数字字母的字符串。

JMeter脚本的Beanshell和JSR223和Java原生请求的对比 JMeter脚本的Beanshell和JSR223和Java原生请求的对比

Comparison metrics will be collected using  10 users x 100 iterations with a  session duration of 1 hour (standard BlazeMeter session length), load generation will be done from single JMeter Console by execution of the above code and demonstrating key performance indicators of following engines:

1个小时内10个用户,100次迭代,标准的BlazeMeter会话长度。

  • Beanshell (as is)

  • JSR223 (Groovy as language, compilation caching enabled)

  • Java (as JMeter Java Request Sampler)

Including associated CPU/RAM cost on load generator side (BlazeMeter Console)

Tests will be using following environment:

  • Test Type - JMeter Test (Sandbox)

  • Threads - 10 threads per Thread Group

  • Server Type - Large

  • Session Time - 1 hour

  • Engine - Console Only

  • Server - 1

  • Threads per engine - 10

  • Rampup - no ramp-up

  • Iterations - 100

  • Server OS - Linux

  • Server CPU - 2x

  • Java 7 x64 1.7.0_03

  • JVM arguments - -server  -Xms3072m -Xmx6144m -XX:NewSize=64m -XX:MaxNewSize=128m  -XX:MaxTenuringThreshold=2  -Dsun.rmi.dgc.client.gcInterval=600000 -Dsun.rmi.dgc.server.gcInterval=600000 -XX:PermSize=64m -XX:MaxPermSize=64m

 

Beanshell Sampler

Configuration

For Beanshell, no pre-requisites are required, everything works out-of-the-box. All we need is to do is add a Beanshell Sampler and paste 1Mb random string generation code. After uploading test scripts to BlazeMeter's testing dashboard and setting appropriate threads, iterations and duration we got the following results:

Load Test Results

JMeter脚本的Beanshell和JSR223和Java原生请求的对比 JMeter脚本的Beanshell和JSR223和Java原生请求的对比

JSR233 Sampler

Configuration

As mentioned, for scripting it’s recommended to use JSR233 + Groovy. Groovy isn’t shipped with JMeter, it needs to be downloaded separately.  To get started:

  1. Download latest groovy binary bundle from Groovy website download area

  2. Locate groovy-all-${VERSION}.jar under “embeddable” folder of distribution and drop it to JMeter/lib folder. Or upload it to BlazeMeter in the “Files” area, BlazeMeter will place it into the corresponding location

  3. Add JSR233 Sampler to Thread Group specifying “groovy” as Language

  4. Set Compilation cache key to something unique 

  5. Paste 1Mb random string generation code to Script area

Groovy的测试要复杂一些,需要下载包。

Important to note:

• Use .groovy files instead of keeping the Groovy code inside the sampler. However if you need to have code directly in sampler, make sure that you have Compilation Cache Key set. If you have > 1 JSR233 Sampler – make sure that they use different keys

• Don’t refer any variables as ${VAR} inside the Groovy script. Use either vars.get(“VAR”) or the Parameters stanza of JSR233 Sampler

用单独的.groovy文件代替在sampler里面写代码。如果你使用了sampler里面写代码,确认你设置了Compilation Cache Key。

Load Test Results

JMeter脚本的Beanshell和JSR223和Java原生请求的对比 JMeter脚本的Beanshell和JSR223和Java原生请求的对比

Java Request

Configuration

Java Request is your own implementation of JavaSamplerClient, all described methods should have appropriate code. The absolute minimum is override of runTest() method but if you intent to parameterize your Java Request you need to provide appropriate logic  to read inputs and conditional interpretation of requests flow to determine whether Sampler passed or not. 

Java原生请求就是自己来写了,需要暴露一个runTest()方法。

The example code for generation of 1Mb random string via Java Request will look as follows:

JMeter脚本的Beanshell和JSR223和Java原生请求的对比 JMeter脚本的Beanshell和JSR223和Java原生请求的对比

Compiled class needs to be placed in /lib/ext folder of your JMeter installation preferably in .jar form so JMeter could automatically pick it up, elsewise you’ll need to amend JMeter classpath. 

When using BlazeMeter just upload the .jar file altogether with your script and other extensions (if any) and the BlazeMeter engine will pick it up. 

2013/12/05 11:09:38 INFO - BlazeMeter: BlazeMeter startup script completed

2013/12/05 11:09:39 INFO - jmeter.protocol.java.sampler.JavaSampler: Created class: com.blazemeter.JavaRequest

Load Test Results

JMeter脚本的Beanshell和JSR223和Java原生请求的对比 JMeter脚本的Beanshell和JSR223和Java原生请求的对比

Conclusion

JMeter脚本的Beanshell和JSR223和Java原生请求的对比 JMeter脚本的Beanshell和JSR223和Java原生请求的对比

Beanshell is recommended to be used for once-only activities like reading configuration file somewhere in single-threaded setUp Thread Group or in situations, where no possibility to use alternatives exists. 

JSR233/Groovy is quite reasonable option for scripting but only with “compilation” feature.

And the winner is......... Java Request which provides blazing performance and cutting-edge productivity!

最后,Java Request最屌

Learn More

Want to learn more? You might be interested in viewing the webinar, Advanced JMeter Scripting - Writing Assertions in Groovy.

Check out BlazeMeter's Knowledge Base for "loads" of  JMeter, BlazeMeter and load testing tutorials!

Click here to subscribe to our newsletter.

Try out BlazeMeter - put your URL or JMX file in the box below and your test will sart in minutes.

总结

这篇文章的结果是,Groovy的效率比Java原生相差不是太大,但是更加易于编写。

参考

https://www.blazemeter.com/blog/beanshell-vs-jsr223-vs-java-jmeter-scripting-its-performance?utm_source=blog&utm_medium=BM_blog&utm_campaign=apache-kafka-how-to-load-test-with-jmeter

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
3年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这