// // Copyright (c) StellaOps. Licensed under the BUSL-1.1. // 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; /// /// Performance benchmarks for SPDX 3.0.1 parser. /// Task: SP3-018 - Validate parsing performance for various document sizes. /// /// /// 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. /// [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(); var options = Options.Create(new Spdx3ContextResolverOptions { AllowRemoteContexts = false }); var resolver = new Spdx3ContextResolver( httpClientFactory.Object, _cache, NullLogger.Instance, options, TimeProvider.System); _parser = new Spdx3Parser(resolver, NullLogger.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. } } }