79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
// <copyright file="Spdx3ParserBenchmarks.cs" company="StellaOps">
|
|
// Copyright (c) StellaOps. Licensed under the BUSL-1.1.
|
|
// </copyright>
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
using StellaOps.Spdx3.JsonLd;
|
|
using StellaOps.Spdx3.Tests.TestInfrastructure;
|
|
|
|
namespace StellaOps.Spdx3.Tests;
|
|
|
|
/// <summary>
|
|
/// Performance benchmarks for SPDX 3.0.1 parser.
|
|
/// Task: SP3-018 - Validate parsing performance for various document sizes.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// These tests measure parsing performance and compare against 2.x parser baseline.
|
|
/// Target: SPDX 3.0.1 parser should be within 2x of 2.x parser performance.
|
|
/// </remarks>
|
|
[Trait("Category", "Performance")]
|
|
[Trait("Intent", "Performance")]
|
|
public sealed partial class Spdx3ParserBenchmarks : IDisposable
|
|
{
|
|
private const int WarmupIterations = 3;
|
|
private const int BenchmarkIterations = 10;
|
|
private static readonly string TempDirRoot = Path.Combine(Path.GetTempPath(), "stellaops-tests", "spdx3-benchmarks");
|
|
|
|
private readonly Spdx3Parser _parser;
|
|
private readonly MemoryCache _cache;
|
|
private readonly string _tempDir;
|
|
|
|
public Spdx3ParserBenchmarks()
|
|
{
|
|
_cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000 });
|
|
var httpClientFactory = new Mock<IHttpClientFactory>();
|
|
var options = Options.Create(new Spdx3ContextResolverOptions { AllowRemoteContexts = false });
|
|
var resolver = new Spdx3ContextResolver(
|
|
httpClientFactory.Object,
|
|
_cache,
|
|
NullLogger<Spdx3ContextResolver>.Instance,
|
|
options,
|
|
TimeProvider.System);
|
|
|
|
_parser = new Spdx3Parser(resolver, NullLogger<Spdx3Parser>.Instance);
|
|
_tempDir = TempDirRoot;
|
|
ResetTempDir();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_cache.Dispose();
|
|
TryDeleteTempDir();
|
|
}
|
|
|
|
private string CreateDocument(int packageCount) => Spdx3BenchmarkDocumentBuilder.WriteDocument(_tempDir, packageCount);
|
|
|
|
private void ResetTempDir()
|
|
{
|
|
TryDeleteTempDir();
|
|
Directory.CreateDirectory(_tempDir);
|
|
}
|
|
|
|
private void TryDeleteTempDir()
|
|
{
|
|
try
|
|
{
|
|
if (Directory.Exists(_tempDir))
|
|
{
|
|
Directory.Delete(_tempDir, recursive: true);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Best effort cleanup.
|
|
}
|
|
}
|
|
}
|