Skip to content

Commit

Permalink
Poc for litedb-org#2446
Browse files Browse the repository at this point in the history
  • Loading branch information
abereznikov committed Jun 9, 2024
1 parent 610e530 commit 2d17461
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
8 changes: 7 additions & 1 deletion LiteDB.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions Poc/Poc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
</ItemGroup>

</Project>
106 changes: 106 additions & 0 deletions Poc/Program.cs
Original file line number Diff line number Diff line change
@@ -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<TestRecord>();
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<IReadOnlyCollection<TestRecord>> GenerateData()
{
var records = new List<TestRecord>(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);
}
}
}
}

0 comments on commit 2d17461

Please sign in to comment.