Guest User

SQLite performance test code

a guest
Sep 20th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Threading;
  6. using SQLite;
  7.  
  8. namespace ConcurrencyTests {
  9.   public class ConcurrentSelectTests {
  10.     private const int ENTRY_COUNT = 50000;
  11.  
  12.     private string m_databaseFileName;
  13.  
  14.     private void SetUp(bool fillTable) {
  15.       this.m_databaseFileName = @"d:\concurrency.db";
  16.       if (File.Exists(this.m_databaseFileName)) {
  17.         File.Delete(this.m_databaseFileName);
  18.       }
  19.  
  20.       var db = DatabaseConnection.OpenFileDb(this.m_databaseFileName, DatabaseAccessMode.ReadWrite);
  21.  
  22.       db.Execute("CREATE TABLE tests (id INTEGER PRIMARY KEY AUTOINCREMENT, val2 INTEGER, val3 INTEGER, val4 INTEGER, val5 INTEGER)");
  23.  
  24.       Stmt stmt = db.Compile("INSERT INTO tests (val2, val3, val4, val5) VALUES (?, ?, ?, ?)");
  25.       Random rnd = new Random();
  26.  
  27.       // NOTE: Strangely, if the table is empty the single
  28.       //   connection version is slower than the multi connection version. If the table is filled,
  29.       //   the single connection version is much faster.
  30.       if (fillTable) {
  31.         db.Execute("BEGIN TRANSACTION");
  32.  
  33.         for (int x = 0; x < ENTRY_COUNT; x++) {
  34.           stmt.ClearBindings();
  35.           stmt.MultiBind(rnd.Next(), rnd.Next(), rnd.Next(), rnd.Next());
  36.           stmt.Execute();
  37.         }
  38.  
  39.         db.Execute("COMMIT TRANSACTION");
  40.       }
  41.  
  42.       db.Close();
  43.     }
  44.  
  45.     public void TestSameConnection(DatabaseAccessMode accessMode, bool fillTable, int threadCount, int selectCount) {
  46.       Console.WriteLine("Testing with one connections (" + accessMode + ") and " + (fillTable ? "filled" : "empty") + " table...");
  47.  
  48.       SetUp(fillTable);
  49.  
  50.       Stopwatch stopwatch = Stopwatch.StartNew();
  51.       var db = DatabaseConnection.OpenFileDb(this.m_databaseFileName, accessMode);
  52.  
  53.       List<QueryThread> threads = new List<QueryThread>();
  54.       for (int x = 0; x < threadCount; x++) {
  55.         threads.Add(QueryThread.Start(db, x, selectCount));
  56.       }
  57.  
  58.       foreach (QueryThread thread in threads) {
  59.         thread.Join();
  60.       }
  61.  
  62.       db.Close();
  63.  
  64.       stopwatch.Stop();
  65.  
  66.       Console.WriteLine("Elapsed: {0:0.0} s", stopwatch.Elapsed.TotalSeconds);
  67.     }
  68.  
  69.     public void TestDifferentConnections(DatabaseAccessMode accessMode, bool fillTable, int threadCount, int selectCount) {
  70.       Console.WriteLine("Testing with one connections (" + accessMode + ") and " + (fillTable ? "filled" : "empty") + " table...");
  71.  
  72.       SetUp(fillTable);
  73.  
  74.       Stopwatch stopwatch = Stopwatch.StartNew();
  75.       List<QueryThread> threads = new List<QueryThread>();
  76.       for (int x = 0; x < threadCount; x++) {
  77.         var db = DatabaseConnection.OpenFileDb(this.m_databaseFileName, accessMode);
  78.         threads.Add(QueryThread.Start(db, x, selectCount));
  79.       }
  80.  
  81.       foreach (QueryThread thread in threads) {
  82.         thread.Join();
  83.         thread.m_stmt.DatabaseConnection.Close();
  84.       }
  85.  
  86.       stopwatch.Stop();
  87.  
  88.       Console.WriteLine("Elapsed: {0:0.0} s", stopwatch.Elapsed.TotalSeconds);
  89.     }
  90.  
  91.     private class QueryThread {
  92.       private const string SELECT_SQL = "SELECT * FROM tests WHERE id = ?";
  93.  
  94.       public readonly Stmt m_stmt;
  95.  
  96.       private readonly Thread m_thread;
  97.       private readonly int m_selectCount;
  98.  
  99.       private QueryThread(DatabaseConnection db, int num, int selectCount) {
  100.         this.m_stmt = db.Compile(SELECT_SQL);
  101.         this.m_thread = new Thread(this.Run);
  102.         this.m_thread.Name = "Query Thread " + num;
  103.         this.m_selectCount = selectCount;
  104.       }
  105.  
  106.       public static QueryThread Start(DatabaseConnection db, int num, int selectCount) {
  107.         QueryThread queryThread = new QueryThread(db, num, selectCount);
  108.         queryThread.m_thread.Start();
  109.  
  110.         return queryThread;
  111.       }
  112.  
  113.       public void Join() {
  114.         this.m_thread.Join();
  115.       }
  116.  
  117.       private void Run() {
  118.         Random rnd = new Random();
  119.  
  120.         int dummy = 0;
  121.  
  122.         for (int x = 0; x < this.m_selectCount; x++) {
  123.           int id = rnd.Next(ENTRY_COUNT);
  124.          
  125.           this.m_stmt.ClearBindings();
  126.           this.m_stmt.Bind(1, id);
  127.  
  128.           foreach (ResultRow resultRow in this.m_stmt.Query()) {
  129.             for (int col = 0; col < resultRow.ColumnCount; col++) {
  130.               dummy += resultRow.GetColumnAsInt(col);
  131.             }
  132.           }
  133.         }
  134.       }
  135.     }
  136.   }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment