From 67153e6fecdc591e0e3f4be0344a62beacc85245 Mon Sep 17 00:00:00 2001 From: Dick Baker Date: Fri, 8 Jan 2021 14:44:46 +0000 Subject: [PATCH] Improvements to AverageBlogRanking benchmark (#2990) * calculate Rating as Double * set Optimise=true (Release build) as Benchmark guidelines * make [some] methods static as analyzer suggestions Co-authored-by: Shay Rojansky --- samples/core/Benchmarks/AverageBlogRanking.cs | 15 ++++++++------- samples/core/Benchmarks/Benchmarks.csproj | 7 ++++--- .../Benchmarks/DynamicallyConstructedQueries.cs | 4 ++-- samples/core/Benchmarks/Inheritance.cs | 8 ++++---- samples/core/Benchmarks/Program.cs | 3 +-- samples/core/Benchmarks/QueryTrackingBehavior.cs | 8 ++++---- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/samples/core/Benchmarks/AverageBlogRanking.cs b/samples/core/Benchmarks/AverageBlogRanking.cs index b7ffbe8dcb..33d762b811 100644 --- a/samples/core/Benchmarks/AverageBlogRanking.cs +++ b/samples/core/Benchmarks/AverageBlogRanking.cs @@ -11,7 +11,8 @@ 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() @@ -19,7 +20,7 @@ public void Setup() using var context = new BloggingContext(); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); - context.SeedData(); + context.SeedData(NumBlogs); } #region LoadEntities @@ -35,7 +36,7 @@ public double LoadEntities() count++; } - return sum / count; + return (double)sum / count; } #endregion @@ -52,7 +53,7 @@ public double LoadEntitiesNoTracking() count++; } - return sum / count; + return (double)sum / count; } #endregion @@ -69,7 +70,7 @@ public double ProjectOnlyRanking() count++; } - return sum / count; + return (double)sum / count; } #endregion @@ -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", diff --git a/samples/core/Benchmarks/Benchmarks.csproj b/samples/core/Benchmarks/Benchmarks.csproj index 94fb4a34a9..d6295ac764 100644 --- a/samples/core/Benchmarks/Benchmarks.csproj +++ b/samples/core/Benchmarks/Benchmarks.csproj @@ -1,12 +1,13 @@ - + Exe net5.0 + true - - + + diff --git a/samples/core/Benchmarks/DynamicallyConstructedQueries.cs b/samples/core/Benchmarks/DynamicallyConstructedQueries.cs index 19dbc079a5..09915ca152 100644 --- a/samples/core/Benchmarks/DynamicallyConstructedQueries.cs +++ b/samples/core/Benchmarks/DynamicallyConstructedQueries.cs @@ -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(); @@ -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(); diff --git a/samples/core/Benchmarks/Inheritance.cs b/samples/core/Benchmarks/Inheritance.cs index 92bd53be1a..e321c23941 100644 --- a/samples/core/Benchmarks/Inheritance.cs +++ b/samples/core/Benchmarks/Inheritance.cs @@ -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..."); @@ -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..."); @@ -38,7 +38,7 @@ public void SetupTPT() } [Benchmark] - public List TPH() + public static List TPH() { using var context = new TPHContext(); @@ -46,7 +46,7 @@ public List TPH() } [Benchmark] - public List TPT() + public static List TPT() { using var context = new TPTContext(); diff --git a/samples/core/Benchmarks/Program.cs b/samples/core/Benchmarks/Program.cs index 61204931fe..a000fbd7eb 100644 --- a/samples/core/Benchmarks/Program.cs +++ b/samples/core/Benchmarks/Program.cs @@ -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); } } diff --git a/samples/core/Benchmarks/QueryTrackingBehavior.cs b/samples/core/Benchmarks/QueryTrackingBehavior.cs index c4c6b1c281..9dd490447a 100644 --- a/samples/core/Benchmarks/QueryTrackingBehavior.cs +++ b/samples/core/Benchmarks/QueryTrackingBehavior.cs @@ -25,12 +25,12 @@ 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 AsTracking() + public static List AsTracking() { using var context = new BloggingContext(); @@ -38,7 +38,7 @@ public List AsTracking() } [Benchmark] - public List AsNoTracking() + public static List AsNoTracking() { using var context = new BloggingContext(); @@ -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(