The goal is to prove with data whether the new code or existing code causes the slowdown, instead of arguing. My approach: 1. Measure the baseline and the change separately - Deploy or run the previous version (without my 10 lines) and the new version in the same environment with the same data and load, and compare latency (p50, p95, p99), CPU, memory and database calls. - If observability exists, compare production metrics before and after the release in Application Insights or Grafana; a step change exactly at the deployment time points to the release. 2. Use distributed tracing and profiling to see where the time goes - Traces (Application Insights or OpenTelemetry) break a slow request into spans: SQL queries, HTTP calls and internal operations. If my new code added a database call inside a loop, it appears immediately. - A profiler shows CPU hot paths and allocations: dotnet-trace (collect a trace, open it in Visual Studio, PerfView or Speedscope), Visual Studio Performance Profiler, or JetBrains dotTrace. dotnet-counters shows live CPU, GC and thread-pool behaviour. 3. Benchmark the specific code with BenchmarkDotNet To isolate my change, I benchmark the old and new implementations side by side: [MemoryDiagnoser] public class OrderMappingBenchmarks { private readonly Order _order = TestData.LargeOrder(); [Benchmark(Baseline = true)] public OrderDto Original() => LegacyMapper.Map(_order); [Benchmark] public OrderDto WithNewValidation() => NewMapper.Map(_order); } // Program.cs BenchmarkRunner.Run<OrderMappingBenchmarks>(); How to use BenchmarkDotNet correctly - Put benchmarks in a separate console project and run in Release mode: dotnet run -c Release. - It handles warm-up, JIT effects, many iterations and statistics, reporting mean time, error, standard deviation and the ratio to the baseline. - [MemoryDiagnoser] shows allocations and GC counts per operation. - Use [Params] to test different input sizes, since problems often appear only with large data. - Benchmark realistic inputs and avoid dead-code elimination by returning results. 4. Check the usual suspects in the new lines - New database or HTTP calls (especially per item in a loop — N+1), synchronous blocking calls, logging large objects, exceptions used for flow control, and heavy allocations. 5. Conclude and communicate "Benchmarks show my change adds 0.3 ms per request, while traces show 1.8 seconds spent in an existing query that started scanning after table growth." Share the data, then fix whichever part is responsible.
Ravindra Wadile · · 0 likes