From 4a6b62cbf4d2ac4c73dc7a6f4b118eca67cbdbae Mon Sep 17 00:00:00 2001 From: Lucas Viana Date: Mon, 2 Oct 2023 22:55:43 -0300 Subject: [PATCH] perf(memory-leaks): introduce Tasks.Texts.Observe --- Tests/YDotNet.Tests.Driver/Program.cs | 4 +- .../Tasks/Texts/Observe.cs | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 Tests/YDotNet.Tests.Driver/Tasks/Texts/Observe.cs diff --git a/Tests/YDotNet.Tests.Driver/Program.cs b/Tests/YDotNet.Tests.Driver/Program.cs index 072e11e1..1ded83ee 100644 --- a/Tests/YDotNet.Tests.Driver/Program.cs +++ b/Tests/YDotNet.Tests.Driver/Program.cs @@ -1,5 +1,5 @@ // See https://aka.ms/new-console-template for more information -using YDotNet.Tests.Driver.Tasks.Branches; +using YDotNet.Tests.Driver.Tasks.Texts; -new StickyIndex().Run(); +new Observe().Run(); diff --git a/Tests/YDotNet.Tests.Driver/Tasks/Texts/Observe.cs b/Tests/YDotNet.Tests.Driver/Tasks/Texts/Observe.cs new file mode 100644 index 00000000..bab4aa34 --- /dev/null +++ b/Tests/YDotNet.Tests.Driver/Tasks/Texts/Observe.cs @@ -0,0 +1,48 @@ +using YDotNet.Document; +using YDotNet.Tests.Driver.Abstractions; + +namespace YDotNet.Tests.Driver.Tasks.Texts; + +public class Observe : ITask +{ + public Task Run() + { + var count = 0; + + // Create many documents + while (count < 1_000_000) + { + // After 1s, stop and show the user the amount of documents + if (count > 0) + { + Console.WriteLine("Status Report"); + Console.WriteLine($"\tTexts:\t{count}"); + Console.WriteLine(); + } + + if (count % 1_000 == 0) + { + Thread.Sleep(millisecondsTimeout: 15); + } + + // Create many documents + for (var i = 0; i < 100; i++) + { + var doc = new Doc(); + var text = doc.Text($"sample-{i}"); + var subscription = text.Observe(_ => { }); + + var transaction = doc.WriteTransaction(); + text.Insert(transaction, index: 0, "YDotNet"); + transaction.Commit(); + + text.Unobserve(subscription); + doc.Dispose(); + + count++; + } + } + + return Task.CompletedTask; + } +}