You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Artillery.io测试中发送含当前时间的消息?

在Artillery.io中实现每个请求使用实时当前时间

完全可以实现,你需要借助Artillery的自定义处理器函数来动态生成实时时间,具体步骤如下:

1. 编写自定义时间生成函数

创建一个名为custom-processors.js的文件,内容如下:

module.exports = {
  // 生成实时当前时间,可自定义格式
  getCurrentTime: function() {
    // 返回ISO标准格式时间,比如 "2024-05-20T14:30:00.123Z"
    return new Date().toISOString();
    
    // 如果需要本地时间格式,比如 "2024/05/20 14:30:00",可以换成:
    // return new Date().toLocaleString('zh-CN', {
    //   year: 'numeric',
    //   month: '2-digit',
    //   day: '2-digit',
    //   hour: '2-digit',
    //   minute: '2-digit',
    //   second: '2-digit'
    // }).replace(/\//g, '-');
  }
};

2. 修改Artillery测试配置

在你的配置文件中,添加processor字段引入自定义函数,并在emit的data中调用这个函数:

config:
  target: "http://localhost:3000/"
  # 引入自定义处理器文件
  processor: "./custom-processors.js"
  phases:
    - duration: 10 
      arrivalRate: 1
  environments:
    local:
      target: "http://localhost:3000/"
      phases:
        - duration: 10
          arrivalRate: 1
    prod:
      target: "http://localhost:3000/"
      phases:
        - duration: 100
          arrivalRate: 1 
          
scenarios:
  - name: "Emit an event"
    engine: socketio
    flow:
      - loop:
          - emit:
              channel: "chat message"
              # 调用自定义函数生成实时时间
              data: "{{ getCurrentTime() }}"
          - think: 1
        count: "{{ count }}" 

原理说明

每次执行emit步骤时,Artillery都会调用getCurrentTime()函数,该函数会返回当前执行时刻的实时时间,完全满足你每个请求都用最新时间的需求。

内容的提问来源于stack exchange,提问作者The Trainer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 05:12:16