Skip to content
Open
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
32 changes: 30 additions & 2 deletions BenchmarkService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using Microsoft.EntityFrameworkCore;
using OptimizeMePlease.Context;
using System;
Expand Down Expand Up @@ -183,5 +181,35 @@ public List<AuthorDTO_OptimizedStruct> GetAuthors_Optimized_Struct1()
.Take(2)
.ToList();
}

[Benchmark]
public List<AuthorDTO_Optimized> 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;
}
}
}
12 changes: 6 additions & 6 deletions OptimizeMePlease.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.29">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
46 changes: 5 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!