Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions playground/Stress/Stress.ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,19 @@
return $"Sent requests to {string.Join(';', urls)}";
});

app.MapGet("/log-message-limit", ([FromServices] ILogger<Program> logger) =>
app.MapGet("/log-message-limit", async ([FromServices] ILogger<Program> logger) =>
{
const int LogCount = 20_000;
const int LogCount = 10_000;
const int BatchSize = 10;

for (var i = 0; i < LogCount; i++)
for (var i = 0; i < LogCount / BatchSize; i++)
{
logger.LogInformation("Log entry {LogEntryIndex}", i);
for (var j = 0; j < BatchSize; j++)
{
logger.LogInformation("Log entry {BatchIndex}-{LogEntryIndex}", i, j);
}

await Task.Delay(100);
}

return $"Created {LogCount} logs.";
Expand Down
4 changes: 1 addition & 3 deletions playground/Stress/Stress.ApiService/TraceCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task CreateTraceAsync(int count, bool createChildren)
{
var activityStack = new Stack<Activity>();

for (var i = 0; i < 10; i++)
for (var i = 0; i < count; i++)
{
if (i > 0)
{
Expand All @@ -54,8 +54,6 @@ public async Task CreateTraceAsync(int count, bool createChildren)
{
await CreateChildActivityAsync(name);
}

await Task.Delay(Random.Shared.Next(10, 50));
}

while (activityStack.Count > 0)
Expand Down
13 changes: 12 additions & 1 deletion playground/Stress/Stress.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.DependencyInjection;

var builder = DistributedApplication.CreateBuilder(args);
builder.Services.AddHttpClient();

for (var i = 0; i < 10; i++)
{
Expand All @@ -27,12 +30,20 @@
iconName: "CloudDatabase",
isHighlighted: true);

for (var i = 0; i < 30; i++)
serviceBuilder.WithHttpEndpoint(5180, name: $"http");
for (var i = 1; i <= 30; i++)
{
var port = 5180 + i;
serviceBuilder.WithHttpEndpoint(port, name: $"http-{port}");
}

serviceBuilder.WithHttpCommand("/write-console", "Write to console", method: HttpMethod.Get, iconName: "ContentViewGalleryLightning");
serviceBuilder.WithHttpCommand("/increment-counter", "Increment counter", method: HttpMethod.Get, iconName: "ContentViewGalleryLightning");
serviceBuilder.WithHttpCommand("/big-trace", "Big trace", method: HttpMethod.Get, iconName: "ContentViewGalleryLightning");
serviceBuilder.WithHttpCommand("/trace-limit", "Trace limit", method: HttpMethod.Get, iconName: "ContentViewGalleryLightning");
serviceBuilder.WithHttpCommand("/log-message", "Log message", method: HttpMethod.Get, iconName: "ContentViewGalleryLightning");
serviceBuilder.WithHttpCommand("/log-message-limit", "Log message limit", method: HttpMethod.Get, iconName: "ContentViewGalleryLightning");

builder.AddProject<Projects.Stress_TelemetryService>("stress-telemetryservice");

#if !SKIP_DASHBOARD_REFERENCE
Expand Down
80 changes: 80 additions & 0 deletions playground/Stress/Stress.AppHost/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.DependencyInjection;

internal static class ResourceBuilderExtensions
{
/// <summary>
/// Adds a command to the resource that sends an HTTP request to the specified path.
/// </summary>
public static IResourceBuilder<TResource> WithHttpsCommand<TResource>(this IResourceBuilder<TResource> builder,
string path,
string displayName,
HttpMethod? method = default,
string? endpointName = default,
string? iconName = default)
where TResource : IResourceWithEndpoints
=> WithHttpCommandImpl(builder, path, displayName, endpointName ?? "https", method, "https", iconName);

/// <summary>
/// Adds a command to the resource that sends an HTTP request to the specified path.
/// </summary>
public static IResourceBuilder<TResource> WithHttpCommand<TResource>(this IResourceBuilder<TResource> builder,
string path,
string displayName,
HttpMethod? method = default,
string? endpointName = default,
string? iconName = default)
where TResource : IResourceWithEndpoints
=> WithHttpCommandImpl(builder, path, displayName, endpointName ?? "http", method, "http", iconName);

private static IResourceBuilder<TResource> WithHttpCommandImpl<TResource>(this IResourceBuilder<TResource> builder,
string path,
string displayName,
string endpointName,
HttpMethod? method,
string expectedScheme,
string? iconName = default)
where TResource : IResourceWithEndpoints
{
method ??= HttpMethod.Post;

var endpoints = builder.Resource.GetEndpoints();
var endpoint = endpoints.FirstOrDefault(e => string.Equals(e.EndpointName, endpointName, StringComparison.OrdinalIgnoreCase))
?? throw new DistributedApplicationException($"Could not create HTTP command for resource '{builder.Resource.Name}' as no endpoint named '{endpointName}' was found.");

var commandType = $"http-{method.Method.ToLowerInvariant()}-{path.ToLowerInvariant()}-request";

builder.WithCommand(commandType, displayName, async context =>
{
if (!endpoint.IsAllocated)
{
return new ExecuteCommandResult { Success = false, ErrorMessage = "Endpoints are not yet allocated." };
}

if (!string.Equals(endpoint.Scheme, expectedScheme, StringComparison.OrdinalIgnoreCase))
{
return new ExecuteCommandResult { Success = false, ErrorMessage = $"The endpoint named '{endpointName}' on resource '{builder.Resource.Name}' does not have the expected scheme of '{expectedScheme}'." };
}

var uri = new UriBuilder(endpoint.Url) { Path = path }.Uri;
var httpClient = context.ServiceProvider.GetRequiredService<IHttpClientFactory>().CreateClient();
var request = new HttpRequestMessage(method, uri);
try
{
var response = await httpClient.SendAsync(request, context.CancellationToken);
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
return new ExecuteCommandResult { Success = false, ErrorMessage = ex.Message };
}
return new ExecuteCommandResult { Success = true };
},
iconName: iconName,
iconVariant: IconVariant.Regular);

return builder;
}
}
Loading