Gå til indhold
DocsTry Aspire
DocsTry

Get started with the Grafana k6 integration

Dette indhold er ikke tilgængeligt i dit sprog endnu.

⭐ Community Toolkit k6 logo

Grafana k6 is an open-source performance and load-testing tool. The Aspire Community Toolkit k6 hosting integration models a k6 container and its HTTP API as an AppHost resource. This lets you run load tests beside the services they test and inspect their logs, endpoint, and health in the Aspire dashboard.

The hosting integration runs the k6 container. Your test script is mounted into that container, and can use the connection information Aspire supplies for referenced resources.

architecture-beta

  group apphost(server)[AppHost]
  group test(logos:javascript)[k6 test]

  service hosting(server)[k6 hosting integration] in apphost
  service k6(server)[k6 container resource] in apphost
  service script(logos:javascript)[JavaScript test script] in test
  service targetapp(server)[Application service] in apphost

  hosting:R --> L:k6
  script:R --> L:k6
  k6:B --> T:targetapp
  • Install Docker and make sure it is running.
  • Install the Aspire CLI and create an AppHost.
  • Create a JavaScript test script that k6 can execute.

Add CommunityToolkit.Aspire.Hosting.k6 to your AppHost. See Set up k6 in the AppHost for package installation options.

Mount the directory that contains your tests, then select the script to run:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
builder.AddK6("k6")
.WithBindMount("./scripts", "/scripts", isReadOnly: true)
.WithScript("/scripts/main.js");
builder.Build().Run();

Use the k6 APIs in the mounted script:

scripts/main.js
import http from 'k6/http';
import { sleep } from 'k6';
export default function () {
http.get('http://host.docker.internal:8080/');
sleep(1);
}

Replace the URL with the address appropriate for the service under test. When the service is also an Aspire resource, reference it from k6 and use the service-discovery configuration supplied by Aspire.

The k6 hosting reference covers the resource endpoint, health check, script arguments, browser image, OpenTelemetry environment variables, and publish behavior.