JMeter获取最近时间戳

还是玩得不溜,取此刻前一分钟的毫秒级时间戳,搞了老久了

首先想到的是${__time(,)},可以获取此刻毫秒级时间戳

然后想到

${__time(/1000,)}

得到的是秒级时间戳

找葫芦画瓢,减去60000就是一分钟前毫秒级时间戳

${__time(-60000,)}

可惜,结果却是-60000,蛋碎,坑爹的官方文档上只有除

__time

The time function returns the current time in various formats.

Parameters

Attribute
Description
Required
Format
The format to be passed to SimpleDateFormat. The function supports various shorthand aliases, see below. If omitted, the function returns the current time in milliseconds since the epoch.
No
Name of variable
The name of the variable to set.
No

If the format string is omitted, then the function returns the current time in milliseconds since the epoch. If the format matches “/ddd” (where ddd are decimal digits), then the function returns the current time in milliseconds divided by the value of ddd. For example, “/1000” returns the current time in seconds since the epoch. Otherwise, the current time is passed to SimpleDateFormat. The following shorthand aliases are provided:

  • YMD = yyyyMMdd
  • HMS = HHmmss
  • YMDHMS = yyyyMMdd-HHmmss
  • USER1 = whatever is in the JMeter property time.USER1
  • USER2 = whatever is in the JMeter property time.USER2

The defaults can be changed by setting the appropriate JMeter property, e.g. time.YMD=yyMMdd

${__time(dd/MM/yyyy,)}

will return 21/01/2018 if ran on 21 january 2018

${__time(YMD,)}

will return 20180121 if ran on 21 january 2018

${__time()}

will return time in millis 1516540541624

这样不行,又想到一个馊主意,intSum好了

比如这样

${__intSum(1,-2,)}

结果就是-1,那么我就用当前时间戳减去60000好了

${__intSum(${__time(,)},-60000,)}

结果居然报了一段错误

For input string: "1535644348229", 
stacktrace:
 java.lang.NumberFormatException: For input string: "1535644348229"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:583)
	at java.lang.Integer.parseInt(Integer.java:615)
	at org.apache.jmeter.functions.IntSum.execute(IntSum.java:67)
	at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:137)
	at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:112)
	at org.apache.jmeter.functions.gui.FunctionHelper.actionPerformed(FunctionHelper.java:200)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6533)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6298)
	at java.awt.Container.processEvent(Container.java:2238)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2296)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4897)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4475)
	at java.awt.Container.dispatchEventImpl(Container.java:2282)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
	at java.awt.EventQueue$4.run(EventQueue.java:733)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

显然timestamp是Long类型,int肯定不够,看到这里,傻乎乎地去BeanShell里写Java代码类型转换去了,二了

其实有一个__longSum函数,满足要求,并且对于使用Function Helper,最好是将生成正确结果后,copy命令,而不是在外面编辑

比如这样就很容易发现可行

NewImage

如果你JS玩的很溜,可以用__javaScript来实现

${__javaScript(${__javaScript(new Number(parseInt(new Date().getTime()) - 60000).toString(),)},)}

具体可查看

__javaScript

The javaScript function executes a piece of JavaScript (not Java!) code and returns its value

The JMeter Javascript function calls a standalone JavaScript interpreter. Javascript is used as a scripting language, so you can do calculations etc.

javaScript is not the best scripting language for performances in JMeter. If your plan requires a high number of threads it is advised to use __jexl3 or __groovy functions.

For Nashorn Engine, please see Java Platform, Standard Edition Nashorn User’s Guide.
For Rhino engine, please see Mozilla Rhino Overview

The following variables are made available to the script:

Rhinoscript allows access to static methods via its Packages object. See the Scripting Java documentation. For example one can access the JMeterContextService static methods thus:Java.type(“org.apache.jmeter.threads.JMeterContextService”).getTotalThreads()

JMeter is not a browser, and does not interpret the JavaScript in downloaded pages.

Parameters

Attribute
Description
Required
Expression
The JavaScript expression to be executed. For example:

  • new Date() – return the current date and time
  • Math.floor(Math.random()*(${maxRandom}+1)) – a random number between 0 and the variable maxRandom
  • ${minRandom}+Math.floor(Math.random()*(${maxRandom}-${minRandom}+1)) – a random number between the variables minRandom and maxRandom
  • “${VAR}”==”abcd”
Yes
Variable Name
A reference name for reusing the value computed by this function.
No
Remember to include any necessary quotes for text strings and JMeter variables. Also, if the expression has commas, please make sure to escape them. For example in:

${__javaScript('${sp}'.slice(7\,99999))}

the comma after 7 is escaped.

解决问题的办法很多,关键就看熟练与否

官方手册:https://jmeter.apache.org/usermanual/index.html

发表回复