From b79cdac80ce715a843c4545ab12fa08711818224 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 21 Aug 2023 18:20:35 +0200 Subject: [PATCH 1/3] Add new benchmark agent in the pipeline --- .azure-pipelines/ultimate-pipeline.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.azure-pipelines/ultimate-pipeline.yml b/.azure-pipelines/ultimate-pipeline.yml index 3f28b6f10b78..a8d37db8bd53 100644 --- a/.azure-pipelines/ultimate-pipeline.yml +++ b/.azure-pipelines/ultimate-pipeline.yml @@ -2707,6 +2707,8 @@ stages: agent: "BenchmarkAgent5" BenchmarkAgent6: agent: "BenchmarkAgent6" + BenchmarkAgent7: + agent: "BenchmarkAgent7" timeoutInMinutes: 30 pool: @@ -2780,6 +2782,12 @@ stages: artifact: benchmarks_results_BenchmarkAgent6 path: $(System.DefaultWorkingDirectory)/tracer/build_data/benchmarks + - task: DownloadPipelineArtifact@2 + displayName: Download results BenchmarkAgent7 + inputs: + artifact: benchmarks_results_BenchmarkAgent7 + path: $(System.DefaultWorkingDirectory)/tracer/build_data/benchmarks + # upload the combined results for simplicity in other pipelines - publish: tracer/build_data/benchmarks artifact: benchmarks_results From a0efbc6f5e6d7db33eccab14b3db23badccb7615 Mon Sep 17 00:00:00 2001 From: Anna Date: Mon, 21 Aug 2023 18:21:05 +0200 Subject: [PATCH 2/3] Add benchmark agent attribute for future benchmarks --- .../benchmarks/Benchmarks.Trace/AgentFilterAttribute.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tracer/test/benchmarks/Benchmarks.Trace/AgentFilterAttribute.cs b/tracer/test/benchmarks/Benchmarks.Trace/AgentFilterAttribute.cs index d2fa289f377e..d78095b3df9c 100644 --- a/tracer/test/benchmarks/Benchmarks.Trace/AgentFilterAttribute.cs +++ b/tracer/test/benchmarks/Benchmarks.Trace/AgentFilterAttribute.cs @@ -38,6 +38,12 @@ public BenchmarkAgent6Attribute() : base(Agent.BenchmarkAgent6) { } } + public class BenchmarkAgent7Attribute : AgentFilterAttribute + { + public BenchmarkAgent7Attribute() : base(Agent.BenchmarkAgent7) + { + } + } public abstract class AgentFilterAttribute : Attribute { @@ -56,6 +62,7 @@ public enum Agent BenchmarkAgent4, BenchmarkAgent5, BenchmarkAgent6, + BenchmarkAgent7, } } } From b39968e8ab8a406b35aee387d738ed8228145124 Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 23 Aug 2023 15:57:28 +0200 Subject: [PATCH 3/3] [ASM] Add a benchmark for waf run (#4528) * Add benchmark for waf run * Change code owners file * try iteration cleanup instead * Try going back to normal as Iteration cleanup seems to have solved it --- .github/CODEOWNERS | 1 + .../{ => Asm}/AppSecBodyBenchmark.cs | 2 +- .../Asm/AppSecWafBenchmark.cs | 131 ++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) rename tracer/test/benchmarks/Benchmarks.Trace/{ => Asm}/AppSecBodyBenchmark.cs (99%) create mode 100644 tracer/test/benchmarks/Benchmarks.Trace/Asm/AppSecWafBenchmark.cs diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index e9745a1956c5..4f744389004a 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -22,6 +22,7 @@ AspectsDefinitions.g.cs @DataDog/asm-dotnet /tracer/test/test-applications/integrations/Samples.InstrumentedTests/ @DataDog/asm-dotnet /tracer/src/Datadog.Trace/Tags.AppSec.cs @DataDog/asm-dotnet +/tracer/test/benchmarks/Benchmarks.Trace/Asm/ @DataDog/asm-dotnet # Profiler /profiler/ @DataDog/profiling-dotnet diff --git a/tracer/test/benchmarks/Benchmarks.Trace/AppSecBodyBenchmark.cs b/tracer/test/benchmarks/Benchmarks.Trace/Asm/AppSecBodyBenchmark.cs similarity index 99% rename from tracer/test/benchmarks/Benchmarks.Trace/AppSecBodyBenchmark.cs rename to tracer/test/benchmarks/Benchmarks.Trace/Asm/AppSecBodyBenchmark.cs index b44f9f7cdf2e..845079cbe589 100644 --- a/tracer/test/benchmarks/Benchmarks.Trace/AppSecBodyBenchmark.cs +++ b/tracer/test/benchmarks/Benchmarks.Trace/Asm/AppSecBodyBenchmark.cs @@ -20,7 +20,7 @@ using Microsoft.AspNetCore.Http.Features; #endif -namespace Benchmarks.Trace +namespace Benchmarks.Trace.Asm { [MemoryDiagnoser] [BenchmarkAgent2] diff --git a/tracer/test/benchmarks/Benchmarks.Trace/Asm/AppSecWafBenchmark.cs b/tracer/test/benchmarks/Benchmarks.Trace/Asm/AppSecWafBenchmark.cs new file mode 100644 index 000000000000..8a1f1447ebec --- /dev/null +++ b/tracer/test/benchmarks/Benchmarks.Trace/Asm/AppSecWafBenchmark.cs @@ -0,0 +1,131 @@ +// +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. +// + +using System; +using System.Collections.Generic; +using System.IO; +using BenchmarkDotNet.Attributes; +using Datadog.Trace; +using Datadog.Trace.AppSec; +using Datadog.Trace.AppSec.Waf; +using Datadog.Trace.AppSec.Waf.NativeBindings; + +namespace Benchmarks.Trace.Asm; + +[MemoryDiagnoser] +[BenchmarkAgent7] +public class AppSecWafBenchmark +{ + private const int TimeoutMicroSeconds = 1_000_000; + + private static readonly Waf Waf; + private Context _context; + + static AppSecWafBenchmark() + { + var fDesc = FrameworkDescription.Instance; + var rid = (fDesc.ProcessArchitecture, fDesc.OSPlatform) switch + { + ("x64", "Windows") => "win-x64", + ("x86", "Windows") => "win-x86", + ("x64", "Linux") => "linux-x64", + ("arm64", "Linux") => "linux-arm64", + _ => throw new Exception($"RID not detected or supported: {fDesc.OSPlatform} / {fDesc.ProcessArchitecture}") + }; + + var folder = new DirectoryInfo(Environment.CurrentDirectory); + var path = Environment.CurrentDirectory; + while (folder.Exists) + { + path = Path.Combine(folder.FullName, "./shared/bin/monitoring-home"); + if (Directory.Exists(path)) + { + break; + } + + if (folder == folder.Parent) + { + break; + } + + folder = folder.Parent; + } + + path = Path.Combine(path, $"./{rid}/"); + if (!Directory.Exists(path)) + { + throw new DirectoryNotFoundException($"The Path: '{path}' doesn't exist."); + } + + Environment.SetEnvironmentVariable("DD_INTERNAL_TRACE_NATIVE_ENGINE_PATH", path); + var libInitResult = WafLibraryInvoker.Initialize(); + if (!libInitResult.Success) + { + throw new ArgumentException("Waf could not load"); + } + + var wafLibraryInvoker = libInitResult.WafLibraryInvoker!; + var initResult = Waf.Create(wafLibraryInvoker, string.Empty, string.Empty); + Waf = initResult.Waf; + } + + public IEnumerable> Source() + { + yield return MakeNestedMap(10); + yield return MakeNestedMap(100); + yield return MakeNestedMap(1000); + } + + private static Dictionary MakeNestedMap(int nestingDepth) + { + var root = new Dictionary(); + var map = root; + + for (var i = 0; i < nestingDepth; i++) + { + if (i % 2 == 0) + { + var nextList = new List + { + true, + false, + false, + false, + true, + 123, + "lorem", + "ipsum", + "dolor", + AddressesConstants.RequestCookies, new Dictionary { { "something", ".htaccess" }, { "something2", ";shutdown--" } } + }; + map.Add("list", nextList); + } + + var nextMap = new Dictionary + { + { "lorem", "ipsum" }, + { "dolor", "sit" }, + { "amet", "amet" }, + { "lorem2", "dolor2" }, + { "sit2", true }, + { "amet3", 4356 } + }; + map.Add("item", nextMap); + map = nextMap; + } + + return root; + } + + [IterationSetup] + public void Setup() => _context = Waf.CreateContext() as Context; + + [IterationCleanup] + public void Cleanup() => _context.Dispose(); + + [Benchmark] + [ArgumentsSource(nameof(Source))] + public void RunWaf(Dictionary args) => _context.Run(args, TimeoutMicroSeconds); +}