Răsfoiți Sursa

CSharp SpanJson Middleware (#3909)

* added span json library tests as a Middleware/SpanJsonMiddleware.cs

* docker configuration

* typo
Dmitry Kushnir 7 ani în urmă
părinte
comite
fad78b5e64

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

@@ -17,6 +17,7 @@
     <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="SpanJson" Version="1.0.7" />
     <PackageReference Include="Utf8Json" Version="1.3.7" />
   </ItemGroup>
 </Project>

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

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

+ 45 - 0
frameworks/CSharp/aspnetcore/Benchmarks/Middleware/SpanJsonMiddleware.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Threading.Tasks;
+using Benchmarks.Configuration;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Http;
+
+namespace Benchmarks.Middleware
+{
+    public class SpanJsonMiddleware
+    {
+        private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.SpanJson));
+        private const int _bufferSize = 27;
+
+        private readonly RequestDelegate _next;
+
+        public SpanJsonMiddleware(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!" };
+                var result = SpanJson.JsonSerializer.Generic.Utf8.SerializeAsync(msg, httpContext.Response.Body);
+                return result.IsCompleted ? Task.CompletedTask : result.AsTask();
+            }
+
+            return _next(httpContext);
+        }
+    }
+
+    public static class SpanJsonMiddlewareExtensions
+    {
+        public static IApplicationBuilder UseSpanJson(this IApplicationBuilder builder)
+        {
+            return builder.UseMiddleware<SpanJsonMiddleware>();
+        }
+    }
+}

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

@@ -143,6 +143,11 @@ namespace Benchmarks
                 app.UseUtf8Json();
             }
 
+            if (Scenarios.SpanJson)
+            {
+                app.UseSpanJson();
+            }
+
             // Single query endpoints
             if (Scenarios.DbSingleQueryRaw)
             {

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

@@ -31,6 +31,7 @@ This includes tests for plaintext and json serialization.
 * [JSON Serialization](Benchmarks/Middleware/JsonMiddleware.cs): "/json"
 * [JSON Serialization MVC](Benchmarks/Controllers/HomeController.cs): "/mvc/json"
 * [JSON Serialization Utf8Json](Benchmarks/Middleware/Utf8JsonMiddleware.cs): "/utf8json"
+* [JSON Serialization SpanJson](Benchmarks/Middleware/SpanJsonMiddleware.cs): "/spanjson"
 * [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-spanjson.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=SpanJson"]

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

@@ -97,6 +97,24 @@
       "notes": "",
       "versus": "aspcore"
     },
+    "mw-spanjson": {
+      "json_url": "/spanjson",
+      "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, SpanJson",
+      "notes": "",
+      "versus": "aspcore"
+    },
     "mw-ado-pg": {
       "db_url": "/db/raw",
       "query_url": "/queries/raw?queries=",