Skip to content

Instantly share code, notes, and snippets.

@tampajohn
Last active March 10, 2018 19:08
Show Gist options
  • Save tampajohn/8b2e9e0f9a03acb03ec79dbacc805de0 to your computer and use it in GitHub Desktop.
Save tampajohn/8b2e9e0f9a03acb03ec79dbacc805de0 to your computer and use it in GitHub Desktop.
Go vs Kestrel
dotnet publish -o ./dotnet-test/ --runtime osx-x64 --self-contained
./dotnet-test/benchmarking
CGO_ENABLED=0 GOOS=darwin go build -a -ldflags '-extldflags "-static"' -o go-test/bench-go
./go-test/bench-go
package main
import (
"fmt"
"log"
"time"
"math/rand"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
func randomString(n int) string {
b := make([]byte, n)
r := rand.New(rand.NewSource(time.Now().Unix()))
l := len(letterBytes)
for i := range b {
b[i] = letterBytes[r.Intn(l)]
}
return string(b)
}
func main() {
router := fasthttprouter.New()
router.GET("/api/values/:id", func(ctx *fasthttp.RequestCtx) {
ctx.WriteString(randomString(1024))
})
addr := "localhost:5001"
fmt.Println("Go Benchmark listening on", addr)
log.Fatal(fasthttp.ListenAndServe(addr, router.Handler))
}
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore;
namespace netcore
{
public class Startup
{
public static void Main(string[] args)
{
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build()
.Run();
}
public Startup()
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
}
const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
static string RandomString(int length)
{
var random = new Random(DateTime.Now.Millisecond);
var bytes = new Char[length];
for (var i = 0; i < length; i++)
{
bytes[i] = chars[random.Next(chars.Length)];
}
return new string(bytes);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var routeBuilder = new RouteBuilder(app);
//var txt = new string('n', 10000);
routeBuilder.MapGet("api/values/{id?}", context =>
{
var txt = RandomString(1024);
return context.Response.WriteAsync(txt);
});
var routes = routeBuilder.Build();
app.UseRouter(routes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment