-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
069920e
commit b37d4fb
Showing
17 changed files
with
386 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear/> | ||
<add key="dotnet.myget.org" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" /> | ||
<add key="AspNetCore" value="https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json" /> | ||
<add key="AspNetCoreTools" value="https://dotnet.myget.org/F/aspnetcore-tools/api/v3/index.json" /> | ||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" /> | ||
<add key="roslyn" value="https://dotnet.myget.org/F/roslyn/api/v3/index.json" /> | ||
</packageSources> | ||
</configuration> |
19 changes: 19 additions & 0 deletions
19
samples/Beeline.AspNetCoreBenchmarks/Beeline.AspNetCoreBenchmarks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<RuntimeFrameworkVersion>2.1.0-preview1-26103-03</RuntimeFrameworkVersion> | ||
<NETStandardImplicitPackageVersion>2.1.0-preview1-26103-03</NETStandardImplicitPackageVersion> | ||
<LangVersion>7.2</LangVersion> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<MicrosoftNETPlatformLibrary>Microsoft.NETCore.App</MicrosoftNETPlatformLibrary> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" /> | ||
<PackageReference Include="Npgsql" Version="3.2.6" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Beeline\Beeline.csproj" /> | ||
</ItemGroup> | ||
</Project> |
7 changes: 7 additions & 0 deletions
7
samples/Beeline.AspNetCoreBenchmarks/Configuration/AppSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Beeline.AspNetCoreBenchmarks.Configuration | ||
{ | ||
public class AppSettings | ||
{ | ||
public string ConnectionString { get; set; } | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
samples/Beeline.AspNetCoreBenchmarks/Controllers/ValuesController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
public class ValuesController : Controller | ||
{ | ||
// GET api/values | ||
[HttpGet] | ||
public IEnumerable<string> Get() | ||
{ | ||
return new string[] { "value1", "value2" }; | ||
} | ||
|
||
// GET api/values/5 | ||
[HttpGet("{id}")] | ||
public string Get(int id) | ||
{ | ||
return "value"; | ||
} | ||
|
||
// POST api/values | ||
[HttpPost] | ||
public void Post([FromBody]string value) | ||
{ | ||
} | ||
|
||
// PUT api/values/5 | ||
[HttpPut("{id}")] | ||
public void Put(int id, [FromBody]string value) | ||
{ | ||
} | ||
|
||
// DELETE api/values/5 | ||
[HttpDelete("{id}")] | ||
public void Delete(int id) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Data; | ||
using System.Data.Common; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Beeline.AspNetCoreBenchmarks.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Data | ||
{ | ||
public class BeelineDb | ||
{ | ||
private readonly IRandom _random; | ||
private readonly DbProviderFactory _dbProvider; | ||
private readonly string _connectionString; | ||
private ObjectWriter _objectWriter; | ||
|
||
public BeelineDb(IRandom random, DbProviderFactory dbProvider, IOptions<AppSettings> appSettings) | ||
{ | ||
_random = random; | ||
_dbProvider = dbProvider; | ||
_connectionString = appSettings.Value.ConnectionString; | ||
} | ||
|
||
public async Task<int> LoadSingleQueryRow(byte[] buffer, CancellationToken ct = default) | ||
{ | ||
using (var db = _dbProvider.CreateConnection()) | ||
{ | ||
Debug.Assert(db != null); | ||
db.ConnectionString = _connectionString; | ||
using (var cmd = CreateSingleQueryCommand(db, _random.Next(1, 10001))) | ||
{ | ||
await db.OpenAsync(ct); | ||
using (var reader = await cmd.ExecuteReaderAsync(ct)) | ||
{ | ||
if (_objectWriter is null) | ||
{ | ||
_objectWriter = new ObjectWriter(RowSerializer.For(reader, true), 1024); | ||
} | ||
|
||
return await _objectWriter.WriteSingle(reader, buffer, ct); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static DbCommand CreateSingleQueryCommand(DbConnection db, int id) | ||
{ | ||
var cmd = db.CreateCommand(); | ||
cmd.CommandText = "SELECT id, randomnumber FROM world WHERE id = @id"; | ||
var idParameter = cmd.CreateParameter(); | ||
idParameter.ParameterName = "id"; | ||
idParameter.DbType = DbType.Int32; | ||
idParameter.Value = id; | ||
cmd.Parameters.Add(idParameter); | ||
return cmd; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
samples/Beeline.AspNetCoreBenchmarks/Data/DatabaseInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using Npgsql; | ||
using NpgsqlTypes; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Data | ||
{ | ||
public static class DatabaseInitializer | ||
{ | ||
private const string DropTable = "drop table if exists world"; | ||
private const string CreateTable = @"create table world | ||
( | ||
id integer not null | ||
constraint world_pkey | ||
primary key, | ||
randomnumber integer | ||
)"; | ||
|
||
private const string Insert = "INSERT INTO world (id, randomnumber) VALUES (@id, @randomnumber)"; | ||
|
||
public static void Initialize(string connectionString) | ||
{ | ||
var random = new Random(42); | ||
using (var connection = new NpgsqlConnection(connectionString)) | ||
using (var cmd = connection.CreateCommand()) | ||
{ | ||
connection.Open(); | ||
cmd.CommandText = DropTable; | ||
cmd.ExecuteNonQuery(); | ||
cmd.CommandText = CreateTable; | ||
cmd.ExecuteNonQuery(); | ||
cmd.CommandText = Insert; | ||
var @id = cmd.Parameters.Add("id", NpgsqlDbType.Integer); | ||
var @randomnumber = cmd.Parameters.Add("randomnumber", NpgsqlDbType.Integer); | ||
cmd.Prepare(); | ||
for (int i = 1; i < 10001; i++) | ||
{ | ||
@id.Value = i; | ||
@randomnumber.Value = random.Next(1, 999_999_999); | ||
cmd.ExecuteNonQuery(); | ||
} | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
samples/Beeline.AspNetCoreBenchmarks/Data/DefaultRandom.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Data | ||
{ | ||
public class DefaultRandom : IRandom | ||
{ | ||
private static int _nextSeed = 0; | ||
// Random isn't thread safe | ||
private static readonly ThreadLocal<Random> Random = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref _nextSeed))); | ||
|
||
public int Next(int minValue, int maxValue) | ||
{ | ||
return Random.Value.Next(minValue, maxValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Beeline.AspNetCoreBenchmarks.Data | ||
{ | ||
public interface IRandom | ||
{ | ||
int Next(int minValue, int maxValue); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
samples/Beeline.AspNetCoreBenchmarks/Middleware/SingleQueryBeelineMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Threading.Tasks; | ||
using Beeline.AspNetCoreBenchmarks.Data; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Middleware | ||
{ | ||
public class SingleQueryBeelineMiddleware | ||
{ | ||
private static readonly PathString Path = new PathString("/db/beeline"); | ||
|
||
private readonly RequestDelegate _next; | ||
|
||
public SingleQueryBeelineMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task Invoke(HttpContext httpContext) | ||
{ | ||
if (httpContext.Request.Path.StartsWithSegments(Path, StringComparison.Ordinal)) | ||
{ | ||
var db = httpContext.RequestServices.GetService<BeelineDb>(); | ||
var buffer = ArrayPool<byte>.Shared.Rent(128); | ||
try | ||
{ | ||
var length = await db.LoadSingleQueryRow(buffer, httpContext.RequestAborted); | ||
|
||
httpContext.Response.StatusCode = StatusCodes.Status200OK; | ||
httpContext.Response.ContentType = "application/json"; | ||
httpContext.Response.ContentLength = length; | ||
await httpContext.Response.Body.WriteAsync(buffer, 0, length, httpContext.RequestAborted); | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(buffer); | ||
} | ||
|
||
return; | ||
} | ||
|
||
await _next(httpContext); | ||
} | ||
} | ||
|
||
public static class SingleQueryDapperMiddlewareExtensions | ||
{ | ||
public static IApplicationBuilder UseSingleQueryBeeline(this IApplicationBuilder builder) | ||
{ | ||
return builder.UseMiddleware<SingleQueryBeelineMiddleware>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks | ||
{ | ||
[UsedImplicitly] | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
BuildWebHost(args).Run(); | ||
} | ||
|
||
private static IWebHost BuildWebHost(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>() | ||
.Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Beeline.AspNetCoreBenchmarks.Configuration; | ||
using Beeline.AspNetCoreBenchmarks.Data; | ||
using Beeline.AspNetCoreBenchmarks.Middleware; | ||
using JetBrains.Annotations; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Npgsql; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks | ||
{ | ||
[UsedImplicitly] | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
private IConfiguration Configuration { get; } | ||
|
||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
DatabaseInitializer.Initialize(Configuration.GetValue<string>("ConnectionString")); | ||
services.Configure<AppSettings>(Configuration); | ||
services.AddSingleton<IRandom, DefaultRandom>(); | ||
services.AddSingleton<DbProviderFactory>(NpgsqlFactory.Instance); | ||
services.AddSingleton<BeelineDb>(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
app.UseSingleQueryBeeline(); | ||
} | ||
} | ||
} |
Oops, something went wrong.