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

评估k6负载测试能力:如何加载网页关联资源?

Does k6 support loading all resources referenced in HTML responses for load testing?

Great question! Let me break down how k6 can handle loading embedded resources from your HTML pages (without rendering them, just as you need) for your LAMP stack load testing.

First off, k6 doesn’t have a single "one-click" function to automatically load every resource linked in an HTML page, but it gives you all the tools to build this behavior easily—using its built-in HTML parsing and batch request capabilities. Here’s a step-by-step approach:

How to implement resource loading in k6

1. Fetch the main page and parse its HTML

When you make a request to your page with http.get(), the response object includes an .html() method. This uses a cheerio-based parser to let you query the DOM just like you would with jQuery.

2. Extract resource URLs from the parsed HTML

Use DOM selectors to pull URLs from elements that reference resources:

  • Images via <img> tags (extract the src attribute)
  • Stylesheets via <link rel="stylesheet"> tags (extract the href attribute)
  • Scripts via <script src> tags (extract the src attribute)
  • Any other resources your pages use (like fonts, iframes, etc.)

Don’t forget to resolve relative URLs by combining them with the main page’s URL—k6 needs absolute paths to make valid requests.

3. Batch-request all extracted resources

Use http.batch() to send all resource requests in parallel (mimicking browser behavior). All these requests will be included in your load test metrics (response times, success rates, etc.), so any database triggers from resource loads will be captured in your test.

Example code

import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  // 1. Request the main page
  const mainRes = http.get('https://your-lamp-site.com');
  
  // 2. Parse HTML and extract resource URLs
  const doc = mainRes.html();
  const resourceUrls = [];
  
  // Extract images
  doc.find('img').each((idx, el) => {
    const src = el.getAttribute('src');
    if (src) {
      // Resolve relative URLs to absolute
      const absoluteUrl = new URL(src, mainRes.url).href;
      resourceUrls.push(absoluteUrl);
    }
  });
  
  // Extract stylesheets
  doc.find('link[rel="stylesheet"]').each((idx, el) => {
    const href = el.getAttribute('href');
    if (href) {
      const absoluteUrl = new URL(href, mainRes.url).href;
      resourceUrls.push(absoluteUrl);
    }
  });
  
  // Extract scripts with src attributes
  doc.find('script[src]').each((idx, el) => {
    const src = el.getAttribute('src');
    if (src) {
      const absoluteUrl = new URL(src, mainRes.url).href;
      resourceUrls.push(absoluteUrl);
    }
  });
  
  // 3. Batch-request all resources
  const batchRequests = resourceUrls.map(url => ['GET', url]);
  http.batch(batchRequests);
  
  sleep(1); // Simulate user wait time between actions
}

Key notes for your use case

  • This approach captures all resource-related database requests, which aligns with your requirement to include those in load testing.
  • You can customize the DOM selectors to include/exclude specific resource types based on your pages’ structure.
  • The batch request method is efficient and mirrors how real browsers load multiple resources simultaneously, making your test more realistic.

While it’s not a fully automated "out-of-the-box" command, implementing this in k6 is straightforward and gives you full control over what’s included in your test—perfect for your mostly static LAMP stack setup.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 07:52:28