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
39 changes: 35 additions & 4 deletions BenchmarkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public BenchmarkService()
/// and all his/her books (Book Name/Title and Publishment Year) published before 1900
/// </summary>
/// <returns></returns>
[Benchmark]
[Benchmark(Baseline = true)]
public List<AuthorDTO> GetAuthors()
{
using var dbContext = new AppDbContext();
Expand Down Expand Up @@ -88,9 +88,40 @@ public List<AuthorDTO> GetAuthors()
[Benchmark]
public List<AuthorDTO> GetAuthors_Optimized()
{
List<AuthorDTO> authors = new List<AuthorDTO>();

return authors;
using var dbContext = new AppDbContext();
return dbContext.Authors
.Where(x => x.Country == "Serbia" && x.Age == 27)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Include(x => x.User)
.ThenInclude(x => x.UserRoles)
.ThenInclude(x => x.Role)
.Include(x => x.Books)
.ThenInclude(x => x.Publisher)
.Select(x => new AuthorDTO {
UserCreated = x.User.Created,
UserEmailConfirmed = x.User.EmailConfirmed,
UserFirstName = x.User.FirstName,
UserLastActivity = x.User.LastActivity,
UserLastName = x.User.LastName,
UserEmail = x.User.Email,
UserName = x.User.UserName,
UserId = x.User.Id,
RoleId = x.User.UserRoles.FirstOrDefault(y => y.UserId == x.UserId).RoleId,
BooksCount = x.BooksCount,
AllBooks = x.Books.Where(y => y.Published.Year < 1900).Select(y => new BookDto {
Id = y.Id,
Name = y.Name,
Published = y.Published,
ISBN = y.ISBN,
PublisherName = y.Publisher.Name
}).ToList(),
AuthorAge = x.Age,
AuthorCountry = x.Country,
AuthorNickName = x.NickName,
Id = x.Id
})
.ToList();
}
}
}
3 changes: 2 additions & 1 deletion Context/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ namespace OptimizeMePlease.Context
{
public class AppDbContext : DbContext
{
public static readonly string sqlConnectionString = @"Server=.;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true";
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true");
options.UseSqlServer(sqlConnectionString);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
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>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="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
11 changes: 5 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.SqlServer.Management.Smo;
using System;
using System.IO;
using OptimizeMePlease.Context;

namespace OptimizeMePlease
{
Expand All @@ -24,24 +25,22 @@ 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()
{
string sqlConnectionString = @"Server=localhost;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");
string script = File.ReadAllText(path);

SqlConnection conn = new SqlConnection(sqlConnectionString);
SqlConnection conn = new SqlConnection(AppDbContext.sqlConnectionString);

Server server = new Server(new ServerConnection(conn));

Expand Down