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
25 changes: 22 additions & 3 deletions BenchmarkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,30 @@ public List<AuthorDTO> GetAuthors()
}

[Benchmark]
public List<AuthorDTO> GetAuthors_Optimized()
public List<OptimizedAuthorDTO> GetAuthors_Optimized()
{
List<AuthorDTO> authors = new List<AuthorDTO>();
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();
}
}
}
2 changes: 1 addition & 1 deletion Context/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 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>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<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="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
25 changes: 25 additions & 0 deletions OptimizeMePlease.sln
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions OptimizedAuthorDto.cs
Original file line number Diff line number Diff line change
@@ -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<OptimizedBookDto> Books { get; set; }
}
}
8 changes: 8 additions & 0 deletions OptimizedBookDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OptimizeMePlease
{
public class OptimizedBookDto
{
public string Name { get; set; }
public int PublishedYear { get; set; }
}
}
9 changes: 5 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BenchmarkService>();
BenchmarkRunner.Run<BenchmarkService>();
}

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");
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down