Exercise to investigate K6 Load testing tool and BenchmarkDotNet NuGet package for Load and performance testing doing a comparation between Exceptions and a Result Pattern.
The results of this tests does not imply Exceptions should never being used neither Result Pattern has to be implemented always. Each technique should be used properly attached to the use case. To read moare about that discussion:
- https://martinfowler.com/articles/replaceThrowWithNotification.html
- https://www.reddit.com/r/csharp/comments/1aoz4b0/result_pattern_vs_exceptions_pros_cons/
- https://www.reddit.com/r/dotnet/comments/11751gx/exceptions_vs_result_pattern/
Having two endpoints that do the same thing, one using exceptions and the other using a Result Pattern, we will compare the performance of both.
- We simulate ramp-up of traffic from 1 to 20 users over 10 seconds.
- Stay at 20 users for 50 seconds
- Assure you have k6 installed. If not, you can download it from here
- Set Exceptions.Vs.ResultPattern.Api as startup project
- Run it without debugging (Ctrl + F5)
- Open a terminal and navigate to the root of the project
- Run the following command to execute the load test scenario you want to test. Assure you pass the correct file name.
k6 run api-test-add-book-v1.js
- Check the results in the terminal
Around 4228 request per second
Around 3893 request per second
Around 30296 request per second. 7 times faster than Exceptions scenario
Around 23469 request per second. 6 times faster than Exceptions scenario
- Set Exceptions.Vs.ResultPattern.PerformanceTests as startup project
- Build the solution in Release mode
- Run it without debugging (Ctrl + F5)
- Check the results in the terminal. Also the results are being exported in HTML and Markdown on
\Benchmarking.Exceptions.Vs.ResultPattern\bin\Release\net8.0\BenchmarkDotNet.Artifacts\resultsfolder
BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.4112/23H2/2023Update/SunValley3) 12th Gen Intel Core i5-1240P, 1 CPU, 16 logical and 12 physical cores .NET SDK 8.0.304 [Host] : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2 DefaultJob : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | StdErr | Min | Q1 | Median | Q3 | Max | Op/s | Gen0 | Exceptions | Completed Work Items | Lock Contentions | Allocated |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| AddBookOrThrowBenchmark | 501.399 μs | 11.9045 μs | 34.7260 μs | 3.5079 μs | 432.779 μs | 477.070 μs | 493.169 μs | 523.111 μs | 586.209 μs | 1,994.4 | 6.8359 | 100.0000 | - | - | 42.97 KB |
| AddBookOrFailBenchmark | 13.057 μs | 0.2603 μs | 0.5137 μs | 0.0742 μs | 11.850 μs | 12.688 μs | 13.016 μs | 13.378 μs | 14.353 μs | 76,588.3 | 7.9041 | - | - | - | 48.44 KB |
| GetBookOrThrowBenchmark | 425.688 μs | 8.3973 μs | 10.3127 μs | 2.1987 μs | 409.043 μs | 416.912 μs | 426.451 μs | 432.276 μs | 441.762 μs | 2,349.1 | 6.8359 | 100.0000 | - | - | 43.75 KB |
| GetBookOrFailBenchmark | 4.889 μs | 0.0918 μs | 0.1832 μs | 0.0262 μs | 4.497 μs | 4.765 μs | 4.871 μs | 4.971 μs | 5.317 μs | 204,528.6 | 3.1815 | - | - | - | 19.53 KB |
AddBookOrFailBenchmark using Result Pattern is 38 times faster than AddBookOrThrowBenchmark using Exceptions.
GetBookOrFailBenchmark using Result Pattern is 87 times faster than GetBookOrThrowBenchmark using Exceptions.



