From 2d17461e932e380de4583d4cef011abdcedcad9e Mon Sep 17 00:00:00 2001 From: abereznikov Date: Sun, 9 Jun 2024 20:31:43 +0500 Subject: [PATCH] Poc for #2446 --- LiteDB.sln | 8 +++- Poc/Poc.csproj | 14 +++++++ Poc/Program.cs | 106 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 Poc/Poc.csproj create mode 100644 Poc/Program.cs diff --git a/LiteDB.sln b/LiteDB.sln index 3f75c194f..4bbbd5323 100644 --- a/LiteDB.sln +++ b/LiteDB.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LiteDB.Benchmarks", "LiteDB EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LiteDB.Stress", "LiteDB.Stress\LiteDB.Stress.csproj", "{FFBC5669-DA32-4907-8793-7B414279DA3B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{E8763934-E46A-4AAF-A2B5-E812016DAF84}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{E8763934-E46A-4AAF-A2B5-E812016DAF84}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Poc", "Poc\Poc.csproj", "{A9284D74-BEF8-48CA-873C-8D14C16D7870}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {E8763934-E46A-4AAF-A2B5-E812016DAF84}.Debug|Any CPU.Build.0 = Debug|Any CPU {E8763934-E46A-4AAF-A2B5-E812016DAF84}.Release|Any CPU.ActiveCfg = Release|Any CPU {E8763934-E46A-4AAF-A2B5-E812016DAF84}.Release|Any CPU.Build.0 = Release|Any CPU + {A9284D74-BEF8-48CA-873C-8D14C16D7870}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A9284D74-BEF8-48CA-873C-8D14C16D7870}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A9284D74-BEF8-48CA-873C-8D14C16D7870}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A9284D74-BEF8-48CA-873C-8D14C16D7870}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Poc/Poc.csproj b/Poc/Poc.csproj new file mode 100644 index 000000000..f4b92a821 --- /dev/null +++ b/Poc/Poc.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0-windows + enable + true + + + + + + + diff --git a/Poc/Program.cs b/Poc/Program.cs new file mode 100644 index 000000000..537d240e8 --- /dev/null +++ b/Poc/Program.cs @@ -0,0 +1,106 @@ +namespace Poc +{ + using System; + using System.Collections.Generic; + using System.Diagnostics; + using System.IO; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using System.Windows.Forms; + using LiteDB; + + public static class Program + { + private const string DbName = "test.db"; + private const string LogName = "test-log.db"; + private const int Records = 100_0000; + private const int Chunk = 1000; + + public static async Task Main(string[] args) + { + if (File.Exists(DbName)) + { + File.Delete(DbName); + } + + if (File.Exists(LogName)) + { + File.Delete(LogName); + } + + var context = new CustomContext(); + + SynchronizationContext.SetSynchronizationContext(context); + + await Task.Factory.StartNew( + () => + { + using (var db = new LiteDatabase( + new ConnectionString + { + Filename = DbName, + })) + { + var collection = db.GetCollection(); + collection.EnsureIndex(x => x.Index); + + var data = GenerateData(); + + var sw = Stopwatch.StartNew(); + + foreach (var chunk in data) + { + collection.Upsert(chunk); + } + + Console.Write(sw.Elapsed); + } + }, + CancellationToken.None, + TaskCreationOptions.None, + TaskScheduler.FromCurrentSynchronizationContext()); + } + + private static IReadOnlyCollection> GenerateData() + { + var records = new List(Records); + + for (var i = 0; i < Records; i++) + { + records.Add(new TestRecord + { + Index = i, + First = Random.Shared.Next(), + Second = Random.Shared.Next(), + }); + } + + return records.Chunk(Chunk).ToArray(); + } + + private sealed class TestRecord + { + public int Index { get; init; } + + public int First { get; init; } + + public int Second { get; init; } + } + + private sealed class CustomContext : SynchronizationContext + { + public override void Post(SendOrPostCallback d, object? state) + { + Console.WriteLine("POST {0} {1}", d.Method, new StackTrace()); + base.Post(d, state); + } + + public override void Send(SendOrPostCallback d, object? state) + { + Console.WriteLine("SEND {0} {1}", d.Method, new StackTrace()); + base.Send(d, state); + } + } + } +} \ No newline at end of file