public async Task Run() { ConsoleWrite.LineLine($"Starting {this.GetType().Name}.{nameof(Run)}."); Random rnd = new Random(); DiagnosticSourceAssembly.SubscribeDynamicInvokerInitializedListener(OnDynamicDiagnosticSourceInvokerInitialized, rnd); { ConsoleWrite.LineLine($"Kicking off the DS magic."); bool prevInit = DiagnosticSourceAssembly.IsInitialized; bool nowInit = DiagnosticSourceAssembly.EnsureInitialized(); ConsoleWrite.Line($"DiagnosticSourceAssembly-magic status: prevInit={prevInit}, nowInit={nowInit}."); } int currIteration = CurrentIteration; while (currIteration < _maxInterations) { WriteIfEnabled(_diagnosticSource, currIteration); if (currIteration == _phaseOneIterations) { PhaseOneCompletedEvent.Set(); } int sleepMillis = rnd.Next(MaxSleepMillis); if (sleepMillis == 0) { ; } else if (sleepMillis == 1) { Thread.Yield(); } else { await Task.Delay(sleepMillis - 2); } currIteration = Interlocked.Increment(ref _currentIteration); } ConsoleWrite.Line(); ConsoleWrite.Line($"Finishing {this.GetType().Name}.{nameof(Run)}."); Dispose(_diagnosticSource); ConsoleWrite.Line($"Finished {this.GetType().Name}.{nameof(Run)}."); }
public void Run() { const int MaxIterations = 2000; const int PhaseOneIterations = 500; const int ReceivedEventsVisualWidth = 100; ConsoleWrite.LineLine($"Welcome to {this.GetType().FullName} in {Process.GetCurrentProcess().ProcessName}"); ConsoleWrite.Line($"Starting {nameof(DirectDiagnosticEventsGenerator)}."); var directGenerator = new DirectDiagnosticEventsGenerator(MaxIterations, PhaseOneIterations); Task directGeneratorTask = Task.Run(directGenerator.Run); ConsoleWrite.LineLine($"Setting up {nameof(StubbedDiagnosticEventsCollector)}."); var directResultsAccumulator = new ReceivedEventsAccumulator(MaxIterations); var stubbedResultsAccumulator = new ReceivedEventsAccumulator(MaxIterations); var stubbedCollector = new StubbedDiagnosticEventsCollector(directResultsAccumulator, stubbedResultsAccumulator); { ConsoleWrite.LineLine($"Kicking off the DS magic."); bool prevInit = DiagnosticSourceAssembly.IsInitialized; bool nowInit = DiagnosticSourceAssembly.EnsureInitialized(); ConsoleWrite.Line($"DiagnosticSourceAssembly-magic status: prevInit={prevInit}, nowInit={nowInit}."); } directGenerator.PhaseOneCompletedEvent.Wait(); ConsoleWrite.LineLine($"Phase one of {nameof(directGenerator)} completed."); ConsoleWrite.LineLine($"Starting {nameof(StubbedDiagnosticEventsGenerator)}."); var stubbedGenerator = new StubbedDiagnosticEventsGenerator(MaxIterations, PhaseOneIterations); Task stubbedGeneratorTask = Task.Run(stubbedGenerator.Run); stubbedGenerator.PhaseOneCompletedEvent.Wait(); ConsoleWrite.LineLine($"Phase one of {nameof(stubbedGenerator)} completed."); ConsoleWrite.LineLine($"Starting Mock Plug-In." + $" directGen.Iteration={directGenerator.CurrentIteration}; stubbedGen.Iteration={stubbedGenerator.CurrentIteration}."); Task pulginTask = Task.Run(Plugin); pulginTask.Wait(); ConsoleWrite.LineLine($"Completed Mock Plug-In." + $" directGen.Iteration={directGenerator.CurrentIteration}; stubbedGen.Iteration={stubbedGenerator.CurrentIteration}."); ConsoleWrite.LineLine($"Waiting for event generators to finish."); Task.WaitAll(stubbedGeneratorTask, directGeneratorTask); ConsoleWrite.LineLine($"Both, {nameof(stubbedGenerator)} and {nameof(directGenerator)} finished."); ConsoleWrite.LineLine($"Summary of {nameof(stubbedResultsAccumulator)}:" + $" Received events: {stubbedResultsAccumulator.ReceivedCount}; Proportion: {stubbedResultsAccumulator.ReceivedProportion}."); ConsoleWrite.Line(Environment.NewLine + stubbedResultsAccumulator.GetReceivedVisual(ReceivedEventsVisualWidth)); ConsoleWrite.LineLine($"Summary of {nameof(directResultsAccumulator)}:" + $" Received events: {directResultsAccumulator.ReceivedCount}; Proportion: {directResultsAccumulator.ReceivedProportion}."); ConsoleWrite.Line(Environment.NewLine + directResultsAccumulator.GetReceivedVisual(ReceivedEventsVisualWidth)); ConsoleWrite.LineLine("All done. Press enter to exit."); Console.ReadLine(); ConsoleWrite.Line("Good bye."); }