Advertisement
uwekeim

TestLiteDBVsAccessPerformance

Apr 16th, 2019
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.42 KB | None | 0 0
  1. // Related to:
  2. // https://stackoverflow.com/questions/6470469/how-fast-is-sqlite-compared-to-microsoft-access-mdb
  3.  
  4. namespace TestLiteDBVsAccessPerformance
  5. {
  6.     using LiteDB;
  7.     using System;
  8.     using System.Data.OleDb;
  9.     using System.Diagnostics;
  10.     using System.IO;
  11.     using System.Reflection;
  12.  
  13.     internal static class Program
  14.     {
  15.         private const int loopCount = 1000;
  16.  
  17.         private static void Main()
  18.         {
  19.             Console.WriteLine("Starting tests.");
  20.             Console.WriteLine();
  21.  
  22.             // Einmalig löschen.
  23.             var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"MyData.db");
  24.             if (File.Exists(path)) File.Delete(path);
  25.  
  26.             testMdbWrite();
  27.             testLiteDBWrite();
  28.             testLiteDBWriteInMemory();
  29.  
  30.             testMdbRead();
  31.             testLiteDBRead();
  32.             testLiteDBReadInMemory();
  33.  
  34.             Console.WriteLine();
  35.             Console.WriteLine("Tests finished.");
  36.         }
  37.  
  38.         private static void testMdbWrite()
  39.         {
  40.             var sw = new Stopwatch();
  41.             sw.Start();
  42.  
  43.             var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"MyData.mdb");
  44.             //Console.WriteLine($"Using Access at '{path}'.");
  45.  
  46.             if (!File.Exists(path))
  47.             {
  48.                 File.WriteAllBytes(path, Resources.MyData);
  49.             }
  50.  
  51.             // create connection and command
  52.             using (var cn = new OleDbConnection($@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={path}"))
  53.             {
  54.                 const string query =
  55.                     @"INSERT INTO Customers ([Name], Phones, IsActive) VALUES (@Name, @Phones, @IsActive)";
  56.  
  57.                 for (var i = 0; i < loopCount; ++i)
  58.                 {
  59.                     // Create your new customer instance
  60.                     var customer = new Customer
  61.                     {
  62.                         Name = $"John Doe {i + 1}",
  63.                         Phones = new[] { $"8000-0000 {i + 1}", $"9000-0000 {i + 1}" },
  64.                         IsActive = true
  65.                     };
  66.  
  67.                     using (var cmd = new OleDbCommand(query, cn))
  68.                     {
  69.                         // define parameters and their values
  70.                         cmd.Parameters.Add(new OleDbParameter(@"Name", customer.Name));
  71.                         cmd.Parameters.Add(new OleDbParameter(@"Phones", string.Join(@",", customer.Phones)));
  72.                         cmd.Parameters.Add(new OleDbParameter(@"IsActive", customer.IsActive));
  73.  
  74.                         cn.Open();
  75.                         cmd.ExecuteNonQuery();
  76.                         cn.Close();
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             sw.Stop();
  82.  
  83.             Console.WriteLine($"Access             of {loopCount} WRITE iterations took {sw.Elapsed}.");
  84.         }
  85.  
  86.         private static void testMdbRead()
  87.         {
  88.             var sw = new Stopwatch();
  89.             sw.Start();
  90.  
  91.             var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"MyData.mdb");
  92.             //Console.WriteLine($"Using Access at '{path}'.");
  93.  
  94.             if (!File.Exists(path))
  95.             {
  96.                 File.WriteAllBytes(path, Resources.MyData);
  97.             }
  98.  
  99.             // create connection and command
  100.             using (var cn = new OleDbConnection($@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={path}"))
  101.             {
  102.                 const string query = @"SELECT * FROM Customers WHERE [Name] like 'Jo%'";
  103.  
  104.                 for (var i = 0; i < loopCount; ++i)
  105.                 {
  106.                     using (var cmd = new OleDbCommand(query, cn))
  107.                     {
  108.                         cn.Open();
  109.                         cmd.ExecuteNonQuery();
  110.                         cn.Close();
  111.                     }
  112.                 }
  113.             }
  114.  
  115.             sw.Stop();
  116.  
  117.             Console.WriteLine($"Access             of {loopCount} READ  iterations took {sw.Elapsed}.");
  118.         }
  119.  
  120.         private static void testLiteDBWrite()
  121.         {
  122.             var sw = new Stopwatch();
  123.             sw.Start();
  124.  
  125.             var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"MyData.db");
  126.             //if (File.Exists(path)) File.Delete(path);
  127.  
  128.             //Console.WriteLine($"Using LiteDB at '{path}'.");
  129.  
  130.             // Open database (or create if doesn't exist)
  131.             using (var db = new LiteDatabase(path))
  132.             {
  133.                 // Get a collection (or create, if doesn't exist)
  134.                 var col = db.GetCollection<Customer>(@"customers");
  135.  
  136.                 for (var i = 0; i < loopCount; ++i)
  137.                 {
  138.                     // Create your new customer instance
  139.                     var customer = new Customer
  140.                     {
  141.                         Name = $"John Doe {i + 1}",
  142.                         Phones = new[] { $"8000-0000 {i + 1}", $"9000-0000 {i + 1}" },
  143.                         IsActive = true
  144.                     };
  145.  
  146.                     // Insert new customer document (Id will be auto-incremented)
  147.                     col.Insert(customer);
  148.  
  149.                     //// Update a document inside a collection
  150.                     //customer.Name = "Joana Doe";
  151.  
  152.                     col.Update(customer);
  153.                 }
  154.  
  155.                 // Index document using document Name property
  156.                 col.EnsureIndex(x => x.Name);
  157.  
  158.                 // Use LINQ to query documents
  159.                 //var results = col.Find(x => x.Name.StartsWith("Jo"));
  160.             }
  161.  
  162.             sw.Stop();
  163.  
  164.             Console.WriteLine($"LiteDB             of {loopCount} WRITE iterations took {sw.Elapsed}.");
  165.         }
  166.  
  167.         private static void testLiteDBWriteInMemory()
  168.         {
  169.             var sw = new Stopwatch();
  170.             sw.Start();
  171.  
  172.             var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"MyData.db");
  173.             //if (File.Exists(path)) File.Delete(path);
  174.  
  175.             //Console.WriteLine($"Using LiteDB at '{path}'.");
  176.  
  177.             // Open database (or create if doesn't exist)
  178.             using (var mem = readStream(path))
  179.             {
  180.                 using (var db = new LiteDatabase(mem))
  181.                 {
  182.                     // Get a collection (or create, if doesn't exist)
  183.                     var col = db.GetCollection<Customer>(@"customers");
  184.  
  185.                     for (var i = 0; i < loopCount; ++i)
  186.                     {
  187.                         // Create your new customer instance
  188.                         var customer = new Customer
  189.                         {
  190.                             Name = $"John Doe {i + 1}",
  191.                             Phones = new[] { $"8000-0000 {i + 1}", $"9000-0000 {i + 1}" },
  192.                             IsActive = true
  193.                         };
  194.  
  195.                         // Insert new customer document (Id will be auto-incremented)
  196.                         col.Insert(customer);
  197.  
  198.                         //// Update a document inside a collection
  199.                         //customer.Name = "Joana Doe";
  200.  
  201.                         col.Update(customer);
  202.                     }
  203.  
  204.                     // Index document using document Name property
  205.                     col.EnsureIndex(x => x.Name);
  206.  
  207.                     // Use LINQ to query documents
  208.                     //var results = col.Find(x => x.Name.StartsWith("Jo"));
  209.                 }
  210.  
  211.                 File.WriteAllBytes(path, mem.ToArray());
  212.             }
  213.  
  214.             sw.Stop();
  215.  
  216.             Console.WriteLine($"LiteDB (in-memory) of {loopCount} WRITE iterations took {sw.Elapsed}.");
  217.         }
  218.  
  219.         private static void testLiteDBRead()
  220.         {
  221.             var sw = new Stopwatch();
  222.             sw.Start();
  223.  
  224.             var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"MyData.db");
  225.             //if (File.Exists(path)) File.Delete(path);
  226.  
  227.             //Console.WriteLine($"Using LiteDB at '{path}'.");
  228.  
  229.             // Open database (or create if doesn't exist)
  230.             using (var db = new LiteDatabase(path))
  231.             {
  232.                 // Get a collection (or create, if doesn't exist)
  233.                 var col = db.GetCollection<Customer>(@"customers");
  234.  
  235.                 for (var i = 0; i < loopCount; ++i)
  236.                 {
  237.                     var results = col.Find(x => x.Name.StartsWith("Jo"));
  238.                     Debug.Assert(results != null);
  239.                 }
  240.             }
  241.  
  242.             sw.Stop();
  243.  
  244.             Console.WriteLine($"LiteDB             of {loopCount} READ  iterations took {sw.Elapsed}.");
  245.         }
  246.  
  247.         private static void testLiteDBReadInMemory()
  248.         {
  249.             var sw = new Stopwatch();
  250.             sw.Start();
  251.  
  252.             var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), @"MyData.db");
  253.             //if (File.Exists(path)) File.Delete(path);
  254.  
  255.             //Console.WriteLine($"Using LiteDB at '{path}'.");
  256.  
  257.             // Open database (or create if doesn't exist)
  258.             using (var mem = readStream(path))
  259.             {
  260.                 using (var db = new LiteDatabase(mem))
  261.                 {
  262.                     // Get a collection (or create, if doesn't exist)
  263.                     var col = db.GetCollection<Customer>(@"customers");
  264.  
  265.                     for (var i = 0; i < loopCount; ++i)
  266.                     {
  267.                         var results = col.Find(x => x.Name.StartsWith("Jo"));
  268.                         Debug.Assert(results != null);
  269.                     }
  270.                 }
  271.  
  272.                 File.WriteAllBytes(path, mem.ToArray());
  273.             }
  274.  
  275.             sw.Stop();
  276.  
  277.             Console.WriteLine($"LiteDB (in-memory) of {loopCount} READ  iterations took {sw.Elapsed}.");
  278.         }
  279.  
  280.         private static MemoryStream readStream(string path)
  281.         {
  282.             using (var temp = new MemoryStream(File.ReadAllBytes(path)))
  283.             {
  284.                 var ms = new MemoryStream();
  285.                 temp.CopyTo(ms);
  286.  
  287.                 return ms;
  288.             }
  289.         }
  290.  
  291.         // Create your POCO class entity
  292.         public class Customer
  293.         {
  294.             public int Id { get; set; }
  295.             public string Name { get; set; }
  296.             public string[] Phones { get; set; }
  297.             public bool IsActive { get; set; }
  298.         }
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement