diff --git a/src/Benchmarks/JsonJilMiddleware.cs b/src/Benchmarks/JsonJilMiddleware.cs new file mode 100644 index 000000000..34ec944b9 --- /dev/null +++ b/src/Benchmarks/JsonJilMiddleware.cs @@ -0,0 +1,55 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Http; +using Jil; + +namespace Benchmarks +{ + public class JsonJilMiddleware + { + private static readonly Task _done = Task.FromResult(0); + private static readonly PathString _path = new PathString("/json/jil"); + + private readonly RequestDelegate _next; + + public JsonJilMiddleware(RequestDelegate next) + { + _next = next; + } + + public Task Invoke(HttpContext httpContext) + { + // We check Ordinal explicitly first because it's faster than OrdinalIgnoreCase + if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal) || + httpContext.Request.Path.StartsWithSegments(_path, StringComparison.OrdinalIgnoreCase)) + { + httpContext.Response.StatusCode = 200; + httpContext.Response.ContentType = "application/json"; + httpContext.Response.ContentLength = 30; + + using (var sw = new StreamWriter(httpContext.Response.Body, Encoding.UTF8, bufferSize: 30)) + { + JSON.Serialize(new { message = "Hello, World!" }, sw); + } + + return _done; + } + + return _next(httpContext); + } + } + + public static class JsonJilMiddlewareExtensions + { + public static IApplicationBuilder UseJilJson(this IApplicationBuilder builder) + { + return builder.UseMiddleware(); + } + } +} diff --git a/src/Benchmarks/JsonMiddleware.cs b/src/Benchmarks/JsonNewtonsoftMiddleware.cs similarity index 81% rename from src/Benchmarks/JsonMiddleware.cs rename to src/Benchmarks/JsonNewtonsoftMiddleware.cs index dfa24a9de..a76d09a8d 100644 --- a/src/Benchmarks/JsonMiddleware.cs +++ b/src/Benchmarks/JsonNewtonsoftMiddleware.cs @@ -11,15 +11,15 @@ namespace Benchmarks { - public class JsonMiddleware + public class JsonNewtonsoftMiddleware { private static readonly Task _done = Task.FromResult(0); - private static readonly PathString _path = new PathString("/json"); + private static readonly PathString _path = new PathString("/json/newtonsoft"); private static readonly JsonSerializer _json = new JsonSerializer(); private readonly RequestDelegate _next; - public JsonMiddleware(RequestDelegate next) + public JsonNewtonsoftMiddleware(RequestDelegate next) { _next = next; } @@ -46,11 +46,11 @@ public Task Invoke(HttpContext httpContext) } } - public static class JsonMiddlewareExtensions + public static class JsonNewtonsoftMiddlewareExtensions { - public static IApplicationBuilder UseJson(this IApplicationBuilder builder) + public static IApplicationBuilder UseNewtonsoftJson(this IApplicationBuilder builder) { - return builder.UseMiddleware(); + return builder.UseMiddleware(); } } } diff --git a/src/Benchmarks/Startup.cs b/src/Benchmarks/Startup.cs index aa2f288bc..e8eb9e564 100644 --- a/src/Benchmarks/Startup.cs +++ b/src/Benchmarks/Startup.cs @@ -66,7 +66,8 @@ public void Configure(IApplicationBuilder app) { app.UseErrorHandler(); app.UsePlainText(); - app.UseJson(); + app.UseNewtonsoftJson(); + app.UseJilJson(); if (StartupOptions.EnableDbTests) { diff --git a/src/Benchmarks/project.json b/src/Benchmarks/project.json index 7f2664858..db4d647ff 100644 --- a/src/Benchmarks/project.json +++ b/src/Benchmarks/project.json @@ -15,7 +15,8 @@ "Microsoft.AspNet.Server.WebListener": "1.0.0-*", "Microsoft.AspNet.StaticFiles": "1.0.0-*", "Microsoft.Extensions.WebEncoders": "1.0.0-*", - "Newtonsoft.Json": "7.0.1" + "Newtonsoft.Json": "7.0.1", + "Jil": "2.13.0-*" }, "commands": {