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

使用Artillery+Playwright未捕获指定HTTP指标,仅显浏览器指标求助

Artillery + Playwright 无法获取HTTP响应时间等指标,仅显示浏览器指标

问题描述

我使用Artillery结合Playwright进行网站性能测试,在配置中指定了要捕获http.response_time.p50、http.response_time.p95、http.codes.200等HTTP相关指标,但运行测试后,报告里只显示浏览器级别的指标(如TTFB、FCP),完全看不到配置的HTTP指标。

我的Artillery配置

import { Page } from "@playwright/test";

export const config = {
  target: "https://www.artillery.io",
  defaults: {
    capture: {
      response: ["status_code", "latency"],
      metrics: [
        "http.response_time.p50",
        "http.response_time.p95",
        "http.response_time.p99",
         "http.response_time.min",
         "http.response_time.max",
        "http.request_rate",
        "http.codes.200",
        "http.codes.4xx",
        "http.codes.5xx",
      ],
    },
  },
  plugins: {
    expect: { enabled: true },
    ensure: {
      thresholds: {
        "browser.page.TTFB.https://www.artillery.io/": { p95: 100 },
        "vusers.session_length.min": 1000,
        "browser.page.codes.404": 0,
      },
    },
  },
  engines: {
    playwright: { showAllPageMetrics: false },
  },
  phases: [{ duration: 10, arrivalRate: 1, name: "Usersarrival" }],
};

export const scenarios = [
  {
    engine: "playwright",
    testFunction: helloWorld,
  },
];

export async function helloWorld(page: Page) {
  await page.goto("https://www.artillery.io/");
  await page.click("text=Docs");
}

当前得到的报告内容

browser.page.TTFB.https://www.artillery.io/: min: 49.7, max: 111, p95:63.4 browser.page.FCP.https://www.artillery.io/: min: 348.7, max: 921.8, p95: 528.6 browser.page.codes.200: 580 browser.page.codes.404: 10 vusers.session_length: min: 717.4, max: 2245.9, p95: 1353.1

解决方案

核心原因

Artillery的http.response_time.*等传统HTTP指标,默认是针对直接发送HTTP请求的场景设计的。当使用Playwright引擎时,所有请求都是通过真实浏览器发出的,Artillery不会自动捕获这类浏览器请求的HTTP指标,只会生成浏览器级别的页面指标。

方法1:启用Playwright的HTTP请求自动捕获

在Playwright引擎配置中添加captureHttpRequests: true,Artillery会自动捕获浏览器发出的每个HTTP请求的指标,这样你配置的http.response_time.*、http.codes.*等指标就会出现在报告中。

修改后的engines配置片段:

engines: {
  playwright: { 
    showAllPageMetrics: false,
    captureHttpRequests: true // 新增此配置项
  },
},

方法2:手动捕获并上报指标(自定义场景)

如果需要更精细的控制(比如只跟踪特定请求),可以在Playwright测试函数中监听请求事件,手动收集指标并通过Artillery的trackMetric方法上报:

import { Page } from "@playwright/test";
import { trackMetric } from 'artillery/core';

export async function helloWorld(page: Page) {
  // 监听所有完成的请求
  page.on('requestfinished', async (request) => {
    const response = await request.response();
    if (!response) return;

    const timing = request.timing();
    // 计算请求的响应时间
    const latency = timing.responseEnd - timing.requestStart;
    const statusCode = response.status();
    const requestUrl = request.url();

    // 上报响应时间指标
    trackMetric(`http.response_time.${requestUrl}`, latency);
    // 上报状态码计数
    trackMetric(`http.codes.${statusCode}`, 1, 'count');
  });

  await page.goto("https://www.artillery.io/");
  await page.click("text=Docs");
}

注意事项

  • 启用captureHttpRequests会带来一定的性能开销,如果是大规模并发测试,需要根据实际情况评估是否启用。
  • 手动上报指标时,可以根据需求过滤请求(比如只跟踪API请求或特定域名的请求),避免生成过多冗余指标。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 16:33:18