<commit_before>---
layout: post
title: ASP.NET Core Performance Tests
---

How to execute performance tests for __ASP.NET Core__ with __wrk__.

### Create .NET Core Web App

Create new web app
```sh
$ dotnet new web
```

Replace code in `Program.cs`

 ```cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace AspNetCoreBenchmark
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
             .UseKestrel()
             .UseStartup<Startup>()
             .Build();

            host.Run();
        }
    }

    public class Startup
    {
        private static string response = "Hello World!";

        public void Configure(IApplicationBuilder app)
        {
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync(response);
            });
        }
    }
}
```

### wrk - HTTP Benchmark Tool

Install [guide](https://github.com/wg/wrk/wiki/Installing-Wrk-on-Linux) for Linux. On Windows 10 use Bash on Ubuntu on Windows.

### Execute Performance Tests

Start the server.

```sh
$ dotnet run
```

If you run wrk from a separate computer, which is recommended, change url to bind to `*`, so server listens requests on any IP address. 

```sh
$ dotnet run --urls http://*:5000
```

Run benchmark with wrk. This run is using 256 connections across 32 client threads for a duration of 10 seconds.

```sh
$ wrk -c 256 -t 32 -d 10 http://localhost:5000
```

## Results

Specs: Dell XPS 13, i7-6560U, 16GB, wrk at the same computer

Version|Req/sec
---|---:
netcoreapp2.0|29119.66
netcoreapp1.1|no difference

```
Running 10s test @ http://localhost:5000
  32 threads and 256 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     8.24ms    4.49ms  78.99ms   81.82%
    Req/Sec     0.93k   384.86     5.70k    91.43%
  293768 requests in 10.09s, 34.46MB read
Requests/sec:  29119.66
Transfer/sec:      3.42MB
```

## Source

GitHub: [aspnet-core-benchmark](https://github.com/ttu/aspnet-core-benchmark)<commit_msg>Add post request link to benchmark post<commit_after>---
layout: post
title: ASP.NET Core Performance Tests
---

How to execute performance tests for __ASP.NET Core__ with __wrk__.

### Create .NET Core Web App

Create new web app
```sh
$ dotnet new web
```

Replace code in `Program.cs`

 ```cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace AspNetCoreBenchmark
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
             .UseKestrel()
             .UseStartup<Startup>()
             .Build();

            host.Run();
        }
    }

    public class Startup
    {
        private static string response = "Hello World!";

        public void Configure(IApplicationBuilder app)
        {
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync(response);
            });
        }
    }
}
```

### wrk - HTTP Benchmark Tool

Install [guide](https://github.com/wg/wrk/wiki/Installing-Wrk-on-Linux) for Linux. On Windows 10 use Bash on Ubuntu on Windows.

### Execute Performance Tests

Start the server.

```sh
$ dotnet run
```

If you run wrk from a separate computer, which is recommended, change url to bind to `*`, so server listens requests on any IP address. 

```sh
$ dotnet run --urls http://*:5000
```

Run benchmark with wrk. This run is using 256 connections across 32 client threads for a duration of 10 seconds.

```sh
$ wrk -c 256 -t 32 -d 10 http://localhost:5000
```

### Results

Specs: Dell XPS 13, i7-6560U, 16GB. ASP.NET Core server and wrk running in the same computer

Version|Req/sec
---|---:
netcoreapp2.0|29119.66
netcoreapp1.1|no difference

```
Running 10s test @ http://localhost:5000
  32 threads and 256 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     8.24ms    4.49ms  78.99ms   81.82%
    Req/Sec     0.93k   384.86     5.70k    91.43%
  293768 requests in 10.09s, 34.46MB read
Requests/sec:  29119.66
Transfer/sec:      3.42MB
```

## Execute benchmark for POST request with payload

More complex benchamrks need to be defined with _lua_-scripts. Check a short example how to execute POST request benchmark from the [.NET Fake JSON Server's documentation](https://github.com/ttu/dotnet-fake-json-server/blob/master/BenchmarkWrk.md).

## Source

Example benchamrking source code with with 3 different implementations (minimal, route with map, controller). GitHub repo: [aspnet-core-benchmark](https://github.com/ttu/aspnet-core-benchmark)
{'fraction_non_alphanumeric': 0.10780590717299578, 'fraction_numerical': 0.04578059071729958, 'mean_word_length': 2.6025835866261398, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2647332', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1448, 'n_words': 488}