diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..f0a1132 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -86,11 +86,30 @@ public List GetAuthors() } [Benchmark] - public List GetAuthors_Optimized() + public List GetAuthors_Optimized() { - List authors = new List(); + using var dbContext = new AppDbContext(); - return authors; + return dbContext.Authors + .AsNoTracking() + .Where(a => a.Country == "Serbia" && a.Age == 27) + .OrderByDescending(a => a.BooksCount) + .Take(2) + .Select(a => new OptimizedAuthorDTO + { + FirstName = a.User.FirstName, + LastName = a.User.LastName, + UserName = a.User.UserName, + Email = a.User.Email, + Age = a.Age, + Country = a.Country, + Books = a.Books.Where(b => b.Published.Year < 1900).Select(b => new OptimizedBookDto + { + Name = b.Name, + PublishedYear = b.Published.Year + }) + }) + .ToList(); } } } diff --git a/Context/AppDbContext.cs b/Context/AppDbContext.cs index a4c4b8b..257ca0a 100644 --- a/Context/AppDbContext.cs +++ b/Context/AppDbContext.cs @@ -7,7 +7,7 @@ public class AppDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder options) { - options.UseSqlServer("Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"); + options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index 7e4fcc9..c898ab0 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -2,16 +2,16 @@ Exe - netcoreapp3.1 + net6.0 - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/OptimizeMePlease.sln b/OptimizeMePlease.sln new file mode 100644 index 0000000..8eb2b0b --- /dev/null +++ b/OptimizeMePlease.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OptimizeMePlease", "OptimizeMePlease.csproj", "{CC059C2B-FDAC-4B94-869B-F139F692624E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CC059C2B-FDAC-4B94-869B-F139F692624E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CC059C2B-FDAC-4B94-869B-F139F692624E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC059C2B-FDAC-4B94-869B-F139F692624E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CC059C2B-FDAC-4B94-869B-F139F692624E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {74EB4B10-1393-4728-BE1D-06C0FFACD78A} + EndGlobalSection +EndGlobal diff --git a/OptimizedAuthorDto.cs b/OptimizedAuthorDto.cs new file mode 100644 index 0000000..21edd0b --- /dev/null +++ b/OptimizedAuthorDto.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace OptimizeMePlease +{ + public class OptimizedAuthorDTO + { + public string FirstName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public string UserName { get; set; } + public int Age { get; set; } + public string Country { get; set; } + public IEnumerable Books { get; set; } + } +} diff --git a/OptimizedBookDto.cs b/OptimizedBookDto.cs new file mode 100644 index 0000000..c9a3ea3 --- /dev/null +++ b/OptimizedBookDto.cs @@ -0,0 +1,8 @@ +namespace OptimizeMePlease +{ + public class OptimizedBookDto + { + public string Name { get; set; } + public int PublishedYear { get; set; } + } +} diff --git a/Program.cs b/Program.cs index b05ee2d..4d6d05e 100644 --- a/Program.cs +++ b/Program.cs @@ -24,18 +24,19 @@ public class Program static void Main(string[] args) { //Debugging - BenchmarkService benchmarkService = new BenchmarkService(); - benchmarkService.GetAuthors(); + //BenchmarkService benchmarkService = new BenchmarkService(); + //benchmarkService.GetAuthors(); + //var authors = benchmarkService.GetAuthors_Optimized().Result; //Comment me after first execution, please. //IWillPopulateData(); - //BenchmarkRunner.Run(); + BenchmarkRunner.Run(); } public static void IWillPopulateData() { - string sqlConnectionString = @"Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"; + string sqlConnectionString = @"Server=(localdb)\MSSQLLocalDB;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"; string workingDirectory = Environment.CurrentDirectory; string path = Path.Combine(Directory.GetParent(workingDirectory).Parent.Parent.FullName, @"script.sql"); diff --git a/README.md b/README.md index 2bef293..e265f67 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # OptimizeMePlease -## You are probably here because you saw my post on Linkedin. -## Welcome! +### Benchmark Result +![benchmark](https://user-images.githubusercontent.com/57834677/192765626-174e2c57-f924-4868-b182-857473cef340.PNG) + # Steps