Skip to content

Commit 47b2e0f

Browse files
authored
Fix slices usage in middleware benchmarks (#2039)
1 parent 2705be0 commit 47b2e0f

File tree

5 files changed

+10
-23
lines changed

5 files changed

+10
-23
lines changed

src/Benchmarks/Middleware/FortunesDapperMiddleware.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Text.Encodings.Web;
76
using System.Threading.Tasks;
87
using Benchmarks.Configuration;
98
using Benchmarks.Data;
9+
using Benchmarks.Templates;
1010
using Microsoft.AspNetCore.Builder;
1111
using Microsoft.AspNetCore.Http;
1212
using Microsoft.Extensions.DependencyInjection;
13-
using RazorSlices;
1413

1514
namespace Benchmarks.Middleware
1615
{
@@ -20,13 +19,11 @@ public class FortunesDapperMiddleware
2019

2120
private readonly RequestDelegate _next;
2221
private readonly HtmlEncoder _htmlEncoder;
23-
private readonly SliceFactory<IEnumerable<FortuneUtf16>> _fortunesFactory;
2422

2523
public FortunesDapperMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
2624
{
2725
_next = next;
2826
_htmlEncoder = htmlEncoder;
29-
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<FortuneUtf16>>("/Templates/FortunesUtf16.cshtml");
3027
}
3128

3229
public async Task Invoke(HttpContext httpContext)
@@ -35,8 +32,9 @@ public async Task Invoke(HttpContext httpContext)
3532
{
3633
var db = httpContext.RequestServices.GetService<DapperDb>();
3734
var rows = await db.LoadFortunesRows();
35+
3836

39-
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
37+
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, FortunesUtf16.Create);
4038

4139
return;
4240
}

src/Benchmarks/Middleware/FortunesEfMiddleware.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Text.Encodings.Web;
76
using System.Threading.Tasks;
87
using Benchmarks.Configuration;
98
using Benchmarks.Data;
109
using Microsoft.AspNetCore.Builder;
1110
using Microsoft.AspNetCore.Http;
1211
using Microsoft.Extensions.DependencyInjection;
13-
using RazorSlices;
1412

1513
namespace Benchmarks.Middleware
1614
{
@@ -20,13 +18,11 @@ public class FortunesEfMiddleware
2018

2119
private readonly RequestDelegate _next;
2220
private readonly HtmlEncoder _htmlEncoder;
23-
private readonly SliceFactory<IEnumerable<Fortune>> _fortunesFactory;
2421

2522
public FortunesEfMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
2623
{
2724
_next = next;
2825
_htmlEncoder = htmlEncoder;
29-
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<Fortune>>("/Templates/Fortunes.cshtml");
3026
}
3127

3228
public async Task Invoke(HttpContext httpContext)
@@ -36,7 +32,7 @@ public async Task Invoke(HttpContext httpContext)
3632
var db = httpContext.RequestServices.GetService<EfDb>();
3733
var rows = await db.LoadFortunesRows();
3834

39-
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
35+
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, Templates.Fortunes.Create);
4036

4137
return;
4238
}

src/Benchmarks/Middleware/FortunesRawMiddleware.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Text.Encodings.Web;
76
using System.Threading.Tasks;
87
using Benchmarks.Configuration;
98
using Benchmarks.Data;
109
using Microsoft.AspNetCore.Builder;
1110
using Microsoft.AspNetCore.Http;
1211
using Microsoft.Extensions.DependencyInjection;
13-
using RazorSlices;
1412

1513
namespace Benchmarks.Middleware
1614
{
@@ -20,13 +18,11 @@ public class FortunesRawMiddleware
2018

2119
private readonly RequestDelegate _next;
2220
private readonly HtmlEncoder _htmlEncoder;
23-
private readonly SliceFactory<IEnumerable<Fortune>> _fortunesFactory;
2421

2522
public FortunesRawMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
2623
{
2724
_next = next;
2825
_htmlEncoder = htmlEncoder;
29-
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<Fortune>>("/Templates/Fortunes.cshtml");
3026
}
3127

3228
public async Task Invoke(HttpContext httpContext)
@@ -36,7 +32,7 @@ public async Task Invoke(HttpContext httpContext)
3632
var db = httpContext.RequestServices.GetService<RawDb>();
3733
var rows = await db.LoadFortunesRows();
3834

39-
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
35+
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, Templates.Fortunes.Create);
4036

4137
return;
4238
}

src/Benchmarks/Middleware/FortunesRawSyncMiddleware.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Text.Encodings.Web;
76
using System.Threading.Tasks;
87
using Benchmarks.Configuration;
98
using Benchmarks.Data;
109
using Microsoft.AspNetCore.Builder;
1110
using Microsoft.AspNetCore.Http;
1211
using Microsoft.Extensions.DependencyInjection;
13-
using RazorSlices;
1412

1513
namespace Benchmarks.Middleware
1614
{
@@ -20,13 +18,11 @@ public class FortunesRawSyncMiddleware
2018

2119
private readonly RequestDelegate _next;
2220
private readonly HtmlEncoder _htmlEncoder;
23-
private readonly SliceFactory<IEnumerable<Fortune>> _fortunesFactory;
2421

2522
public FortunesRawSyncMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
2623
{
2724
_next = next;
2825
_htmlEncoder = htmlEncoder;
29-
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<Fortune>>("/Templates/Fortunes.cshtml");
3026
}
3127

3228
public Task Invoke(HttpContext httpContext)
@@ -36,7 +32,7 @@ public Task Invoke(HttpContext httpContext)
3632
var db = httpContext.RequestServices.GetService<RawDb>();
3733
var rows = db.LoadFortunesRowsSync();
3834

39-
return MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
35+
return MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, Templates.Fortunes.Create);
4036
}
4137

4238
return _next(httpContext);

src/Benchmarks/Middleware/MiddlewareHelpers.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Text.Encodings.Web;
67
using System.Threading.Tasks;
@@ -30,13 +31,13 @@ public static int GetMultipleQueriesQueryCount(HttpContext httpContext)
3031
}
3132

3233
public static async Task RenderFortunesHtml<T>(IEnumerable<T> model, HttpContext httpContext,
33-
HtmlEncoder htmlEncoder, SliceFactory<IEnumerable<T>> fortunesFactory)
34+
HtmlEncoder htmlEncoder, Func<IEnumerable<T>, RazorSlice> templateFactory)
3435
{
3536
httpContext.Response.StatusCode = StatusCodes.Status200OK;
3637
httpContext.Response.ContentType = "text/html; charset=UTF-8";
3738

38-
using var template = fortunesFactory(model);
39-
await template.RenderToPipeWriterAsync(httpContext.Response.BodyWriter, htmlEncoder);
39+
using var template = templateFactory(model);
40+
await template.RenderAsync(httpContext.Response.BodyWriter, htmlEncoder);
4041
await httpContext.Response.BodyWriter.FlushAsync();
4142
}
4243
}

0 commit comments

Comments
 (0)