From b00bf0e464ca2d30c4c02b15a2f1fb7a6ba35dc3 Mon Sep 17 00:00:00 2001 From: Mads Magnus Due Date: Mon, 18 Nov 2024 10:13:35 +0100 Subject: [PATCH] test: load test result is now added to as a custom metric (#1365) * test: load test result is now added to as a custom metric --------- Co-authored-by: Ebbe Knudsen --- .../SubsystemTests/Drivers/EdiDatabaseDriver.cs | 15 +++++++++++++++ .../SubsystemTests/LoadTest/LoadTestFixture.cs | 16 ++++++++++++++++ source/SubsystemTests/LoadTest/LoadTestHelper.cs | 16 +++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/source/SubsystemTests/Drivers/EdiDatabaseDriver.cs b/source/SubsystemTests/Drivers/EdiDatabaseDriver.cs index 4de47f7c7b..89c62b73ac 100644 --- a/source/SubsystemTests/Drivers/EdiDatabaseDriver.cs +++ b/source/SubsystemTests/Drivers/EdiDatabaseDriver.cs @@ -274,6 +274,21 @@ WHERE B.DequeuedAt IS NOT NULL return dequeuedMessagesCount; } + internal async Task CountEnqueuedMessagesForCalculationAsync(Guid calculationId) + { + await using var connection = new SqlConnection(_connectionString); + + await connection.OpenAsync(); + + var enqueuedMessagesCount = await connection.ExecuteScalarAsync( + sql: @"SELECT COUNT(B.[Id]) FROM [Bundles] B + INNER JOIN [OutgoingMessages] OM ON B.[Id] = OM.[AssignedBundleId] + WHERE OM.[CalculationId] = @CalculationId", + param: new { CalculationId = calculationId, }); + + return enqueuedMessagesCount; + } + private async Task GetProcessIdAsync(SqlCommand command, CancellationToken cancellationToken) { await using var connection = new SqlConnection(_connectionString); diff --git a/source/SubsystemTests/LoadTest/LoadTestFixture.cs b/source/SubsystemTests/LoadTest/LoadTestFixture.cs index 54c3a08f40..7a5edf40d8 100644 --- a/source/SubsystemTests/LoadTest/LoadTestFixture.cs +++ b/source/SubsystemTests/LoadTest/LoadTestFixture.cs @@ -18,6 +18,8 @@ using Energinet.DataHub.Core.FunctionApp.TestCommon.Configuration; using Energinet.DataHub.EDI.B2BApi.AppTests.DurableTask; using Energinet.DataHub.EDI.SubsystemTests.Drivers; +using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Azure.WebJobs.Extensions.DurableTask; using Microsoft.Extensions.Configuration; @@ -61,6 +63,11 @@ public LoadTestFixture() configuration, "LOAD_TEST_CALCULATION_ID"); + MinimumEnqueuedMessagesCount = GetConfigurationValue( + configuration, + "MINIMUM_ENQUEUED_MESSAGES_COUNT", + defaultValue: 0); + MinimumDequeuedMessagesCount = GetConfigurationValue( configuration, "MINIMUM_DEQUEUED_MESSAGES_COUNT", @@ -73,18 +80,27 @@ public LoadTestFixture() _durableTaskManager = new DurableTaskManager( "OrchestrationsStorageConnectionString", GetConfigurationValue(configuration, "func-edi-api-taskhub-storage-connection-string")); + + var credential = new DefaultAzureCredential(); + var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); + telemetryConfiguration.SetAzureTokenCredential(credential); + TelemetryClient = new TelemetryClient(telemetryConfiguration); } internal EdiInboxClient EdiInboxClient { get; } internal Guid LoadTestCalculationId { get; } + internal int MinimumEnqueuedMessagesCount { get; } + internal int MinimumDequeuedMessagesCount { get; } internal IntegrationEventPublisher IntegrationEventPublisher { get; } internal string DatabaseConnectionString { get; } + internal TelemetryClient TelemetryClient { get; } + [NotNull] internal IDurableClient? DurableClient { get; private set; } diff --git a/source/SubsystemTests/LoadTest/LoadTestHelper.cs b/source/SubsystemTests/LoadTest/LoadTestHelper.cs index 08e7f7e6ec..9a488154d9 100644 --- a/source/SubsystemTests/LoadTest/LoadTestHelper.cs +++ b/source/SubsystemTests/LoadTest/LoadTestHelper.cs @@ -16,6 +16,7 @@ using Energinet.DataHub.EDI.SubsystemTests.Drivers; using Energinet.DataHub.EDI.SubsystemTests.Dsl; using FluentAssertions; +using FluentAssertions.Execution; using Nito.AsyncEx; using NodaTime; using Xunit.Abstractions; @@ -35,6 +36,8 @@ namespace Energinet.DataHub.EDI.SubsystemTests.LoadTest; [SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "Test class")] public sealed class LoadTestHelper : IClassFixture { + private const string EnqueuedAmountMetric = "EnqueuedAmount"; + private const string DequeuedAmountMetric = "DequeuedAmount"; private readonly LoadTestFixture _fixture; private readonly ITestOutputHelper _logger; private readonly EdiDriver _ediDriver; @@ -69,13 +72,20 @@ await CalculationCompletedDsl.StartEnqueueMessagesOrchestration( [Fact] public async Task After_load_test() { - await _ediDriver.StopOrchestrationForCalculationAsync( - calculationId: _fixture.LoadTestCalculationId, - createdAfter: SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromHours(1))); + var enqueuedMessagesCount = await _ediDatabaseDriver.CountEnqueuedMessagesForCalculationAsync(_fixture.LoadTestCalculationId); + _logger.WriteLine($"Enqueued messages count: {enqueuedMessagesCount} (CalculationId={_fixture.LoadTestCalculationId})"); var dequeuedMessagesCount = await _ediDatabaseDriver.CountDequeuedMessagesForCalculationAsync(_fixture.LoadTestCalculationId); _logger.WriteLine($"Dequeued messages count: {dequeuedMessagesCount} (CalculationId={_fixture.LoadTestCalculationId})"); + _fixture.TelemetryClient.GetMetric(EnqueuedAmountMetric).TrackValue(enqueuedMessagesCount); + _fixture.TelemetryClient.GetMetric(DequeuedAmountMetric).TrackValue(dequeuedMessagesCount); + + using var scope = new AssertionScope(); + enqueuedMessagesCount.Should().BeGreaterThanOrEqualTo( + _fixture.MinimumEnqueuedMessagesCount, + $"because the system should be performant enough to enqueue at least {_fixture.MinimumEnqueuedMessagesCount} messages during the load test"); + dequeuedMessagesCount.Should().BeGreaterThanOrEqualTo( _fixture.MinimumDequeuedMessagesCount, $"because the system should be performant enough to dequeue at least {_fixture.MinimumDequeuedMessagesCount} messages during the load test");