public void Profiler_CreateSection_DisposeSectionSimultaneously_ShouldWriteTraceOnce() { var traceWriter = new StubTraceWriter(); var profiler = new ProfilerConfiguration() .UseTraceWriter(() => traceWriter) .CreateProfiler(); var section = profiler.Section("section"); int threadCount = 1_000_000; var tasks = new Task[threadCount]; //var threadIds = new HashSet<int>(); for (int i = 0; i < threadCount; i++) { tasks[i] = Task.Factory.StartNew(() => { // uncomment to ensure there are several threads //lock (threadIds) //{ // threadIds.Add(Thread.CurrentThread.ManagedThreadId); //} section.Dispose(); }); } Task.WaitAll(tasks); traceWriter.Count.ShouldBe(1); }
public async Task Profiler_CreateSection_EnterSectionsSimultaneously_ExitAfterAwait_ShouldNotThrowSectionInUseException() { var profiler = new ProfilerConfiguration() .CreateProfiler(); int count = 1000; var tasks = new Task[count]; for (int i = 0; i < count; i++) { tasks[i] = await Task.Factory.StartNew(async() => { int j = i; var section = profiler.Section("section"); //Thread.Sleep(1000); - okay await Task.Delay(1000).ConfigureAwait(false); // -- error section.Dispose(); }); } await Task.WhenAll(tasks); }
static void Main(string[] args) { var logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.File(new CompactJsonFormatter(), "log.txt") .WriteTo.Console() .CreateLogger(); var profiler = new ProfilerConfiguration() .UseSerilogTraceWriter(settings => settings .UseLogEventLevel(LogEventLevel.Verbose) .UseDefaultTraceFormatter() .UseLogger(logger)) .CreateProfiler(); using (var section = profiler.Section("outer {number}", "one")) { using (var section2 = section.Section("inner {letter}", "z")) { using (section2.Section("deep {sign}", "-")) { } } } }
//[InlineData(1_000_000, 1, 1000 )] //[InlineData(1_000_000, 100, 1000 )] //[InlineData(1_000_000, 10_000, 1000 )] public async Task Profiler_CreateSections_Enter_AsyncYield_Exit(int count, int keysCount, int depth) { var reportWriter = new StubReportWriter(); var profiler = new ProfilerConfiguration() .UseReportWriter(() => reportWriter) .CreateProfiler(); string[] keys = new string[count]; for (int i = 0; i < keysCount; i++) { keys[i] = i.ToString(); } int batchSize = 100; var tasks = new List <Task>(batchSize); var stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < (count / depth) / batchSize; i++) { tasks.Add(await Task.Factory.StartNew(async() => { for (int j = 0; j < batchSize; j++) { var section = profiler.Section(keys[i % keysCount]); ISection currentSection = section; Stack <ISection> sections = new Stack <ISection>(depth - 1); sections.Push(currentSection); while (sections.Count < depth) { currentSection = currentSection.Section(keys[(i + sections.Count) % keysCount]); sections.Push(currentSection); await Task.Yield(); } while (sections.Count > 0) { await Task.Yield(); sections.Pop().Dispose(); } } })); } await Task.WhenAll(tasks); stopwatch.Stop(); string log = $"count: {count}, keysCount: {keysCount}, depth: {depth}, elapsed: {stopwatch.ElapsedMilliseconds} ms, allocated {reportWriter.Queue.Count} sections"; _output.WriteLine(log); Console.WriteLine(log); reportWriter.Queue.Sum(m => m.Count).ShouldBe(count); }
static void Main(string[] args) { var logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.File(new CompactJsonFormatter(), "report.txt") .WriteTo.Console() .CreateLogger(); var profiler = new ProfilerConfiguration() .UseSerilogReportWriter(settings => settings .UseLogEventLevel(LogEventLevel.Verbose) .UseDefaultReportFormatter() .UseLogger(logger)) .CreateProfiler(); int n = 100; var tasks = new Task[n]; for (int i = 0; i < n; i++) { tasks[i] = Task.Factory.StartNew(() => { using (var section = profiler.Section("outer {number}", "one")) { using (var section2 = section.Section("inner {letter}", "z")) { using (section2.Section("deep {sign}", "-")) { } using (section2.Section("deep {sign}", "+")) { } } using (var section2 = section.Section("inner {letter}", "x")) { using (section2.Section("deep {sign}", "-")) { } using (section2.Section("deep {sign}", "+")) { } } } }); } Task.WaitAll(tasks); profiler.WriteReport(); }
//[InlineData(1_000_000, 1, 1000, 100)] //[InlineData(1_000_000, 100, 1000, 100)] //[InlineData(1_000_000, 10_000, 1000, 100)] public void Profiler_CreateSections_Enter_Exit(int count, int keysCount, int depth, int maxDegreeOfParallelism) { var reportWriter = new StubReportWriter(); var profiler = new ProfilerConfiguration() .UseReportWriter(() => reportWriter) .CreateProfiler(); string[] keys = new string[count]; for (int i = 0; i < keysCount; i++) { keys[i] = i.ToString(); } var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }; var stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.For(0, count / depth, parallelOptions, i => //for (int i = 0; i < count / depth; i++) { var section = profiler.Section(keys[i % keysCount]); ISection currentSection = section; Stack <ISection> sections = new Stack <ISection>(depth - 1); sections.Push(currentSection); while (sections.Count < depth) { currentSection = currentSection.Section(keys[(i + sections.Count) % keysCount]); sections.Push(currentSection); } while (sections.Count > 0) { sections.Pop().Dispose(); } }); stopwatch.Stop(); string log = $"count: {count}, keysCount: {keysCount}, depth: {depth}, maxDegreeOfParallelism: {maxDegreeOfParallelism}, elapsed: {stopwatch.ElapsedMilliseconds} ms, allocated {reportWriter.Queue.Count} sections"; _output.WriteLine(log); Console.WriteLine(log); reportWriter.Queue.Sum(m => m.Count).ShouldBe(count); }
static void Main(string[] args) { var profiler = new ProfilerConfiguration() .UseStopwatchTimeMeasure() .UseConsoleTraceWriter() .UseConsoleReportWriter() .CreateProfiler(); var threads = new List <Thread>(); for (int i = 0; i < 3; i++) { var thread = new Thread(() => { TimeSpan delay = TimeSpan.FromMilliseconds(i * 100); using (var section = profiler.Section("section.{i}.{delay}", i, delay)) { Thread.Sleep(delay); for (int j = 0; j < 3; j++) { TimeSpan innerDelay = TimeSpan.FromMilliseconds(j * 10); using (section.Section("child.{j}.{innerDelay}", j, innerDelay)) { Thread.Sleep(innerDelay); } } } }); threads.Add(thread); thread.Start(); } foreach (var thread in threads) { thread.Join(); } profiler.WriteReport(); Console.ReadKey(); }
public void InitializeApplication() { try { Logger = new WhiteDefaultLoggerFactory(TestLoggerLevel).Create(typeof(TestBase)); Wait = new Waiter(); NameGenerator = new NameGenerator(); ServiceControlStub = ServiceControl.Start(); Configuration = new ProfilerConfiguration(); Application = Configuration.LaunchApplication(); MainWindow = Configuration.GetMainWindow(Application); Container = CreateContainer(); OnApplicationInitialized(); OnSetupUI(); } catch (Exception ex) { Logger.Error("Failed to launch application and get main window", ex); TryCloseApplication(); throw; } }
static async Task Main(string[] args) { var insider = new LocalInsiderConfiguration() .ConfigureDefault() .CreateInsider(); var profiler = new ProfilerConfiguration() .UseInsiderReportWriter(insider) .CreateProfiler(); insider.Run(); Console.WriteLine("Commands:"); Console.WriteLine("e - [E]xit"); Console.WriteLine("s - measure-[S]tart KEY"); Console.WriteLine("p - measure-[P]ause KEY"); Console.WriteLine("r - measure-[R]eport"); Console.WriteLine("v - state-set KEY [V]ALUE"); Console.WriteLine("b - open [B]rowser with `ui-web` page"); var sections = new Dictionary <string, ISection>(); while (true) { Console.WriteLine(); Console.WriteLine("Press one key of: 'e', 's', 'p', 'r', 'v', 'b':"); var command = Console.ReadKey().KeyChar; if (!new[] { 'e', 's', 'p', 'r', 'v', 'b' }.Contains(command)) { continue; } if (command == 'e') { break; } else if (command == 's') { Console.WriteLine(" - measure-start"); Console.Write("KEY: "); string key = Console.ReadLine(); if (!sections.ContainsKey(key)) { sections[key] = profiler.Section(key); } } else if (command == 'p') { Console.WriteLine(" - measure-pause"); Console.Write("KEY: "); string key = Console.ReadLine(); if (sections.TryGetValue(key, out ISection section)) { section.Dispose(); Console.WriteLine($"measure: {(section as ISectionMetrics).Elapsed}"); sections.Remove(key); } } else if (command == 'r') { Console.WriteLine(" - measure-report"); profiler.WriteReport(); } else if (command == 'v') { Console.WriteLine(" - state-set"); Console.Write("KEY: "); string key = Console.ReadLine(); Console.Write("VALUE: "); string value = Console.ReadLine(); insider.SetState(new[] { key }, value); } else if (command == 'b') { Browser.Open("http://localhost:8080/insider/ui-web"); } } await insider.StopAsync(); Console.ReadKey(); }