From abe8f1364932ad405954c4a6f1d347a774d6c37a Mon Sep 17 00:00:00 2001 From: Syed Muhammad Umer Date: Thu, 5 Sep 2024 17:36:02 +0500 Subject: [PATCH 1/2] optimized challenge --- BenchmarkService.cs | 32 ++++++++++++++++++++++++++++++-- OptimizeMePlease.csproj | 12 ++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index a32a5c8..1f6c526 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -1,6 +1,4 @@ using BenchmarkDotNet.Attributes; -using BenchmarkDotNet.Columns; -using BenchmarkDotNet.Configs; using Microsoft.EntityFrameworkCore; using OptimizeMePlease.Context; using System; @@ -183,5 +181,35 @@ public List GetAuthors_Optimized_Struct1() .Take(2) .ToList(); } + + [Benchmark] + public List GetAuthors_Optimized() + { + using var dbContext = new AppDbContext(); + + var authors = dbContext.Authors + .Include(x => x.Books.Where(a => a.Published.Year < 1900)) + .Where(x => x.Country == "Serbia" && x.Age == 27) + .OrderByDescending(x => x.BooksCount) + .Select(x => new AuthorDTO_Optimized + { + FirstName = x.User.FirstName, + LastName = x.User.LastName, + UserName = x.User.UserName, + Email = x.User.Email, + Age = x.Age, + Country = x.Country, + Books = x.Books + .Select(y => new BookDTO_Optimized + { + Title = y.Name, + PublishedYear = y.Published.Year + }).ToList() + }) + .Take(2) + .ToList(); + + return authors; + } } } \ No newline at end of file diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index f603ecd..8b7eba4 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -2,16 +2,16 @@ Exe - netcoreapp3.1 + net8.0 - + - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive From ed2613a06dbc5678f6ed24c6273beca5a65a37ae Mon Sep 17 00:00:00 2001 From: Syed Muhammad Umer <60312380+umermuhammad114@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:54:07 +0500 Subject: [PATCH 2/2] Update README.md --- README.md | 46 +++++----------------------------------------- 1 file changed, 5 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 4982f83..e69a1fd 100644 --- a/README.md +++ b/README.md @@ -1,49 +1,13 @@ # OptimizeMePlease -## You are probably here because you saw my post on Linkedin. -## Welcome! +Hi! In this forked repository, I took a challenge to improve LINQ query performance using entity framework core. -# Steps +Following is the link to the pull request, feel free to check it out: https://github.com/StefanTheCode/OptimizeMePlease/pull/25/files -- Before you run an application, you will need to create a database (I'm using MSSQL) named "OptimizeMePlease" -- Go to Program.cs class -- "IWillPopulateDate()" is a method which will get a script from the project directory and run in on created DB -- Run application in Debug/Release mode -- Comment or delete IWillPopulateData() call from Main method -- Go to BenchmarkService.cs class -- Start coding within GetAuthors_Optimized method +Also, you can see the screenshot below for benchmark results: -# How do I submit my solution? -- Clone the project, create a branch and work on that branch. +![benchmark](https://github.com/user-attachments/assets/fd4c8f16-0706-4f02-b4fa-476ae3367a16) -### OR -- If you don't want to bother with github, after you're done with the changes, send me the results on Linkedin. - -# Rules - -- Only Entity Framework (Core) is allowed for using -- The data obtained in the non-optimized version of the code must also be obtained in the optimized version -- If you see potential optimization of something else, you can do it -- Entities and DbContext cannot be changed (you got legacy code no matter what it might be bad :) ) -- The models returned from the method can be changed - -# What should the method return? - -- Given that there is a predefined database of data, the method should in any case return the list of data currently returned by the non-optimized method. - -# How will performance success be measured? - -- Given that each of us works on a computer with different performance and power, the execution time quotient of the non-optimized and optimized method will be checked for each separately. - -Example: - -Non-optimized method Execution time: 1.1s = 1100ms -Optimized method Execution time: 200ms - -### Result: 1100ms/200ms = 5.5x faster. - -In addition, I will check the performance on my computer for individual results. - -# GOOD LUCK! +Thanks!