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
30 changes: 28 additions & 2 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 @@ -86,11 +87,36 @@ public List<AuthorDTO> GetAuthors()
}

[Benchmark]
public List<AuthorDTO> GetAuthors_Optimized()
public List<MyAuthorDTO> GetAuthors_Optimized()
{
List<AuthorDTO> authors = new List<AuthorDTO>();
using var context = new AppDbContext();
var date = new DateTime(1900, 1, 1);

var authors = context.Authors
.Include(b => b.Books.Where(by => by.Published < date))
.AsNoTracking()
.Where(a => a.Country == "Serbia" && a.Age == 27)
.OrderByDescending(a => a.BooksCount)
.Select(author => new MyAuthorDTO
{

UserFirstName = author.User.FirstName,
UserLastName = author.User.LastName,
UserEmail = author.User.Email,
UserName = author.User.UserName,
AuthorAge = author.Age,
AuthorCountry = author.Country,
AllBooks = author.Books.Select(y => new MyBookDTO
{
Name = y.Name,
PublishedYear = y.Published.Year

})
}).Take(2).ToList();


return authors;
}
}
}

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(@"Data Source=(localdb)\mssqllocaldb;Initial Catalog=OptimizeMePlease;Integrated Security=True");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
19 changes: 19 additions & 0 deletions MyAuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OptimizeMePlease
{
public class MyAuthorDTO
{
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<MyBookDTO> AllBooks { get; set; }
}
}
14 changes: 14 additions & 0 deletions MyBookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OptimizeMePlease
{
public class MyBookDTO
{
public string Name { get; set; }
public int PublishedYear { get; set; }
}
}
14 changes: 7 additions & 7 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</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="BenchmarkDotNet" Version="0.13.2"/>
<PackageReference Include="Bogus" Version="34.0.2"/>
<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 @@ -25,17 +25,17 @@ static void Main(string[] args)
{
//Debugging
BenchmarkService benchmarkService = new BenchmarkService();
benchmarkService.GetAuthors();
//benchmarkService.GetAuthors();

//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 = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=OptimizeMePlease;Integrated Security=True";

string workingDirectory = Environment.CurrentDirectory;
string path = Path.Combine(Directory.GetParent(workingDirectory).Parent.Parent.FullName, @"script.sql");
Expand Down