DEV Community

Cover image for Minimal API Performance Benchmark
Spyros Ponaris
Spyros Ponaris

Posted on • Edited on

Minimal API Performance Benchmark

Benchmarking Minimal APIs vs Controllers in .NET

Inspired by the insightful article How to Increase Performance of Web APIs in .NET by Anton Martyniuk, I decided to create a minimal project to test the performance of Minimal APIs in .NET.

GitHub Repository

You can find the full project and source code on GitHub.

This project benchmarks Minimal APIs against Controllers, focusing on real-world scenarios like lightweight requests and data processing.

Goals of the Project

  • Compare the performance of Minimal APIs and traditional Controllers.
  • Evaluate their suitability for high-performance web applications.
  • Measure key metrics like execution time, memory allocation, and consistency across multiple iterations.

Features

  • Benchmarking Tool: Uses BenchmarkDotNet for accurate and repeatable measurements.

Scenarios Benchmarked:

  • Simple GET requests (e.g., /hello).
  • POST requests with JSON payloads (e.g., /data).

Detailed Metrics:

  • Captures execution time (Mean, Error, StdDev) and memory allocations.

Why Minimal APIs?

Minimal APIs in .NET are a lightweight alternative to Controllers, designed for performance-critical applications. They:

  • Reduce overhead by removing unnecessary abstractions.
  • Simplify the API development process.
  • Offer better performance for small-scale APIs and microservices.

This project aims to quantify these benefits with benchmarks.

Image description

Project Structure

  • Benchmarks/ApiBenchmark.cs: Defines the benchmark scenarios for Minimal APIs and Controllers.
  • Program.cs: Configures the endpoints for both Minimal APIs and Controllers.
  • Models/MyData.cs: Data model used for POST benchmarks.

Benchmark Scenarios

Hello Endpoints (GET):

  • Minimal API: /minimalapi/hello
  • Controller: /api/test/hello
  • Purpose: Compare the performance of lightweight, static responses.

Data Endpoints (POST):

  • Minimal API: /minimalapi/data
  • Controller: /api/test/data
  • Purpose: Benchmark JSON payload processing (serialization/deserialization).

Observations

Performance:

  • Minimal APIs generally outperform Controllers in lightweight operations due to reduced overhead.
  • The performance gap narrows as complexity increases.

Memory Allocation:

  • Memory usage for Minimal APIs and Controllers is comparable, especially for smaller payloads.

Use Cases:

  • Minimal APIs are ideal for microservices and high-performance scenarios.
  • Controllers offer flexibility for larger applications requiring features like model validation, filters, and middleware.

Future Work

To make the benchmarks more comprehensive and reflect real-world use cases, the following enhancements and tests are planned:

Database Queries:

  • Benchmark endpoints that interact with a database using popular ORMs like Entity Framework Core or lightweight alternatives like Dapper.
  • Test read-heavy vs write-heavy workloads (e.g., fetching records vs inserting/updating records).
  • Compare database performance with in-memory operations for Minimal APIs and Controllers.

Validation:

  • Incorporate validation frameworks like FluentValidation to evaluate how validation impacts performance.
  • Implement Data Annotations for basic validation scenarios and compare its efficiency against FluentValidation.
  • Use Middleware-based validation to ensure centralized request validation before reaching the API endpoints.
  • Test complex validation rules, such as nested objects or custom rules, for both Minimal APIs and Controllers.

Authentication & Authorization:

  • Benchmark performance with JWT-based Authentication or OAuth2.
  • Compare the impact of applying policies or role-based authorization on Minimal APIs vs Controllers.
  • Measure how authentication impacts latency under load.

CQRS Pattern:

  • Implement a Command-Query Responsibility Segregation (CQRS) pattern.
  • Benchmark read operations using Queries and write operations using Commands.
  • Compare the use of MediatR or custom implementations of CQRS in Minimal APIs and Controllers.

Middleware Impact:

  • Test the effect of custom middleware (e.g., request logging, caching) on API performance.
  • Measure the overhead introduced by common middleware components (e.g., CORS, authentication).

Asynchronous Operations:

  • Benchmark endpoints with heavy asynchronous tasks, such as third-party API calls or file processing.
  • Compare concurrency handling between Minimal APIs and Controllers.

Serialization/Deserialization:

  • Evaluate the performance of various JSON serializers, including System.Text.Json, Newtonsoft.Json, and Utf8Json.
  • Test performance for large and nested objects in both Minimal APIs and Controllers.

Error Handling:

  • Measure the performance impact of global exception handling middleware.
  • Test scenarios where detailed error responses are returned, comparing Minimal APIs with Controllers.

Load Testing:

  • Perform load tests using tools like K6 or Apache JMeter to simulate concurrent user requests.
  • Measure scalability and latency under high load conditions.

Advanced Caching:

  • Test caching mechanisms (e.g., in-memory cache, distributed cache) in endpoints for repeated data access.
  • Compare performance when caching is applied in Minimal APIs vs Controllers.

References

Acknowledgments

Special thanks to Anton Martyniuk for the inspiring article on API performance in .NET. How to Increase Performance of Web APIs in .NET

Top comments (2)

Collapse
 
stevsharp profile image
Spyros Ponaris

Thanks for your comment!
To be honest, I still prefer controllers and haven’t completely adapted to minimal APIs yet. Minimal APIs are faster and definitely worth exploring further.

Collapse
 
canro91 profile image
Cesar Aguirre

I still don't get used to syntax of minimal APIs on "real" projects, but I found it really useful when writing posts. Usually the whole example/code can fit in a single (coherent) file.