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
15 changes: 15 additions & 0 deletions AuthorDTO_Optimized.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace OptimizeMePlease
{
public class AuthorDTO_Optimized
{
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserName { get; set; }
public string UserEmail { get; set; }
public int AuthorAge { get; set; }
public string AuthorCountry { get; set; }
public IEnumerable<BookDto_Optimized> AllBooks { get; set; }
}
}
32 changes: 28 additions & 4 deletions BenchmarkService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BenchmarkDotNet.Attributes;
using Microsoft.EntityFrameworkCore;
using OptimizeMePlease.Context;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -85,12 +86,35 @@ public List<AuthorDTO> GetAuthors()
return finalAuthors;
}

[Benchmark]
public List<AuthorDTO> GetAuthors_Optimized()
[Benchmark]
public List<AuthorDTO_Optimized> GetAuthors_Optimized()
{
List<AuthorDTO> authors = new List<AuthorDTO>();
var cutOfDate = new DateTime(1900, 1, 1);

using var dbContext = new AppDbContext();

return authors;
return dbContext.Authors
.Where(x => x.Country == "Serbia" && x.Age == 27)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Include(x => x.User)
.Include(x => x.Books.Where(xx => xx.Published < cutOfDate))
.AsNoTracking()
.Select(x => new AuthorDTO_Optimized
{
UserFirstName = x.User.FirstName,
UserLastName = x.User.LastName,
UserName = x.User.UserName,
UserEmail = x.User.Email,
AuthorAge = x.Age,
AuthorCountry = x.Country,
AllBooks = x.Books.Select(y => new BookDto_Optimized
{
Name = y.Name,
PublishedYear = y.Published.Year,
})
})
.ToList();
}
}
}
8 changes: 8 additions & 0 deletions BookDto_Optimized.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OptimizeMePlease
{
public class BookDto_Optimized
{
public string Name { get; set; }
public int PublishedYear { get; set; }
}
}
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
6 changes: 3 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class Program
static void Main(string[] args)
{
//Debugging
BenchmarkService benchmarkService = new BenchmarkService();
benchmarkService.GetAuthors();
//BenchmarkService benchmarkService = new BenchmarkService();
//benchmarkService.GetAuthors();

//Comment me after first execution, please.
//IWillPopulateData();

//BenchmarkRunner.Run<BenchmarkService>();
BenchmarkRunner.Run<BenchmarkService>();
}

public static void IWillPopulateData()
Expand Down