feat(rate-limiting): Implement core rate limiting functionality with configuration, decision-making, metrics, middleware, and service registration
- Add RateLimitConfig for configuration management with YAML binding support. - Introduce RateLimitDecision to encapsulate the result of rate limit checks. - Implement RateLimitMetrics for OpenTelemetry metrics tracking. - Create RateLimitMiddleware for enforcing rate limits on incoming requests. - Develop RateLimitService to orchestrate instance and environment rate limit checks. - Add RateLimitServiceCollectionExtensions for dependency injection registration.
This commit is contained in:
137
bench/proof-chain/Benchmarks/IdGenerationBenchmarks.cs
Normal file
137
bench/proof-chain/Benchmarks/IdGenerationBenchmarks.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// IdGenerationBenchmarks.cs
|
||||
// Sprint: SPRINT_0501_0001_0001_proof_evidence_chain_master
|
||||
// Task: PROOF-MASTER-0005
|
||||
// Description: Benchmarks for content-addressed ID generation
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
|
||||
namespace StellaOps.Bench.ProofChain.Benchmarks;
|
||||
|
||||
/// <summary>
|
||||
/// Benchmarks for content-addressed ID generation operations.
|
||||
/// Target: Evidence ID generation < 50μs for 10KB payload.
|
||||
/// </summary>
|
||||
[MemoryDiagnoser]
|
||||
[SimpleJob(warmupCount: 3, iterationCount: 10)]
|
||||
public class IdGenerationBenchmarks
|
||||
{
|
||||
private byte[] _smallPayload = null!;
|
||||
private byte[] _mediumPayload = null!;
|
||||
private byte[] _largePayload = null!;
|
||||
private string _canonicalJson = null!;
|
||||
private Dictionary<string, object> _bundleData = null!;
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
// Small: 1KB
|
||||
_smallPayload = new byte[1024];
|
||||
RandomNumberGenerator.Fill(_smallPayload);
|
||||
|
||||
// Medium: 10KB
|
||||
_mediumPayload = new byte[10 * 1024];
|
||||
RandomNumberGenerator.Fill(_mediumPayload);
|
||||
|
||||
// Large: 100KB
|
||||
_largePayload = new byte[100 * 1024];
|
||||
RandomNumberGenerator.Fill(_largePayload);
|
||||
|
||||
// Canonical JSON for bundle ID generation
|
||||
_bundleData = new Dictionary<string, object>
|
||||
{
|
||||
["statements"] = Enumerable.Range(0, 5).Select(i => new
|
||||
{
|
||||
statementId = $"sha256:{Guid.NewGuid():N}",
|
||||
predicateType = "evidence.stella/v1",
|
||||
predicate = new { index = i, data = Convert.ToBase64String(_smallPayload) }
|
||||
}).ToList(),
|
||||
["signatures"] = new[]
|
||||
{
|
||||
new { keyId = "key-1", algorithm = "ES256" },
|
||||
new { keyId = "key-2", algorithm = "ES256" }
|
||||
}
|
||||
};
|
||||
|
||||
_canonicalJson = JsonSerializer.Serialize(_bundleData, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Baseline: Generate evidence ID from small (1KB) payload.
|
||||
/// Target: < 20μs
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public string GenerateEvidenceId_Small()
|
||||
{
|
||||
return GenerateContentAddressedId(_smallPayload, "evidence");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate evidence ID from medium (10KB) payload.
|
||||
/// Target: < 50μs
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public string GenerateEvidenceId_Medium()
|
||||
{
|
||||
return GenerateContentAddressedId(_mediumPayload, "evidence");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate evidence ID from large (100KB) payload.
|
||||
/// Target: < 200μs
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public string GenerateEvidenceId_Large()
|
||||
{
|
||||
return GenerateContentAddressedId(_largePayload, "evidence");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate proof bundle ID from JSON content.
|
||||
/// Target: < 500μs
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public string GenerateProofBundleId()
|
||||
{
|
||||
return GenerateContentAddressedId(Encoding.UTF8.GetBytes(_canonicalJson), "bundle");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate SBOM entry ID (includes PURL formatting).
|
||||
/// Target: < 30μs
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public string GenerateSbomEntryId()
|
||||
{
|
||||
var digest = "sha256:" + Convert.ToHexString(SHA256.HashData(_smallPayload)).ToLowerInvariant();
|
||||
var purl = "pkg:npm/%40scope/package@1.0.0";
|
||||
return $"{digest}:{purl}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate reasoning ID with timestamp.
|
||||
/// Target: < 25μs
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public string GenerateReasoningId()
|
||||
{
|
||||
var timestamp = DateTimeOffset.UtcNow.ToString("O");
|
||||
var input = Encoding.UTF8.GetBytes($"reasoning:{timestamp}:{_canonicalJson}");
|
||||
var hash = SHA256.HashData(input);
|
||||
return $"sha256:{Convert.ToHexString(hash).ToLowerInvariant()}";
|
||||
}
|
||||
|
||||
private static string GenerateContentAddressedId(byte[] content, string prefix)
|
||||
{
|
||||
var hash = SHA256.HashData(content);
|
||||
return $"sha256:{Convert.ToHexString(hash).ToLowerInvariant()}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user