JMeter - JSR223 以毫秒为单位获取当前时间

JMeter - JSR223 get current time in milliseconds

我正在尝试使用以下 JSR223 采样器在简单负载测试中计算 SSE 流量延迟:

EventHandler eventHandler = eventText -> {
  count++;
  // get the time from the server
  def result = eventText.substring(eventText.indexOf("data='") + 6, eventText.indexOf("', event")).trim() as Long;  
  def currenTime = System.currentTimeMillis();
  def diff = currenTime - result;
  list.add (diff);
  resp = resp + "Time from server:" + result + ", JMeter time:" + currenTime + ", diff:"+ diff +"\n";  
};

SSEClient sseClient = SSEClient.builder().url(pURL).eventHandler(eventHandler).build();
      
sseClient.start();

sleep(SLEEP_TIME);

sseClient.shutdown();

来自服务器 (NodeJS -JavaScript) 的时间是 Date.now() 而 JMeter 上的时间是 System.currentTimeMillis()

Server和JMeter在同一台电脑上。

似乎时间方法没有对齐,因为我看到在某些情况下 JMeter 时间早于服务器时间:

所以我不能相信结果...

我应该在 JavaScript 端或 JMeter 端使用任何其他方法吗?

在任何情况下你都不能相信结果,因为在同一台机器上测试系统和负载生成器不是最好的主意,你不会得到可靠的结果,因为 race conditions. Moreover it will be much harder to analyze the bottlenecks even with PerfMon Plugin

也根据System.currentTimeMillis() function JavaDoc

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

所以如果你想测量上次和下一次SSE的时间差可以考虑使用System.nanoTime()

不过,最好将 JMeter 移动到另一台机器上,最好是 Linux,因为 System.currentTimeMillis() 函数的精度要高得多