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
13 changes: 13 additions & 0 deletions Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BenchmarkDotNet.Configs;
using Comparisons.SQLiteVSDoublets.Model;
using Comparisons.SQLiteVSDoublets.SQLite;
using Comparisons.SQLiteVSDoublets.PostgreSQL;
using Comparisons.SQLiteVSDoublets.Doublets;

namespace Comparisons.SQLiteVSDoublets
Expand All @@ -22,13 +23,15 @@ private class Config : ManualConfig
[Params(1000, 10000, 100000)]
public int N;
private SQLiteTestRun _sqliteTestRun;
private PostgreSQLTestRun _postgresqlTestRun;
private DoubletsTestRun _doubletsTestRun;

[GlobalSetup]
public void Setup()
{
BlogPosts.GenerateData(N);
_sqliteTestRun = new SQLiteTestRun("test.db");
_postgresqlTestRun = new PostgreSQLTestRun("Host=localhost;Database=test;Username=test;Password=test");
_doubletsTestRun = new DoubletsTestRun("test.links");
}

Expand All @@ -42,6 +45,16 @@ public void SQLiteOutput()
File.WriteAllText(Path.Combine(SizeAfterCreationColumn.DbSizeOutputFolder, $"disk-size.sqlite.{N}.txt"), _sqliteTestRun.Results.DbSizeAfterCreation.ToString());
}

[Benchmark]
public void PostgreSQL() => _postgresqlTestRun.Run();

[IterationCleanup(Target = "PostgreSQL")]
public void PostgreSQLOutput()
{
Directory.CreateDirectory(SizeAfterCreationColumn.DbSizeOutputFolder);
File.WriteAllText(Path.Combine(SizeAfterCreationColumn.DbSizeOutputFolder, $"disk-size.postgresql.{N}.txt"), _postgresqlTestRun.Results.DbSizeAfterCreation.ToString());
}

[Benchmark]
public void Doublets() => _doubletsTestRun.Run();

Expand Down
3 changes: 2 additions & 1 deletion Comparisons.SQLiteVSDoublets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.9" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
<PackageReference Include="Platform.Diagnostics" Version="0.1.0" />
</ItemGroup>

Expand Down
85 changes: 85 additions & 0 deletions PostgreSQL/PostgreSQLDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Comparisons.SQLiteVSDoublets.Model;

namespace Comparisons.SQLiteVSDoublets.PostgreSQL
{
/// <summary>
/// <para>
/// Represents the PostgreSQL db context.
/// </para>
/// <para></para>
/// </summary>
/// <seealso cref="DbContext"/>
public class PostgreSQLDbContext : DbContext
{
/// <summary>
/// <para>
/// The connection string.
/// </para>
/// <para></para>
/// </summary>
private readonly string _connectionString;

/// <summary>
/// <para>
/// Gets or sets the blog posts value.
/// </para>
/// <para></para>
/// </summary>
public DbSet<BlogPost> BlogPosts { get; set; }

/// <summary>
/// <para>
/// Initializes a new <see cref="PostgreSQLDbContext"/> instance.
/// </para>
/// <para></para>
/// </summary>
/// <param name="connectionString">
/// <para>A connection string.</para>
/// <para></para>
/// </param>
public PostgreSQLDbContext(string connectionString) => _connectionString = connectionString;

/// <summary>
/// <para>
/// Ons the configuring using the specified options builder.
/// </para>
/// <para></para>
/// </summary>
/// <param name="optionsBuilder">
/// <para>The options builder.</para>
/// <para></para>
/// </param>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_connectionString, options =>
{
options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
});
base.OnConfiguring(optionsBuilder);
}

/// <summary>
/// <para>
/// Ons the model creating using the specified model builder.
/// </para>
/// <para></para>
/// </summary>
/// <param name="modelBuilder">
/// <para>The model builder.</para>
/// <para></para>
/// </param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BlogPost>().ToTable("BlogPosts", "test");
modelBuilder.Entity<BlogPost>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.Title).IsUnique();
entity.Property(e => e.PublicationDateTime).HasDefaultValueSql("CURRENT_TIMESTAMP");
});
base.OnModelCreating(modelBuilder);
}
}
}
92 changes: 92 additions & 0 deletions PostgreSQL/PostgreSQLTestRun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Linq;
using Comparisons.SQLiteVSDoublets.Model;

