浏览代码

Add "ASP.NET Core, Middleware, Utf8Json" (#3781)

* Add "ASP.NET Core, Middleware, Utf8Json"

* Remove plaintext_url from "CSharp\aspnetcore\benchmark_config.json"

* Rename dockerfile

* Fix Utf8JsonMiddleware path

* Update README.md
Kiyoaki Tsurutani 7 年之前
父节点
当前提交
280021b57d

+ 1 - 0
frameworks/CSharp/aspnetcore/Benchmarks/Benchmarks.csproj

@@ -17,5 +17,6 @@
     <PackageReference Include="MySqlConnector" Version="0.40.3" />
     <PackageReference Include="Npgsql" Version="4.0.0-rc1" />
     <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.0-rc1" />
+    <PackageReference Include="Utf8Json" Version="1.3.7" />
   </ItemGroup>
 </Project>

+ 3 - 0
frameworks/CSharp/aspnetcore/Benchmarks/Configuration/Scenarios.cs

@@ -23,6 +23,9 @@ namespace Benchmarks.Configuration
         [ScenarioPath("/json")]
         public bool Json { get; set; }
 
+        [ScenarioPath("/utf8json")]
+        public bool Utf8Json { get; set; }
+
         [ScenarioPath("/128B.txt", "/512B.txt", "/1KB.txt", "/4KB.txt", "/16KB.txt", "/512KB.txt", "/1MB.txt", "/5MB.txt")]
         public bool StaticFiles { get; set; }
 

+ 52 - 0
frameworks/CSharp/aspnetcore/Benchmarks/Middleware/Utf8JsonMiddleware.cs

@@ -0,0 +1,52 @@
+using Benchmarks.Configuration;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Http;
+using System;
+using System.Threading.Tasks;
+using Utf8Json;
+
+namespace Benchmarks.Middleware
+{
+    public struct JsonMessage
+    {
+        public string message;
+    }
+
+    public class Utf8JsonMiddleware
+    {
+        private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.Utf8Json));
+        private const int _bufferSize = 27;
+
+        private readonly RequestDelegate _next;
+
+        public Utf8JsonMiddleware(RequestDelegate next)
+        {
+            _next = next;
+        }
+
+        public Task Invoke(HttpContext httpContext)
+        {
+            if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
+            {
+                httpContext.Response.StatusCode = 200;
+                httpContext.Response.ContentType = "application/json";
+                httpContext.Response.ContentLength = _bufferSize;
+
+                var msg = new JsonMessage { message = "Hello, World!" };
+                JsonSerializer.Serialize(httpContext.Response.Body, msg);
+
+                return Task.CompletedTask;
+            }
+
+            return _next(httpContext);
+        }
+    }
+
+    public static class Utf8JsonMiddlewareExtensions
+    {
+        public static IApplicationBuilder UseUtf8Json(this IApplicationBuilder builder)
+        {
+            return builder.UseMiddleware<Utf8JsonMiddleware>();
+        }
+    }
+}

+ 5 - 0
frameworks/CSharp/aspnetcore/Benchmarks/Startup.cs

@@ -133,6 +133,11 @@ namespace Benchmarks
                 app.UseJson();
             }
 
+            if (Scenarios.Utf8Json)
+            {
+                app.UseUtf8Json();
+            }
+
             // Single query endpoints
             if (Scenarios.DbSingleQueryRaw)
             {

+ 1 - 0
frameworks/CSharp/aspnetcore/README.md

@@ -30,6 +30,7 @@ This includes tests for plaintext and json serialization.
 * [Plaintext MVC](Benchmarks/Controllers/HomeController.cs): "/mvc/plaintext"
 * [JSON Serialization](Benchmarks/Middleware/JsonMiddleware.cs): "/json"
 * [JSON Serialization MVC](Benchmarks/Controllers/HomeController.cs): "/mvc/json"
+* [JSON Serialization Utf8Json](Benchmarks/Middleware/Utf8JsonMiddleware.cs): "/utf8json"
 * [Single Query Raw](Benchmarks/Middleware/SingleQueryRawMiddleware.cs): "/db/raw"
 * [Single Query EF](Benchmarks/Middleware/SingleQueryEfMiddleware.cs): "/db/ef"
 * [Single Query Dapper](Benchmarks/Middleware/SingleQueryDapperMiddleware.cs): "/db/dapper"

+ 12 - 0
frameworks/CSharp/aspnetcore/aspcore-mw-utf8json.dockerfile

@@ -0,0 +1,12 @@
+FROM microsoft/dotnet:2.1-sdk-stretch AS build
+WORKDIR /app
+COPY Benchmarks .
+RUN dotnet publish -c Release -o out
+
+FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
+ENV ASPNETCORE_URLS http://+:8080
+ENV COMPlus_ReadyToRun 0
+WORKDIR /app
+COPY --from=build /app/out ./
+
+ENTRYPOINT ["dotnet", "Benchmarks.dll", "scenarios=Utf8Json"]

+ 18 - 0
frameworks/CSharp/aspnetcore/benchmark_config.json

@@ -77,6 +77,24 @@
       "notes": "",
       "versus": "aspcore"
     },
+    "mw-utf8json": {
+      "json_url": "/utf8json",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "None",
+      "framework": "ASP.NET Core",
+      "language": "C#",
+      "orm": "Raw",
+      "platform": ".NET",
+      "flavor": "CoreCLR",
+      "webserver": "Kestrel",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "ASP.NET Core, Middleware, Utf8Json",
+      "notes": "",
+      "versus": "aspcore"
+    },
     "mw-ado-pg": {
       "db_url": "/db/raw",
       "query_url": "/queries/raw?queries=",