Skip to content

Bring over a few changes from the TechEmpower repo #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="$(CoreFxVersion)" />
<PackageReference Include="Dapper" Version="$(DapperVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.ResponseCaching" Version="$(AspNetCoreVersion)" />
Expand All @@ -17,7 +16,6 @@
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why this can/should be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… because databases are automatically created in all cases. No need for dotnet ef database update, migration management, et cetera.

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="$(AspNetCoreVersion)" />
Expand All @@ -31,10 +29,6 @@
-->
<PackageReference Include="Npgsql" Version="$(NpgsqlVersion)" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(NpgsqlEntityFrameworkCorePostgreSQLVersion)" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="$(CoreFxVersion)" />
</ItemGroup>

<ItemGroup>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why this can/should be removed?

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.Dotnet" Version="$(AspNetCoreVersion)" />
</ItemGroup>

</Project>
6 changes: 2 additions & 4 deletions src/Benchmarks/Configuration/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// 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 Microsoft.Extensions.Logging;
// 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.

namespace Benchmarks.Configuration
{
Expand Down
2 changes: 1 addition & 1 deletion src/Benchmarks/Configuration/Scenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static string GetPath(Expression<Func<Scenarios, bool>> scenarioExpressio

public int Enable(string partialName)
{
if(string.Equals(partialName, "[default]", StringComparison.OrdinalIgnoreCase))
if (string.Equals(partialName, "[default]", StringComparison.OrdinalIgnoreCase))
{
EnableDefault();
return 2;
Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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 Microsoft.EntityFrameworkCore;

Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Data/ApplicationDbSeeder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Linq;
Expand Down
6 changes: 3 additions & 3 deletions src/Benchmarks/Data/DapperDb.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Collections.Generic;
Expand Down Expand Up @@ -40,7 +40,7 @@ public async Task<World> LoadSingleQueryRow()
async Task<World> ReadSingleRow(DbConnection db)
{
return await db.QueryFirstOrDefaultAsync<World>(
"SELECT id, randomnumber FROM world WHERE Id = @Id",
"SELECT id, randomnumber FROM world WHERE id = @Id",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was required for compat with a particular DB or driver, but either way it seems better all-lowercase.

new { Id = _random.Next(1, 10001) });
}

Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Data/EfDb.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Collections.Generic;
Expand Down
83 changes: 45 additions & 38 deletions src/Benchmarks/Data/RawDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ public RawDb(IRandom random, DbProviderFactory dbProviderFactory, IOptions<AppSe
public async Task<World> LoadSingleQueryRow()
{
using (var db = _dbProviderFactory.CreateConnection())
using (var cmd = CreateReadCommand(db))
{
db.ConnectionString = _connectionString;
await db.OpenAsync();

return await ReadSingleRow(db, cmd);
using (var cmd = CreateReadCommand(db))
{
return await ReadSingleRow(db, cmd);
}
}
}

async Task<World> ReadSingleRow(DbConnection connection, DbCommand cmd)
{
// Prepared statements improve PostgreSQL performance by 10-15%
cmd.Prepare();

using (var rdr = await cmd.ExecuteReaderAsync(CommandBehavior.SingleRow))
{
await rdr.ReadAsync();
Expand All @@ -64,6 +63,10 @@ DbCommand CreateReadCommand(DbConnection connection)
id.Value = _random.Next(1, 10001);
cmd.Parameters.Add(id);

// Prepared statements improve PostgreSQL performance by 10-15%
// Especially if you only call them once, instead of on every execution :)
cmd.Prepare();

return cmd;
}

Expand All @@ -72,14 +75,16 @@ public async Task<World[]> LoadMultipleQueriesRows(int count)
var result = new World[count];

using (var db = _dbProviderFactory.CreateConnection())
using (var cmd = CreateReadCommand(db))
{
db.ConnectionString = _connectionString;
await db.OpenAsync();
for (int i = 0; i < count; i++)
using (var cmd = CreateReadCommand(db))
{
result[i] = await ReadSingleRow(db, cmd);
cmd.Parameters["@Id"].Value = _random.Next(1, 10001);
for (int i = 0; i < count; i++)
{
result[i] = await ReadSingleRow(db, cmd);
cmd.Parameters["@Id"].Value = _random.Next(1, 10001);
}
}
}

Expand All @@ -93,43 +98,45 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
var updateCommand = new StringBuilder(count);

using (var db = _dbProviderFactory.CreateConnection())
using (var updateCmd = db.CreateCommand())
using (var queryCmd = CreateReadCommand(db))
{
db.ConnectionString = _connectionString;
await db.OpenAsync();

for (int i = 0; i < count; i++)
using (var updateCmd = db.CreateCommand())
using (var queryCmd = CreateReadCommand(db))
{
results[i] = await ReadSingleRow(db, queryCmd);
queryCmd.Parameters["@Id"].Value = _random.Next(1, 10001);
}
for (int i = 0; i < count; i++)
{
results[i] = await ReadSingleRow(db, queryCmd);
queryCmd.Parameters["@Id"].Value = _random.Next(1, 10001);
}

// postgres has problems with deadlocks when these aren't sorted
Array.Sort<World>(results, (a, b) => a.Id.CompareTo(b.Id));
// postgres has problems with deadlocks when these aren't sorted
Array.Sort<World>(results, (a, b) => a.Id.CompareTo(b.Id));

for(int i = 0; i < count; i++)
{
var id = updateCmd.CreateParameter();
id.ParameterName = BatchUpdateString.Strings[i].Id;
id.DbType = DbType.Int32;
updateCmd.Parameters.Add(id);

var random = updateCmd.CreateParameter();
random.ParameterName = BatchUpdateString.Strings[i].Random;
id.DbType = DbType.Int32;
updateCmd.Parameters.Add(random);

var randomNumber = _random.Next(1, 10001);
id.Value = results[i].Id;
random.Value = randomNumber;
results[i].RandomNumber = randomNumber;

updateCommand.Append(BatchUpdateString.Strings[i].UpdateQuery);
}
for(int i = 0; i < count; i++)
{
var id = updateCmd.CreateParameter();
id.ParameterName = BatchUpdateString.Strings[i].Id;
id.DbType = DbType.Int32;
updateCmd.Parameters.Add(id);

var random = updateCmd.CreateParameter();
random.ParameterName = BatchUpdateString.Strings[i].Random;
id.DbType = DbType.Int32;
updateCmd.Parameters.Add(random);

var randomNumber = _random.Next(1, 10001);
id.Value = results[i].Id;
random.Value = randomNumber;
results[i].RandomNumber = randomNumber;

updateCommand.Append(BatchUpdateString.Strings[i].UpdateQuery);
}

updateCmd.CommandText = updateCommand.ToString();
await updateCmd.ExecuteNonQueryAsync();
updateCmd.CommandText = updateCommand.ToString();
await updateCmd.ExecuteNonQueryAsync();
}
}

return results;
Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Data/World.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.ComponentModel.DataAnnotations.Schema;

Expand Down
10 changes: 2 additions & 8 deletions src/Benchmarks/Middleware/DebugInfoPageMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Linq;
Expand All @@ -24,13 +24,7 @@ public class DebugInfoPageMiddleware
private static readonly string _configurationName = "";
#endif

#if NET46
private static readonly string _targetFrameworkName = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;
#elif NETCOREAPP2_0
private static readonly string _targetFrameworkName = AppContext.TargetFrameworkName;
#else
#error the target framework needs to be updated.
#endif

private readonly IHostingEnvironment _hostingEnv;
private readonly RequestDelegate _next;
Expand Down
6 changes: 3 additions & 3 deletions src/Benchmarks/Middleware/FortunesDapperMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Text.Encodings.Web;
Expand Down Expand Up @@ -40,7 +40,7 @@ public async Task Invoke(HttpContext httpContext)
await _next(httpContext);
}
}

public static class FortunesDapperMiddlewareExtensions
{
public static IApplicationBuilder UseFortunesDapper(this IApplicationBuilder builder)
Expand Down
6 changes: 3 additions & 3 deletions src/Benchmarks/Middleware/FortunesRawMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Text.Encodings.Web;
Expand Down Expand Up @@ -40,7 +40,7 @@ public async Task Invoke(HttpContext httpContext)
await _next(httpContext);
}
}

public static class FortunesRawMiddlewareExtensions
{
public static IApplicationBuilder UseFortunesRaw(this IApplicationBuilder builder)
Expand Down
1 change: 0 additions & 1 deletion src/Benchmarks/Middleware/MiddlewareHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public static async Task RenderFortunesHtml(IEnumerable<Fortune> model, HttpCont
// fortunes includes multibyte characters so response.Length is incorrect
httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(response);
await httpContext.Response.WriteAsync(response);

}
}
}
4 changes: 2 additions & 2 deletions src/Benchmarks/Middleware/MultipleQueriesDapperMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Threading.Tasks;
Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Middleware/MultipleQueriesRawMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Threading.Tasks;
Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Middleware/MultipleUpdatesDapperMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Threading.Tasks;
Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Middleware/MultipleUpdatesRawMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Threading.Tasks;
Expand Down
8 changes: 4 additions & 4 deletions src/Benchmarks/Middleware/PlaintextMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Text;
Expand All @@ -16,7 +16,7 @@ public class PlaintextMiddleware
private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");

private readonly RequestDelegate _next;

public PlaintextMiddleware(RequestDelegate next)
{
_next = next;
Expand All @@ -41,7 +41,7 @@ public static Task WriteResponse(HttpResponse response)
return response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength);
}
}

public static class PlaintextMiddlewareExtensions
{
public static IApplicationBuilder UsePlainText(this IApplicationBuilder builder)
Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Middleware/SingleQueryDapperMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Threading.Tasks;
Expand Down
1 change: 0 additions & 1 deletion src/Benchmarks/Middleware/SingleQueryEfMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down
4 changes: 2 additions & 2 deletions src/Benchmarks/Middleware/SingleQueryRawMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 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.
// 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.Threading.Tasks;
Expand Down
6 changes: 4 additions & 2 deletions src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ private static void StartInteractiveConsoleThread()
Console.WriteLine($"Gen 0: {GC.CollectionCount(0)}, Gen 1: {GC.CollectionCount(1)}, Gen 2: {GC.CollectionCount(2)}");
}
}
});
})
{
IsBackground = true
};

interactiveThread.IsBackground = true;
interactiveThread.Start();

started.WaitOne();
Expand Down
Loading