Skip to content

Instantly share code, notes, and snippets.

@iSeiryu
Last active December 14, 2024 22:38
Show Gist options
  • Save iSeiryu/c1b95242af000a11ea4710b79c2d6a53 to your computer and use it in GitHub Desktop.
Save iSeiryu/c1b95242af000a11ea4710b79c2d6a53 to your computer and use it in GitHub Desktop.
Simple get and post endpoints with C# vs NodeJS vs Rust

Program.cs

using System.Text.Json.Serialization;

var app = WebApplication.CreateBuilder(args).Build();

app.MapGet("/hi", () => "hi");
app.MapPost("send-money", (SendMoneyRequest request) =>
{
    var receipt = new Receipt($"{request.From.FirstName} {request.From.LastName}",
                              $"{request.To.FirstName} {request.To.LastName}",
                              request.Amount,
                              DateTime.Now,
                              $"{request.To.Address.Street}, {request.To.Address.City}, {request.To.Address.State}, {request.To.Address.Zip}");
    return receipt;
});

app.Run();



record AccountHolder(Guid Id, string FirstName, string LastName, Address Address, string Email);
record Address(string Street, string City, [property: JsonConverter(typeof(JsonStringEnumConverter))] State State, string Zip);
record SendMoneyRequest(AccountHolder From, AccountHolder To, decimal Amount, DateTime SendOn);
record Receipt(string FromAccount, string ToAccount, decimal Amount, DateTime CreatedOn, string ToAddress);
enum State { CA, NY, WA, TX, FL, IL, PA, OH, GA, MI, NC, NJ, VA }

myapp.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

Benchmark via hey

hey -n 200000 -c 100 -m POST -H 'Content-Type: application/json' -d '{
  "from": {
      "id": "b1b9b3b1-3b1b-3b1b-3b1b-3b1b3b1b3b1b",
      "firstName": "John",
      "lastName": "Doe",
      "address": {
          "street": "123 Main St",
          "city": "Anytown",
          "state": "CA",
          "zip": "12345"
      },
      "email": "[email protected]"
  },
  "to": {
      "id": "7eb53909-8977-4a7d-8e91-f1bfcfe812e2",
      "firstName": "Jane",
      "lastName": "Doe",
      "address": {
          "street": "456 Elm St",
          "city": "Anytown",
          "state": "FL",
          "zip": "12345"
      },
      "email": "[email protected]"
  },
  "amount": 30.14,
  "sendOn": "2024-06-01T12:00:00"
}' http://localhost:5000/send-money
@iSeiryu
Copy link
Author

iSeiryu commented Jul 10, 2024

wrk is a lot faster than hey.
https://github.com/wg/wrk/

./wrk -t1 -c1 -d1s http://localhost:5000/send-money -s scripts/post.lua

scripts/post.lua

-- example HTTP POST script which demonstrates setting the
-- HTTP method, body, and adding a header

wrk.method = "POST"
wrk.body   = [[
    {
    "from": {
        "id": "b1b9b3b1-3b1b-3b1b-3b1b-3b1b3b1b3b1b",
        "firstName": "John",
        "lastName": "Doe",
        "address": {
            "street": "123 Main St",
            "city": "Anytown",
            "state": "CA",
            "zip": "12345"
        },
        "email": "[email protected]"
    },
    "to": {
        "id": "7eb53909-8977-4a7d-8e91-f1bfcfe812e2",
        "firstName": "Jane",
        "lastName": "Doe",
        "address": {
            "street": "456 Elm St",
            "city": "Anytown",
            "state": "FL",
            "zip": "12345"
        },
        "email": "[email protected]"
    },
    "amount": 30.14,
    "sendOn": "2024-06-01T12:00:00"
  }
  ]]
wrk.headers["Content-Type"] = "application/json"

function response(status, headers, body)
    --print(status)
    --print(body)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment