Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- using SQLite;
- namespace ConcurrencyTests {
- public class ConcurrentSelectTests {
- private const int ENTRY_COUNT = 50000;
- private string m_databaseFileName;
- private void SetUp(bool fillTable) {
- this.m_databaseFileName = @"d:\concurrency.db";
- if (File.Exists(this.m_databaseFileName)) {
- File.Delete(this.m_databaseFileName);
- }
- var db = DatabaseConnection.OpenFileDb(this.m_databaseFileName, DatabaseAccessMode.ReadWrite);
- db.Execute("CREATE TABLE tests (id INTEGER PRIMARY KEY AUTOINCREMENT, val2 INTEGER, val3 INTEGER, val4 INTEGER, val5 INTEGER)");
- Stmt stmt = db.Compile("INSERT INTO tests (val2, val3, val4, val5) VALUES (?, ?, ?, ?)");
- Random rnd = new Random();
- // NOTE: Strangely, if the table is empty the single
- // connection version is slower than the multi connection version. If the table is filled,
- // the single connection version is much faster.
- if (fillTable) {
- db.Execute("BEGIN TRANSACTION");
- for (int x = 0; x < ENTRY_COUNT; x++) {
- stmt.ClearBindings();
- stmt.MultiBind(rnd.Next(), rnd.Next(), rnd.Next(), rnd.Next());
- stmt.Execute();
- }
- db.Execute("COMMIT TRANSACTION");
- }
- db.Close();
- }
- public void TestSameConnection(DatabaseAccessMode accessMode, bool fillTable, int threadCount, int selectCount) {
- Console.WriteLine("Testing with one connections (" + accessMode + ") and " + (fillTable ? "filled" : "empty") + " table...");
- SetUp(fillTable);
- Stopwatch stopwatch = Stopwatch.StartNew();
- var db = DatabaseConnection.OpenFileDb(this.m_databaseFileName, accessMode);
- List<QueryThread> threads = new List<QueryThread>();
- for (int x = 0; x < threadCount; x++) {
- threads.Add(QueryThread.Start(db, x, selectCount));
- }
- foreach (QueryThread thread in threads) {
- thread.Join();
- }
- db.Close();
- stopwatch.Stop();
- Console.WriteLine("Elapsed: {0:0.0} s", stopwatch.Elapsed.TotalSeconds);
- }
- public void TestDifferentConnections(DatabaseAccessMode accessMode, bool fillTable, int threadCount, int selectCount) {
- Console.WriteLine("Testing with one connections (" + accessMode + ") and " + (fillTable ? "filled" : "empty") + " table...");
- SetUp(fillTable);
- Stopwatch stopwatch = Stopwatch.StartNew();
- List<QueryThread> threads = new List<QueryThread>();
- for (int x = 0; x < threadCount; x++) {
- var db = DatabaseConnection.OpenFileDb(this.m_databaseFileName, accessMode);
- threads.Add(QueryThread.Start(db, x, selectCount));
- }
- foreach (QueryThread thread in threads) {
- thread.Join();
- thread.m_stmt.DatabaseConnection.Close();
- }
- stopwatch.Stop();
- Console.WriteLine("Elapsed: {0:0.0} s", stopwatch.Elapsed.TotalSeconds);
- }
- private class QueryThread {
- private const string SELECT_SQL = "SELECT * FROM tests WHERE id = ?";
- public readonly Stmt m_stmt;
- private readonly Thread m_thread;
- private readonly int m_selectCount;
- private QueryThread(DatabaseConnection db, int num, int selectCount) {
- this.m_stmt = db.Compile(SELECT_SQL);
- this.m_thread = new Thread(this.Run);
- this.m_thread.Name = "Query Thread " + num;
- this.m_selectCount = selectCount;
- }
- public static QueryThread Start(DatabaseConnection db, int num, int selectCount) {
- QueryThread queryThread = new QueryThread(db, num, selectCount);
- queryThread.m_thread.Start();
- return queryThread;
- }
- public void Join() {
- this.m_thread.Join();
- }
- private void Run() {
- Random rnd = new Random();
- int dummy = 0;
- for (int x = 0; x < this.m_selectCount; x++) {
- int id = rnd.Next(ENTRY_COUNT);
- this.m_stmt.ClearBindings();
- this.m_stmt.Bind(1, id);
- foreach (ResultRow resultRow in this.m_stmt.Query()) {
- for (int col = 0; col < resultRow.ColumnCount; col++) {
- dummy += resultRow.GetColumnAsInt(col);
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment