public void CanMeasureTimeSpentInSteps() { var stats = new PipelineStepProfilerStats(); var pipeline = new DefaultPipeline() .OnReceive(new Step300()) .OnReceive(new Step100()) .OnReceive(new Step200()); var profiler = new PipelineStepProfiler(pipeline, stats); var receivePipeline = profiler.ReceivePipeline(); var invoker = new DefaultPipelineInvoker(); var transportMessage = new TransportMessage(new Dictionary<string, string>(), new byte[0]); using (new DefaultTransactionContextScope()) { var stepContext = new IncomingStepContext(transportMessage, AmbientTransactionContext.Current); invoker.Invoke(stepContext, receivePipeline).Wait(); var stepStats = stats.GetStats(); Console.WriteLine(string.Join(Environment.NewLine, stepStats)); } }
/// <summary> /// Creates the profiler /// </summary> public PipelineStepProfiler(IPipeline pipeline, PipelineStepProfilerStats profilerStats) { if (pipeline == null) throw new ArgumentNullException(nameof(pipeline)); if (profilerStats == null) throw new ArgumentNullException(nameof(profilerStats)); _pipeline = pipeline; _profilerStats = profilerStats; }
static TimeSpan RunTest(int numberOfMessages, PipelineStepProfilerStats profilerStats) { using (var adapter = new BuiltinHandlerActivator()) { var network = new InMemNetwork(); Configure.With(adapter) .Logging(l => l.Console(LogLevel.Warn)) .Transport(t => t.UseInMemoryTransport(network, "perftest")) .Options(o => { o.SetNumberOfWorkers(0); o.SetMaxParallelism(1); o.Decorate<IPipeline>(c => new PipelineStepProfiler(c.Get<IPipeline>(), profilerStats)); }) .Start(); var serializer = new JsonSerializer(); var boy = new SomeMessage("hello there!"); numberOfMessages.Times(() => { var headers = new Dictionary<string, string> { { Headers.MessageId, Guid.NewGuid().ToString() } }; var message = new Message(headers, boy); var transportMessage = serializer.Serialize(message).Result; var inMemTransportMessage = transportMessage.ToInMemTransportMessage(); network.Deliver("perftest", inMemTransportMessage); }); var numberOfReceivedMessages = 0; var gotAllMessages = new ManualResetEvent(false); adapter.Handle<SomeMessage>(async m => { numberOfReceivedMessages++; if (numberOfReceivedMessages == numberOfMessages) { gotAllMessages.Set(); } }); var stopwatch = Stopwatch.StartNew(); adapter.Bus.Advanced.Workers.SetNumberOfWorkers(1); gotAllMessages.WaitOrDie(TimeSpan.FromSeconds(30)); return stopwatch.Elapsed; } }
public void TakeTime(int numberOfMessages, int numberOfSamples) { var profilerStats = new PipelineStepProfilerStats(); var results = Enumerable.Range(1, numberOfSamples) .Select(i => { Console.Write($"Performing sample {i}: "); var result = RunTest(numberOfMessages, profilerStats); Console.WriteLine($"{result.TotalSeconds:0.#####}"); return result; }) .Select(t => t.TotalSeconds) .ToList(); Console.WriteLine($@"{numberOfSamples} runs Avg s: {results.Average():0.00###} Avg msg/s: {numberOfMessages * numberOfSamples / results.Sum():0} Stats: {string.Join(Environment.NewLine, profilerStats.GetAndResetStats().Select(s => $" {s}"))}"); }
public RegisterCollectedProfilerStatsStep(PipelineStepProfilerStats profilerStats) { _profilerStats = profilerStats; }
/// <summary> /// Creates the profiler /// </summary> public PipelineStepProfiler(IPipeline pipeline, PipelineStepProfilerStats profilerStats) { _pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline)); _profilerStats = profilerStats ?? throw new ArgumentNullException(nameof(profilerStats)); }