跳转到内容

k6 integration

此内容尚不支持你的语言。

⭐ Community Toolkit k6 logo

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.

To get started with the Aspire k6 hosting integration, install the CommunityToolkit.Aspire.Hosting.k6 NuGet package in the app host project.

Aspire CLI — 添加 CommunityToolkit.Aspire.Hosting.k6 包
aspire add communitytoolkit-k6

Aspire CLI 是交互式的;按提示选择合适的搜索结果:

Aspire CLI — 输出示例
Select an integration to add:
> communitytoolkit-k6 (CommunityToolkit.Aspire.Hosting.k6)
> Other results listed as selectable options...

In your app host project, call AddK6 on the builder instance to add a flagd container resource:

C# — AppHost.cs
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.

You will need to add a JavaScript file that will be executed by k6. Here is a simple example:

scripts/main.js
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.

To customize the ports used by the k6 container resource, provide the port parameter to the AddK6 method:

C# — AppHost.cs
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.

C# — AppHost.cs
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.

问 & 答协作社区讨论观看