diff --git a/Benchmarks.cs b/Benchmarks.cs index 253dff7..0bbaef5 100644 --- a/Benchmarks.cs +++ b/Benchmarks.cs @@ -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 @@ -22,6 +23,7 @@ private class Config : ManualConfig [Params(1000, 10000, 100000)] public int N; private SQLiteTestRun _sqliteTestRun; + private PostgreSQLTestRun _postgresqlTestRun; private DoubletsTestRun _doubletsTestRun; [GlobalSetup] @@ -29,6 +31,7 @@ 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"); } @@ -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(); diff --git a/Comparisons.SQLiteVSDoublets.csproj b/Comparisons.SQLiteVSDoublets.csproj index 2fa06d4..bb23911 100644 --- a/Comparisons.SQLiteVSDoublets.csproj +++ b/Comparisons.SQLiteVSDoublets.csproj @@ -2,13 +2,14 @@ Exe - net6 + net7.0 enable + diff --git a/PostgreSQL/PostgreSQLDbContext.cs b/PostgreSQL/PostgreSQLDbContext.cs new file mode 100644 index 0000000..86cc341 --- /dev/null +++ b/PostgreSQL/PostgreSQLDbContext.cs @@ -0,0 +1,85 @@ +using System.Reflection; +using Microsoft.EntityFrameworkCore; +using Comparisons.SQLiteVSDoublets.Model; + +namespace Comparisons.SQLiteVSDoublets.PostgreSQL +{ + /// + /// + /// Represents the PostgreSQL db context. + /// + /// + /// + /// + public class PostgreSQLDbContext : DbContext + { + /// + /// + /// The connection string. + /// + /// + /// + private readonly string _connectionString; + + /// + /// + /// Gets or sets the blog posts value. + /// + /// + /// + public DbSet BlogPosts { get; set; } + + /// + /// + /// Initializes a new instance. + /// + /// + /// + /// + /// A connection string. + /// + /// + public PostgreSQLDbContext(string connectionString) => _connectionString = connectionString; + + /// + /// + /// Ons the configuring using the specified options builder. + /// + /// + /// + /// + /// The options builder. + /// + /// + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseNpgsql(_connectionString, options => + { + options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName); + }); + base.OnConfiguring(optionsBuilder); + } + + /// + /// + /// Ons the model creating using the specified model builder. + /// + /// + /// + /// + /// The model builder. + /// + /// + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().ToTable("BlogPosts", "test"); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.Id); + entity.HasIndex(e => e.Title).IsUnique(); + entity.Property(e => e.PublicationDateTime).HasDefaultValueSql("CURRENT_TIMESTAMP"); + }); + base.OnModelCreating(modelBuilder); + } + } +} \ No newline at end of file diff --git a/PostgreSQL/PostgreSQLTestRun.cs b/PostgreSQL/PostgreSQLTestRun.cs new file mode 100644 index 0000000..555c215 --- /dev/null +++ b/PostgreSQL/PostgreSQLTestRun.cs @@ -0,0 +1,92 @@ +using System.Linq; +using Comparisons.SQLiteVSDoublets.Model; + +namespace Comparisons.SQLiteVSDoublets.PostgreSQL +{ + /// + /// + /// Represents the PostgreSQL test run. + /// + /// + /// + /// + public class PostgreSQLTestRun : TestRun + { + /// + /// + /// The connection string. + /// + /// + /// + private readonly string _connectionString; + + /// + /// + /// Initializes a new instance. + /// + /// + /// + /// + /// A connection string. + /// + /// + public PostgreSQLTestRun(string connectionString) : base("postgresql") + { + _connectionString = connectionString; + } + + /// + /// + /// Prepares this instance. + /// + /// + /// + public override void Prepare() + { + using var dbContext = new PostgreSQLDbContext(_connectionString); + dbContext.Database.EnsureCreated(); + } + + /// + /// + /// Creates the list. + /// + /// + /// + public override void CreateList() + { + using var dbContext = new PostgreSQLDbContext(_connectionString); + dbContext.BlogPosts.AddRange(BlogPosts.List); + dbContext.SaveChanges(); + } + + /// + /// + /// Reads the list. + /// + /// + /// + public override void ReadList() + { + using var dbContext = new PostgreSQLDbContext(_connectionString); + foreach (var blogPost in dbContext.BlogPosts) + { + ReadBlogPosts.Add(blogPost); + } + } + + /// + /// + /// Deletes the list. + /// + /// + /// + public override void DeleteList() + { + using var dbContext = new PostgreSQLDbContext(_connectionString); + var blogPostsToDelete = dbContext.BlogPosts.ToList(); + dbContext.BlogPosts.RemoveRange(blogPostsToDelete); + dbContext.SaveChanges(); + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 36418b6..932e7cd 100644 --- a/Program.cs +++ b/Program.cs @@ -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; @@ -28,12 +29,16 @@ private static void Run() const int numberOfRecordsPerTestRun = 1; BlogPosts.GenerateData(numberOfRecordsPerTestRun); var sqliteTestRuns = new List(); + var postgresqlTestRuns = new List(); var doubletsTestRuns = new List(); 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); @@ -41,6 +46,9 @@ private static void Run() 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());