Skip to content

Commit 0807381

Browse files
committed
Added benchmark to test ways to 'clean' a PostgreSQL db
1 parent da53208 commit 0807381

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
12+
<PackageReference Include="BenchmarkDotNet.Annotations" Version="0.13.1" />
13+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\DataLayer\DataLayer.csproj" />
18+
<ProjectReference Include="..\TestSupport\TestSupport.csproj" />
19+
<ProjectReference Include="..\Test\Test.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

BenchmarkPostgreSql/Program.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Running;
3+
using DataLayer.BookApp;
4+
using DataLayer.BookApp.EfCode;
5+
using Microsoft.EntityFrameworkCore;
6+
using Npgsql;
7+
using Test.Helpers;
8+
using TestSupport.EfHelpers;
9+
using TestSupport.Helpers;
10+
11+
public class Program
12+
{
13+
private const string ConnectionString = "Host=localhost;Username=test;Password=test";
14+
private NpgsqlConnection _conn;
15+
private BookContext _context;
16+
17+
[GlobalSetup]
18+
public void Setup()
19+
{
20+
NpgsqlConnection.ClearAllPools();
21+
22+
var options = this.CreatePostgreSqlUniqueDatabaseOptions<BookContext>();
23+
_context = new BookContext(options);
24+
_context.Database.EnsureCreated();
25+
26+
_conn = new NpgsqlConnection(_context.Database.GetDbConnection().ConnectionString);
27+
_conn.Open();
28+
}
29+
30+
//[IterationSetup]
31+
//public void CreateTables()
32+
//{
33+
// _context.Database.EnsureCreated();
34+
//}
35+
36+
[Benchmark]
37+
public void DropPublicSchemaWithEnsureCreated()
38+
{
39+
var dropPublicSchemaBatch = new NpgsqlBatch(_conn)
40+
{
41+
BatchCommands =
42+
{
43+
new ("DROP SCHEMA public CASCADE"),
44+
new ("CREATE SCHEMA public"),
45+
new ("GRANT ALL ON SCHEMA public TO postgres"),
46+
new ("GRANT ALL ON SCHEMA public TO public")
47+
}
48+
};
49+
dropPublicSchemaBatch.ExecuteNonQuery();
50+
_context.Database.EnsureCreated();
51+
}
52+
53+
[Benchmark]
54+
public void DropAllSchemasWithEnsureCreated()
55+
{
56+
var dropPublicSchemaCommand = new NpgsqlCommand
57+
{
58+
Connection = _conn,
59+
CommandText = @"
60+
DO $$
61+
DECLARE
62+
r RECORD;
63+
BEGIN
64+
FOR r IN (SELECT nspname FROM pg_namespace WHERE nspname NOT IN ('pg_toast', 'pg_catalog', 'information_schema'))
65+
LOOP
66+
EXECUTE 'DROP SCHEMA ' || quote_ident(r.nspname) || ' CASCADE';
67+
END LOOP;
68+
EXECUTE 'CREATE SCHEMA public';
69+
END $$"
70+
};
71+
dropPublicSchemaCommand.ExecuteNonQuery();
72+
_context.Database.EnsureCreated();
73+
}
74+
75+
[Benchmark]
76+
public void EnsureClean()
77+
{
78+
_context.Database.EnsureClean();
79+
}
80+
81+
static void Main(string[] args) => BenchmarkRunner.Run<Program>();
82+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ConnectionStrings": {
3+
"PostgreSqlConnection": "host=localhost;Database=TestSupportBenchmark-Test;Username=postgres;Password=LetMeIn"
4+
}
5+
}
6+

EfCoreInAction.Test.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestSupport", "TestSupport\
2121
EndProject
2222
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFromSqlRaw", "TestFromSqlRaw\TestFromSqlRaw.csproj", "{AADA2740-B6C5-4915-8705-4AD3B40F00D8}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkPostgreSql", "BenchmarkPostgreSql\BenchmarkPostgreSql.csproj", "{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -115,6 +117,26 @@ Global
115117
{AADA2740-B6C5-4915-8705-4AD3B40F00D8}.Release|x64.Build.0 = Release|Any CPU
116118
{AADA2740-B6C5-4915-8705-4AD3B40F00D8}.Release|x86.ActiveCfg = Release|Any CPU
117119
{AADA2740-B6C5-4915-8705-4AD3B40F00D8}.Release|x86.Build.0 = Release|Any CPU
120+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
121+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
122+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|ARM.ActiveCfg = Debug|Any CPU
123+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|ARM.Build.0 = Debug|Any CPU
124+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|ARM64.ActiveCfg = Debug|Any CPU
125+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|ARM64.Build.0 = Debug|Any CPU
126+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|x64.ActiveCfg = Debug|Any CPU
127+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|x64.Build.0 = Debug|Any CPU
128+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|x86.ActiveCfg = Debug|Any CPU
129+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Debug|x86.Build.0 = Debug|Any CPU
130+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
131+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|Any CPU.Build.0 = Release|Any CPU
132+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|ARM.ActiveCfg = Release|Any CPU
133+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|ARM.Build.0 = Release|Any CPU
134+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|ARM64.ActiveCfg = Release|Any CPU
135+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|ARM64.Build.0 = Release|Any CPU
136+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|x64.ActiveCfg = Release|Any CPU
137+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|x64.Build.0 = Release|Any CPU
138+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|x86.ActiveCfg = Release|Any CPU
139+
{E12C6C5C-699B-4361-B717-7E4B40A9C1E1}.Release|x86.Build.0 = Release|Any CPU
118140
EndGlobalSection
119141
GlobalSection(SolutionProperties) = preSolution
120142
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)