Skip to content

Instantly share code, notes, and snippets.

@JKamsker
Created November 14, 2024 16:52
Show Gist options
  • Save JKamsker/8c3fe9dd15f61c70abad67dbcc0e788b to your computer and use it in GitHub Desktop.
Save JKamsker/8c3fe9dd15f61c70abad67dbcc0e788b to your computer and use it in GitHub Desktop.
LiteDB repro
using System.Diagnostics;
using LiteDB;
namespace LiteDBLoadTest;
internal class Program
{
private static async Task Main(string[] args)
{
var cts = new CancellationTokenSource();
var loadTest = new LoadTest();
loadTest.RunTest(cts.Token);
Console.ReadLine();
cts.Cancel();
}
}
public class LoadTest
{
private volatile LiteDatabase _db;
public void RunTest(CancellationToken token)
{
var mapperx = new BsonMapper();
using var ms = new MemoryStream();
_db = new LiteDatabase(ms, mapperx);
// TestLoop(token);
var threads = new List<Thread>();
for (var i = 0; i < 24; i++)
{
var thread = new Thread(() => TestLoop(token));
thread.Start();
threads.Add(thread);
}
// replace db every 10 ms
while (!token.IsCancellationRequested)
{
Thread.Sleep(5);
var old_db = _db;
var mapper = new BsonMapper();
_db = new LiteDatabase(ms, mapper);
Thread.Sleep(5);
old_db.Dispose();
}
}
private void TestLoop(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
try
{
Test<C1>();
Test<C2>();
Test<C3>();
Test<C4>();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
private void Test<T>()
{
_db.GetCollection<T>();
_db.GetCollection<T>();
}
}
public class C1
{
public int ID { get; set; }
}
public class C2
{
public int ID { get; set; }
}
public class C3
{
public int ID { get; set; }
}
public class C4
{
public int ID { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment