k6 integration
此内容尚不支持你的语言。
Grafana k6 is a performance & load testing tool. The Aspire k integration enables you to create new container instances from Aspire with the docker.io/grafana/k6 container image.
Hosting integration
Section titled “Hosting integration”To get started with the Aspire k6 hosting integration, install the CommunityToolkit.Aspire.Hosting.k6 NuGet package in the app host project.
aspire add communitytoolkit-k6Aspire CLI 是交互式的;按提示选择合适的搜索结果:
Select an integration to add:
> communitytoolkit-k6 (CommunityToolkit.Aspire.Hosting.k6)> Other results listed as selectable options...#:package CommunityToolkit.Aspire.Hosting.k6@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.k6" Version="*" />Add k6 server resource
Section titled “Add k6 server resource”In your app host project, call AddK6 on the builder instance to add a flagd container resource:
var builder = DistributedApplication.CreateBuilder(args);
var myService = builder.AddProject<Projects.MyService>();
var k6 = builder.AddK6("k6") .WithBindMount("scripts", "/scripts", true) .WithScript("/scripts/main.js") .WithReference(myService) .WaitFor(apiservice);
// After adding all resources, run the app...When Aspire adds a container image to the app host, as shown in the preceding example with the docker.io/grafana/k6 image, it creates a new k6 instance on your local machine. You may need to add a reference to your k6 instance in order to test this resource.
Scripts configuration
Section titled “Scripts configuration”You will need to add a JavaScript file that will be executed by k6. Here is a simple example:
import http from "k6/http";import { sleep } from "k6";
export default function () { http.get(`${__ENV.services__myService__http__0}/hello`);
sleep(1);}The services__myService__http__0 environment variable is used to get the http endpoint provided for the myService resource.
For more information on how to write a Grafana k6 script, see the Write your first test with Grafana k6.
Customize ports
Section titled “Customize ports”To customize the ports used by the k6 container resource, provide the port parameter to the AddK6 method:
var builder = DistributedApplication.CreateBuilder(args);
var myService = builder.AddProject<Projects.MyService>();
var k6 = builder.AddK6("k6", port: 6565) .WithBindMount("scripts", "/scripts", true) .WithScript("/scripts/main.js") .WithReference(myService) .WaitFor(apiservice);
// After adding all resources, run the app...The port parameter specifies the host port for the k6 HTTP endpoint. If this parameter is not provided, a random port is assigned.
Enable browser extensions
Section titled “Enable browser extensions”var builder = DistributedApplication.CreateBuilder(args);
var myService = builder.AddProject<Projects.MyService>();
var k6 = builder.AddK6("k6", enableBrowserExtensions: true) .WithBindMount("scripts", "/scripts", true) .WithScript("/scripts/main.js") .WithReference(myService) .WaitFor(apiservice);
// After adding all resources, run the app...The browser module brings browser automation and end-to-end web testing to k6 while supporting core k6 features. It adds browser-level APIs to interact with browsers and collect frontend performance metrics as part of your k6 tests.
For more information on how to use Grafana k6 browser, see the Using k6 browser.