菜单
开源

MetricMessage

MetricMessage 对象允许对页面测量和发出的指标进行标记。

当使用 page.on(‘metric’) 注册处理程序时,页面会分派 MetricMessage 对象。对于页面测量和发出的每个指标,k6 浏览器模块会向注册的处理程序传递一个 MetricMessage 对象,允许他们进行模式匹配和标记指标。

tag

tag 方法将给定的 matches 与当前指标的 url 和 name 标签进行匹配。找到匹配项时,它将使用 name 替换现有的 URL 和 name 标签值。

这样做有助于对具有不同 URL 和 name 标签但实际引用同一资源的指标进行分组,从而实现随时间推移的关联并降低指标的基数。

参数类型描述
tagMatch对象用于匹配指标的 tagMatch 对象及其属性。必需。
tagMatch.name字符串如果找到匹配项,则用于替换当前指标的 URL 和 name 标签值的 name 值。必需,且不能为空字符串。
tagMatch.matches对象数组包含用于匹配当前指标的 URL 和 name 标签的匹配器对象的数组。必需。
tagMatch.matches.urlRegExp用于在当前指标的 URL 和 name 标签中查找匹配项的正则表达式。必需。
tagMatch.matches.method字符串?用于匹配指标的 method 标签。有效值包括 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'TRACE''CONNECT'。这是可选的,未设置时将根据 method 标签对所有指标进行分组。

使用示例

JavaScript
// First we need to register a handler with `page.on('metric')`.
page.on('metric', (metric) => {
  // It will find a match between the current metric url and name tags against
  // the supplied regular expression in `url`.
  metric.tag({
    // This is the new name value that will replace the existing value in the
    // url and name tags when a match is found.
    name: 'test',
    // You can provide multiple matches here.
    matches: [
      {
        url: /^https:\/\/test\.k6\.io\/\?q=[0-9a-z]+$/,
        // When a method is defined it will also need to match on that too. If a
        // method is not provided it will match on all method types.
        method: 'GET',
      },
    ],
  });
});