Skip to content

Commit

Permalink
Allow journal mode configuration
Browse files Browse the repository at this point in the history
Fix pragmas to apply everytime a connection is opened per dotnet/efcore#5024
  • Loading branch information
gfs committed Mar 11, 2020
1 parent 6de3e67 commit 46d9f60
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
17 changes: 13 additions & 4 deletions AsaBenchmarks/InsertTestsWithoutTransactions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AttackSurfaceAnalyzer.Objects;
using AttackSurfaceAnalyzer.Utils;
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,7 +14,7 @@ public class InsertTestsWithoutTransactions
{
// The number of records to insert for the benchmark
//[Params(25000,50000,100000)]
[Params(75000)]
[Params(10000)]
public int N { get; set; }

// The number of records to populate the database with before the benchmark
Expand All @@ -31,6 +32,9 @@ public class InsertTestsWithoutTransactions
[Params(1,2,3,4,5,6,7,8,9,10,11,12)]
public int Shards { get; set; }

[Params("OFF","DELETE","WAL","MEMORY")]
public string JournalMode { get; set; }

// Bag of reusable objects to write to the database.
private static readonly ConcurrentBag<FileSystemObject> BagOfObjects = new ConcurrentBag<FileSystemObject>();

Expand Down Expand Up @@ -80,7 +84,7 @@ public static FileSystemObject GetRandomObject(int ObjectPadding = 0)

public void PopulateDatabases()
{
DatabaseManager.Setup(filename: $"AsaBenchmark_{Shards}.sqlite", shardingFactor: Shards);
Setup();
DatabaseManager.BeginTransaction();

Insert_X_Objects(StartingSize,ObjectPadding,"PopulateDatabase");
Expand All @@ -98,17 +102,22 @@ public void GlobalSetup()
[GlobalCleanup]
public void GlobalCleanup()
{
DatabaseManager.Setup(filename: $"AsaBenchmark_{Shards}.sqlite", shardingFactor: Shards);
Setup();
DatabaseManager.Destroy();
}

[IterationSetup]
public void IterationSetup()
{
DatabaseManager.Setup(filename: $"AsaBenchmark_{Shards}.sqlite", shardingFactor: Shards);
Setup();
DatabaseManager.BeginTransaction();
}

private void Setup()
{
DatabaseManager.Setup(filename: $"AsaBenchmark_{Shards}.sqlite", shardingFactor: Shards, JournalMode: JournalMode);
}

[IterationCleanup]
public void IterationCleanup()
{
Expand Down
2 changes: 1 addition & 1 deletion AsaBenchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<InsertTestsWithIntermittentTransactions>();
var summary = BenchmarkRunner.Run<InsertTestsWithoutTransactions>();
}
}
}
14 changes: 13 additions & 1 deletion Lib/Objects/SqlConnectionHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using System.Globalization;

namespace AttackSurfaceAnalyzer.Objects
{
Expand All @@ -18,10 +19,21 @@ public class SqlConnectionHolder
private int RecordCount { get; set; }
public int FlushCount { get; set; } = -1;

public SqlConnectionHolder(string databaseFilename, int flushCount = -1)
private const string PRAGMAS = "PRAGMA auto_vacuum = 0; PRAGMA synchronous = OFF";
private const string JOURNAL_MODE = "PRAGMA journal_mode = {0};";

public SqlConnectionHolder(string databaseFilename, int flushCount = -1, string journalMode = "OFF")
{
Source = databaseFilename;
Connection = new SqliteConnection($"Data source={Source}");
Connection.Open();

using var cmd = new SqliteCommand(PRAGMAS, Connection);
cmd.ExecuteNonQuery();

cmd.CommandText = string.Format(CultureInfo.InvariantCulture,JOURNAL_MODE, journalMode);
cmd.ExecuteNonQuery();

StartWriter();
FlushCount = flushCount;
}
Expand Down
17 changes: 8 additions & 9 deletions Lib/Utils/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public static class DatabaseManager

private const string SQL_INSERT = "insert into file_system_monitored (run_id, row_key, timestamp, change_type, path, old_path, name, old_name, extended_results, notify_filters, serialized) values (@run_id, @row_key, @timestamp, @change_type, @path, @old_path, @name, @old_name, @extended_results, @notify_filters, @serialized)";

private const string PRAGMAS = "PRAGMA main.auto_vacuum = 0; PRAGMA main.synchronous = OFF; PRAGMA main.journal_mode = OFF;";

private const string INSERT_RUN_INTO_RESULT_TABLE_SQL = "insert into results (base_run_id, compare_run_id, status) values (@base_run_id, @compare_run_id, @status);";
private const string UPDATE_RUN_IN_RESULT_TABLE = "update results set status = @status where (base_run_id = @base_run_id and compare_run_id = @compare_run_id)";

Expand Down Expand Up @@ -99,6 +97,8 @@ public static class DatabaseManager
private static int SHARDING_FACTOR = 1;
private static int FLUSH_COUNT = -1;

private static string JOURNAL_MODE;


public static SqlConnectionHolder MainConnection
{
Expand All @@ -116,9 +116,12 @@ public static SqlConnectionHolder MainConnection

public static bool FirstRun { get; private set; } = true;

public static bool Setup(string filename = null, int shardingFactor = 1, int flushCount = -1)
public static bool Setup(string filename = null, int shardingFactor = 1, int flushCount = -1, string JournalMode = "OFF")
{
JsonSerializer.SetDefaultResolver(StandardResolver.ExcludeNull);

JOURNAL_MODE = JournalMode;

if (filename != null)
{
if (SqliteFilename != filename)
Expand Down Expand Up @@ -170,9 +173,6 @@ public static bool Setup(string filename = null, int shardingFactor = 1, int flu
{
try
{
using var cmd = new SqliteCommand(PRAGMAS, MainConnection.Connection);
cmd.ExecuteNonQuery();

BeginTransaction();

using var cmd2 = new SqliteCommand(SQL_CREATE_RUNS, MainConnection.Connection, MainConnection.Transaction);
Expand Down Expand Up @@ -282,7 +282,6 @@ public static int PopulateConnections()
for (int i = Connections.Count; i < SHARDING_FACTOR; i++)
{
Connections.Add(GenerateSqlConnection(i));
Connections[i].Connection.Open();
connectionsCreated++;
}
return connectionsCreated;
Expand All @@ -292,11 +291,11 @@ private static SqlConnectionHolder GenerateSqlConnection(int i)
{
if (i == 0)
{
return new SqlConnectionHolder(SqliteFilename, flushCount:FLUSH_COUNT);
return new SqlConnectionHolder(SqliteFilename, flushCount:FLUSH_COUNT, journalMode:JOURNAL_MODE);
}
else
{
return new SqlConnectionHolder($"{SqliteFilename}_{i}", flushCount: FLUSH_COUNT);
return new SqlConnectionHolder($"{SqliteFilename}_{i}", flushCount: FLUSH_COUNT, journalMode: JOURNAL_MODE);
}
}

Expand Down

0 comments on commit 46d9f60

Please sign in to comment.