namespace Comparisons.SQLiteVSDoublets.PostgreSQL
{
/// <summary>
/// <para>
/// Represents the PostgreSQL test run.
/// </para>
/// <para></para>
/// </summary>
/// <seealso cref="TestRun"/>
public class PostgreSQLTestRun : TestRun
{
/// <summary>
/// <para>
/// The connection string.
/// </para>
/// <para></para>
/// </summary>
private readonly string _connectionString;

/// <summary>
/// <para>
/// Initializes a new <see cref="PostgreSQLTestRun"/> instance.
/// </para>
/// <para></para>
/// </summary>
/// <param name="connectionString">
/// <para>A connection string.</para>
/// <para></para>
/// </param>
public PostgreSQLTestRun(string connectionString) : base("postgresql")
{
_connectionString = connectionString;
}

/// <summary>
/// <para>
/// Prepares this instance.
/// </para>
/// <para></para>
/// </summary>
public override void Prepare()
{
using var dbContext = new PostgreSQLDbContext(_connectionString);
dbContext.Database.EnsureCreated();
}

/// <summary>
/// <para>
/// Creates the list.
/// </para>
/// <para></para>
/// </summary>
public override void CreateList()
{
using var dbContext = new PostgreSQLDbContext(_connectionString);
dbContext.BlogPosts.AddRange(BlogPosts.List);
dbContext.SaveChanges();
}

/// <summary>
/// <para>
/// Reads the list.
/// </para>
/// <para></para>
/// </summary>
public override void ReadList()
{
using var dbContext = new PostgreSQLDbContext(_connectionString);
foreach (var blogPost in dbContext.BlogPosts)
{
ReadBlogPosts.Add(blogPost);
}
}

/// <summary>
/// <para>
/// Deletes the list.
/// </para>
/// <para></para>
/// </summary>
public override void DeleteList()
{
using var dbContext = new PostgreSQLDbContext(_connectionString);
var blogPostsToDelete = dbContext.BlogPosts.ToList();
dbContext.BlogPosts.RemoveRange(blogPostsToDelete);
dbContext.SaveChanges();
}
}
}
8 changes: 8 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Collections.Generic;
using Comparisons.SQLiteVSDoublets.SQLite;
using Comparisons.SQLiteVSDoublets.PostgreSQL;
using Comparisons.SQLiteVSDoublets.Doublets;
using Comparisons.SQLiteVSDoublets.Model;
using BenchmarkDotNet.Running;
Expand All @@ -28,19 +29,26 @@ private static void Run()
const int numberOfRecordsPerTestRun = 1;
BlogPosts.GenerateData(numberOfRecordsPerTestRun);
var sqliteTestRuns = new List<SQLiteTestRun>();
var postgresqlTestRuns = new List<PostgreSQLTestRun>();
var doubletsTestRuns = new List<DoubletsTestRun>();
for (int i = 0; i < numberOfTestRuns; i++)
{
var sqliteTestRun = new SQLiteTestRun("test.db");
sqliteTestRun.Run();
sqliteTestRuns.Add(sqliteTestRun);
var postgresqlTestRun = new PostgreSQLTestRun("Host=localhost;Database=test;Username=test;Password=test");
postgresqlTestRun.Run();
postgresqlTestRuns.Add(postgresqlTestRun);
var doubletsTestRun = new DoubletsTestRun("test.links");
doubletsTestRun.Run();
doubletsTestRuns.Add(doubletsTestRun);
}
Console.WriteLine("SQLite results:");
var averageSqliteResults = GetResultsAverage(sqliteTestRuns);
Console.WriteLine(averageSqliteResults.ToString());
Console.WriteLine("PostgreSQL results:");
var averagePostgreSqlResults = GetResultsAverage(postgresqlTestRuns);
Console.WriteLine(averagePostgreSqlResults.ToString());
Console.WriteLine("Doublets results:");
var averageDoubletsResults = GetResultsAverage(doubletsTestRuns);
Console.WriteLine(averageDoubletsResults.ToString());
Expand Down
Loading