Skip to content

Commit

Permalink
Improvements to AverageBlogRanking benchmark (#2990)
Browse files Browse the repository at this point in the history
* calculate Rating as Double
* set Optimise=true (Release build) as Benchmark guidelines
* make [some] methods static as analyzer suggestions

Co-authored-by: Shay Rojansky <roji@roji.org>
  • Loading branch information
Dick Baker and roji authored Jan 8, 2021
1 parent d03e8c5 commit 67153e6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
15 changes: 8 additions & 7 deletions samples/core/Benchmarks/AverageBlogRanking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ namespace Benchmarks
[MemoryDiagnoser]
public class AverageBlogRanking
{
public const int NumBlogs = 1000;
[Params(1000)]
public int NumBlogs; // number of records to write [once], and read [each pass]

[GlobalSetup]
public void Setup()
{
using var context = new BloggingContext();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.SeedData();
context.SeedData(NumBlogs);
}

#region LoadEntities
Expand All @@ -35,7 +36,7 @@ public double LoadEntities()
count++;
}

return sum / count;
return (double)sum / count;
}
#endregion

Expand All @@ -52,7 +53,7 @@ public double LoadEntitiesNoTracking()
count++;
}

return sum / count;
return (double)sum / count;
}
#endregion

Expand All @@ -69,7 +70,7 @@ public double ProjectOnlyRanking()
count++;
}

return sum / count;
return (double)sum / count;
}
#endregion

Expand All @@ -89,10 +90,10 @@ public class BloggingContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True");

public void SeedData()
public void SeedData(int numblogs)
{
Blogs.AddRange(
Enumerable.Range(0, NumBlogs).Select(i => new Blog
Enumerable.Range(0, numblogs).Select(i => new Blog
{
Name = $"Blog{i}",
Url = $"blog{i}.blogs.net",
Expand Down
7 changes: 4 additions & 3 deletions samples/core/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="5.0.1" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions samples/core/Benchmarks/DynamicallyConstructedQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DynamicallyConstructedQueries
private int _blogNumber;

[GlobalSetup]
public void GlobalSetup()
public static void GlobalSetup()
{
using var context = new BloggingContext();
context.Database.EnsureDeleted();
Expand All @@ -30,7 +30,7 @@ public int WithConstant()
{
return GetBlogCount("blog" + Interlocked.Increment(ref _blogNumber));

int GetBlogCount(string url)
static int GetBlogCount(string url)
{
using var context = new BloggingContext();

Expand Down
8 changes: 4 additions & 4 deletions samples/core/Benchmarks/Inheritance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Inheritance
[Params(5000)]
public int RowsPerEntityType { get; set; }

[GlobalSetup(Target=nameof(TPH))]
[GlobalSetup(Target = nameof(TPH))]
public void SetupTPH()
{
Console.WriteLine("Setting up database...");
Expand All @@ -26,7 +26,7 @@ public void SetupTPH()
Console.WriteLine("Setup complete.");
}

[GlobalSetup(Target=nameof(TPT))]
[GlobalSetup(Target = nameof(TPT))]
public void SetupTPT()
{
Console.WriteLine("Setting up database...");
Expand All @@ -38,15 +38,15 @@ public void SetupTPT()
}

[Benchmark]
public List<Root> TPH()
public static List<Root> TPH()
{
using var context = new TPHContext();

return context.Roots.ToList();
}

[Benchmark]
public List<Root> TPT()
public static List<Root> TPT()
{
using var context = new TPTContext();

Expand Down
3 changes: 1 addition & 2 deletions samples/core/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Benchmarks
{
public class Program
{
public static void Main(string[] args)
=> BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
8 changes: 4 additions & 4 deletions samples/core/Benchmarks/QueryTrackingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public void Setup()
using var context = new BloggingContext();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.SeedData(NumBlogs, NumPostsPerBlog);
BloggingContext.SeedData(NumBlogs, NumPostsPerBlog);
Console.WriteLine("Setup complete.");
}

[Benchmark(Baseline = true)]
public List<Post> AsTracking()
public static List<Post> AsTracking()
{
using var context = new BloggingContext();

return context.Posts.AsTracking().Include(p => p.Blog).ToList();
}

[Benchmark]
public List<Post> AsNoTracking()
public static List<Post> AsNoTracking()
{
using var context = new BloggingContext();

Expand All @@ -53,7 +53,7 @@ public class BloggingContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True");

public void SeedData(int numBlogs, int numPostsPerBlog)
public static void SeedData(int numBlogs, int numPostsPerBlog)
{
using var context = new BloggingContext();
context.AddRange(
Expand Down

0 comments on commit 67153e6

Please sign in to comment.