public async Task TestObjTracker() { int nThreads = 10; int nIter = 100; int cnt = 0; object hardref = null; var objTracker = new ObjTracker(new PerfGraphToolWindowControl()); try { using (var cts = new CancellationTokenSource()) { using (var doneEvent = new ManualResetEventSlim()) { var tasks = new Task[nThreads]; for (int iThread = 0; iThread < nThreads; iThread++) { var tt = iThread; tasks[iThread] = Task.Run(() => { Thread.CurrentThread.Name = $"task {tt}"; for (int i = 0; i < nIter; i++) { var obj = new MyBigData(cnt); Interlocked.Increment(ref cnt); Interlocked.CompareExchange(ref hardref, obj, null); objTracker.AddObjectToTrack(obj, ObjSource.FromTest); } } ); } await Task.Run(() => Task.WaitAll(tasks)); doneEvent.Set(); } var res = objTracker.GetCounts(); cts.Cancel(); Assert.AreEqual(1, res.Item1.Count, "only 1 left in hardref"); } // Assert.AreEqual(nIter * nThreads, coll.Count, $" should be equal"); } catch (Exception ex) { LogMessage($"got exception {ex}"); throw; } //LogTestMessage($"expect {nThreads * nIter} cnt = {cnt} CollSize={coll.Count}"); //Assert.AreEqual(nIter * nThreads, cnt, $" items added should be equal"); //Assert.IsTrue(nIter * nThreads > coll.Count, $" coll count should be < because GC"); //Assert.AreEqual(1, coll.Count, "GC collected all but hardref"); }
public MainWindow() { InitializeComponent(); LoadConfig(); //IronPython. //ScriptEngine engine = Python.CreateEngine(); //engine.Execute("print('hello, world')"); myBigData = new MyBigData(config); endDate.DisplayDate = DateTime.Now; startDate.DisplayDate = DateTime.Now.Subtract(new TimeSpan(365 * 4, 0, 0, 0, 0)); endDate.SelectedDate = endDate.DisplayDate; startDate.SelectedDate = startDate.DisplayDate; if (config.MyId == 0) { MessageBox.Show("id не найден. Установите ваш id!", "Внимание", MessageBoxButton.OK, MessageBoxImage.Warning); } else { UpdateNameById(); } //var words = myBigData.RunWordsCounter("Эве"); //myBigData this.WindowState = System.Windows.WindowState.Maximized; // series.ChartLegend = new DefaultLegend(); //series.LoadLegend(); //series.ShowLegend(new LiveCharts.Dtos.CorePoint(10, 10)); //axisY.DataContext = this; //sc.se dataGrid.ItemsSource = statTable; dataGrid.IsReadOnly = true; series.Series = sc; Closing += (s, e) => { SaveConfig(); }; }
public async Task TestObjTrackerWithDisposable() { int nThreads = 10; int nIter = 1000; int cnt = 0; var rand = new Random(1); ConcurrentBag <object> hashHardRefs = new ConcurrentBag <object>(); var objTracker = new ObjTracker(new PerfGraphToolWindowControl()); try { using (var cts = new CancellationTokenSource()) { using (var doneEvent = new ManualResetEventSlim()) { var tasks = new Task[nThreads]; for (int iThread = 0; iThread < nThreads; iThread++) { tasks[iThread] = Task.Run(() => { for (int i = 0; i < nIter; i++) { var obj = new MyBigData(cnt); Interlocked.Increment(ref cnt); objTracker.AddObjectToTrack(obj, ObjSource.FromTest); if (rand.Next(100) < 50) { obj.Dispose(); } if (rand.Next(100) < 50) { hashHardRefs.Add(obj); } } } ); } await Task.Run(() => Task.WaitAll(tasks)); doneEvent.Set(); } var res = objTracker.GetCounts(); LogMessage($"Got results: live: {res.Item1.Count} Leaked: {res.Item2.Count} HardRefsCount={hashHardRefs.Count}"); foreach (var live in res.Item1) { LogMessage($" Live {live.Value,3} {live.Key}"); } foreach (var leak in res.Item2) { LogMessage($" Leaked {leak._serialNo} {leak.Descriptor}"); } cts.Cancel(); Assert.AreEqual(1, res.Item1.Count, "only 1 leaking type"); Assert.IsTrue(res.Item1.Values.First() > 1000, "# live > 1000"); Assert.IsTrue(res.Item2.Count > 1000, "# leaked > 1000"); } // Assert.AreEqual(nIter * nThreads, coll.Count, $" should be equal"); } catch (Exception ex) { LogMessage($"got exception {ex}"); throw; } //LogTestMessage($"expect {nThreads * nIter} cnt = {cnt} CollSize={coll.Count}"); //Assert.AreEqual(nIter * nThreads, cnt, $" items added should be equal"); //Assert.IsTrue(nIter * nThreads > coll.Count, $" coll count should be < because GC"); //Assert.AreEqual(1, coll.Count, "GC collected all but hardref"); }