Skip to content

Commit

Permalink
test: forward metered data load test (#1421)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsDue authored Dec 13, 2024
1 parent 9e0e5c6 commit 2bb71be
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed class MeteredDataForMeasurementPointMessageProcessDto(
EventId eventId,
Actor receiver,
BusinessReason businessReason,
MessageId relatedToMessageId,
MeteredDataForMeasurementPointMessageSeriesDto series)
: OutgoingMessageDto(
DocumentType.NotifyValidatedMeasureData,
Expand All @@ -29,7 +30,7 @@ public sealed class MeteredDataForMeasurementPointMessageProcessDto(
businessReason.Name,
receiver.ActorRole,
new ExternalId(Guid.NewGuid()),
null)
relatedToMessageId)
{
public MeteredDataForMeasurementPointMessageSeriesDto Series { get; } = series;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ await _outgoingMessagesClient.EnqueueAndCommitAsync(
EventId.From(Guid.NewGuid()),
new Actor(ActorNumber.Create("8100000000115"), ActorRole.EnergySupplier),
BusinessReason.FromCode(marketMessage.BusinessReason),
MessageId.Create(marketMessage.MessageId),
new MeteredDataForMeasurementPointMessageSeriesDto(
TransactionId.From(string.Join(string.Empty, series.TransactionId.Reverse())),
series.MeteringPointLocationId!,
Expand Down
37 changes: 37 additions & 0 deletions source/SubsystemTests/Drivers/EdiDatabaseDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,29 @@ internal async Task DeleteOutgoingMessagesForCalculationAsync(Guid calculationId
}
}

/// <summary>
/// Delete outgoing messages for previuse performance test.
/// </summary>
internal async Task DeleteOutgoingMessagesForFromLoadTestAsync()
{
await using var connection = new SqlConnection(_connectionString);

await connection.OpenAsync().ConfigureAwait(false);
await using (var deleteOutgoingMessagesCommand = new SqlCommand())
{
deleteOutgoingMessagesCommand.CommandText = @"
DELETE FROM [MarketDocuments] WHERE BundleId IN (SELECT Id FROM [Bundles] WHERE RelatedToMessageId like 'perf_test_%');
DELETE FROM [OutgoingMessages] WHERE [AssignedBundleId] = (SELECT Id FROM [Bundles] WHERE RelatedToMessageId like 'perf_test_%');
DELETE FROM [Bundles] WHERE RelatedToMessageId like 'perf_test_%';
";

deleteOutgoingMessagesCommand.Connection = connection;
deleteOutgoingMessagesCommand.CommandTimeout = (int)TimeSpan.FromMinutes(2).TotalSeconds;

await deleteOutgoingMessagesCommand.ExecuteNonQueryAsync().ConfigureAwait(false);
}
}

internal async Task<(bool Success, string? Payload)>
GetOutboxMessageAsync(
Instant createdAfter,
Expand Down Expand Up @@ -289,6 +312,20 @@ internal async Task<int> CountEnqueuedMessagesForCalculationAsync(Guid calculati
return enqueuedMessagesCount;
}

internal async Task<double> CountEnqueuedNotifyValidatedMeasureDataMessagesFromLoadTestAsync()
{
await using var connection = new SqlConnection(_connectionString);

await connection.OpenAsync();

var enqueuedMessagesCount = await connection.ExecuteScalarAsync<int>(
sql: @"SELECT COUNT(B.[Id]) FROM [Bundles]
WHERE [DocumentTypeInBundle] = 'NotifyValidatedMeasureData'
AND RelatedToMessageId like 'perf_test_%'");

return enqueuedMessagesCount;
}

private async Task<Guid?> GetProcessIdAsync(SqlCommand command, CancellationToken cancellationToken)
{
await using var connection = new SqlConnection(_connectionString);
Expand Down
65 changes: 65 additions & 0 deletions source/SubsystemTests/LoadTest/ForwardMeteredData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Diagnostics.CodeAnalysis;
using Energinet.DataHub.EDI.SubsystemTests.Drivers;
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit.Abstractions;

namespace Energinet.DataHub.EDI.SubsystemTests.LoadTest;

/// <summary>
/// Test class used in the CI to trigger a calculation completed event, used for load testing on t001.
/// GitHub action should be as following:
/// 1. Run Before_load_test() test
/// 2. Start Azure Load Test
/// 3. Wait for the Azure Load Test to finish
/// 4. Run After_load_test() test
/// </summary>
[SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "Test class")]
public sealed class ForwardMeteredData : IClassFixture<LoadTestFixture>
{
private const string ForwardMeteredEnqueuedAmountMetric = "ForwardMeteredEnqueuedAmount";
private readonly LoadTestFixture _fixture;
private readonly ITestOutputHelper _logger;
private readonly EdiDatabaseDriver _ediDatabaseDriver;

public ForwardMeteredData(LoadTestFixture fixture, ITestOutputHelper logger)
{
_fixture = fixture;
_logger = logger;
_ediDatabaseDriver = new EdiDatabaseDriver(_fixture.DatabaseConnectionString);
}

[Fact]
public async Task Before_load_test()
{
await _ediDatabaseDriver.DeleteOutgoingMessagesForFromLoadTestAsync();
}

[Fact]
public async Task After_load_test()
{
var enqueuedMessagesCount = await _ediDatabaseDriver.CountEnqueuedNotifyValidatedMeasureDataMessagesFromLoadTestAsync();
_logger.WriteLine($"Enqueued messages count: {enqueuedMessagesCount} (CalculationId={_fixture.LoadTestCalculationId})");

_fixture.TelemetryClient.GetMetric(ForwardMeteredEnqueuedAmountMetric).TrackValue(enqueuedMessagesCount);

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");
}
}
3 changes: 2 additions & 1 deletion source/SubsystemTests/LoadTest/LoadTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public LoadTestFixture()

LoadTestCalculationId = GetConfigurationValue<Guid>(
configuration,
"LOAD_TEST_CALCULATION_ID");
"LOAD_TEST_CALCULATION_ID",
defaultValue: Guid.Empty);

MinimumEnqueuedMessagesCount = GetConfigurationValue<int>(
configuration,
Expand Down

0 comments on commit 2bb71be

Please sign in to comment.