Skip to content

Commit 2068ce6

Browse files
committed
Use PooledContextFactory
Also needs aspnet/HttpAbstractions#501 to be real
1 parent e742afb commit 2068ce6

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Benchmarks/DebugInfoPageMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public async Task Invoke(HttpContext httpContext)
4848
await httpContext.Response.WriteAsync($"<li>Configuration: {_configurationName}</li>");
4949
await httpContext.Response.WriteAsync($"<li>Server: {_hostingEnv.Configuration["server"]}</li>");
5050
await httpContext.Response.WriteAsync($"<li>Server URLs: {_hostingEnv.Configuration["server.urls"]}</li>");
51-
await httpContext.Response.WriteAsync($"<li>Supports Send File: {httpContext.Response.SupportsSendFile()}</li>");
5251

5352
await httpContext.Response.WriteAsync($"<li>Server features:<ul>");
5453

src/Benchmarks/Startup.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
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;
56
using System.Data.Common;
67
using System.Data.SqlClient;
78
using Benchmarks.Data;
89
using Microsoft.AspNet.Builder;
910
using Microsoft.AspNet.Hosting;
1011
using Microsoft.AspNet.Http;
12+
using Microsoft.AspNet.Http.Features;
13+
using Microsoft.AspNet.Http.Internal;
1114
using Microsoft.Data.Entity;
1215
using Microsoft.Extensions.Configuration;
1316
using Microsoft.Extensions.DependencyInjection;
@@ -44,6 +47,7 @@ public void ConfigureServices(IServiceCollection services)
4447
// No scenarios covered by the benchmarks require the HttpContextAccessor so we're replacing it with a
4548
// no-op version to avoid the cost.
4649
services.AddSingleton(typeof(IHttpContextAccessor), typeof(InertHttpContextAccessor));
50+
services.AddSingleton(typeof(IHttpContextFactory), typeof(PooledContextFactory));
4751

4852
if (StartupOptions.EnableDbTests)
4953
{
@@ -121,5 +125,44 @@ public class Options
121125

122126
public string ConnectionString { get; set; }
123127
}
128+
129+
public class PooledContextFactory : IHttpContextFactory
130+
{
131+
[ThreadStatic]
132+
Queue<HttpContext> _contextPool;
133+
134+
private Queue<HttpContext> ContextPool
135+
{
136+
get
137+
{
138+
if (_contextPool == null)
139+
{
140+
_contextPool = new Queue<HttpContext>(16);
141+
}
142+
return _contextPool;
143+
}
144+
}
145+
146+
public HttpContext Create(IFeatureCollection featureCollection)
147+
{
148+
if (ContextPool.Count > 0)
149+
{
150+
var context = ContextPool.Dequeue();
151+
// Needs https://github.com/aspnet/HttpAbstractions/pull/501
152+
// context.ReplaceFeatures(featureCollection);
153+
return context;
154+
}
155+
156+
return new DefaultHttpContext(featureCollection);
157+
}
158+
159+
public void Dispose(HttpContext httpContext)
160+
{
161+
if (ContextPool.Count < 16)
162+
{
163+
ContextPool.Enqueue(httpContext);
164+
}
165+
}
166+
}
124167
}
125168
}

0 commit comments

Comments
 (0)