public void Given_2_Aggregators_GetLatest_Called_Once_For_Each_Aggregator()
        {
            var fakeAggregator1 = new Mock<IItemAggregator>(MockBehavior.Loose);
            var fakeAggregator2 = new Mock<IItemAggregator>(MockBehavior.Loose);

            var streamAggregator = new StreamAggregator(fakeAggregator1.Object, fakeAggregator2.Object);

            var items = streamAggregator.GetLatest(DateTime.MinValue);

            fakeAggregator1.Verify(a => a.GetLatest(DateTime.MinValue), Times.Once());
            fakeAggregator2.Verify(a => a.GetLatest(DateTime.MinValue), Times.Once());
        }
示例#2
0
        public void Given_2_Aggregators_GetLatest_Called_Once_For_Each_Aggregator()
        {
            var fakeAggregator1 = new Mock <IItemAggregator>(MockBehavior.Loose);
            var fakeAggregator2 = new Mock <IItemAggregator>(MockBehavior.Loose);

            var streamAggregator = new StreamAggregator(fakeAggregator1.Object, fakeAggregator2.Object);

            var items = streamAggregator.GetLatest(DateTime.MinValue);

            fakeAggregator1.Verify(a => a.GetLatest(DateTime.MinValue), Times.Once());
            fakeAggregator2.Verify(a => a.GetLatest(DateTime.MinValue), Times.Once());
        }
        // All IAggregator implementations receive un-aggregated raw measurements (later raw-data) from workers.
        //  - (TODO: More info in IAggregator customization, But IAggregator ir pretty self explanatory anyway :))
        // Main idea behind Raw-Data is that one gets un-aggregated measurements in IResult form
        // and one can do anything with it, preferably post-load-test.
        //
        //
        // Having this unlocks few advantages:
        // * Save this raw-data somewhere(file on disk, array in memory) and do any aggregations after the test.
        //   - I personally persist raw-data from all of the tests I do,
        //     because one will never know what aggregation might be needed until one runs the test and sees initial results.
        //   - There is a RnD JsonStreamAggregator which saves to JSON array file. Though it can take up some space on disk as JSON has lots of overhead.
        // * Or one can write own custom specialized aggregation ignoring provided HistogramAggregator totally.
        //
        //
        // This demo contains:
        // * Load-test with one aggregation:
        //    - [Tools nuget: JsonStreamAggregator] Write data to raw.json file for later aggregation
        // * Replay of raw measurements after the test and do some more aggregating post-load-test.
        //    - Two histogram aggregators: Timeline and KPI.
        //    - One StreamAggregator instance which gives IEnumerable of IResult's for custom inline aggregation.

        public static void Run()
        {
            // JsonStreamAggregator dumps everything it receives to file.
            JsonStreamAggregator jsonAggregator = new JsonStreamAggregator("raw.json");


            StrategyBuilder builder = new StrategyBuilder()
                                      .AddLimit(new TimeLimit(TimeSpan.FromSeconds(4)))
                                      .SetThreading(new FixedThreadCount(4))
                                      .SetScenario(new SleepingScenarioFactory(TimeSpan.FromMilliseconds(100))) // Use scenario from common which sleeps every iteration
                                      .SetAggregator(jsonAggregator);                                           // Register JsonAggregator to be only one to receive results

            // A quick run
            builder.Build().Run();


            // Now lets do actual aggregation post testing.
            // First - just configure HistogramAggregator or any other aggregator in same way one would do for the live test.

            HistogramAggregator histogramTimeline = new HistogramAggregator()
                                                    .Add(new TimeDimension(TimeSpan.FromSeconds(1)))
                                                    .Add(new CountMetric())
                                                    .Add(new GlobalTimerMinValueMetric())
                                                    .Add(new GlobalTimerMaxValueMetric())
                                                    .Add(new GlobalTimerPeriodMetric());

            HistogramAggregator histogramKpi = new HistogramAggregator()
                                               .Add(new CountMetric())
                                               .Add(new GlobalTimerMinValueMetric())
                                               .Add(new GlobalTimerMaxValueMetric())
                                               .Add(new GlobalTimerPeriodMetric());

            // Lets define another more specialized aggregation just for lols.
            TimeSpan         lastIterationEnded = TimeSpan.Zero;
            StreamAggregator streamAggregator   = new StreamAggregator(
                results =>
            {
                lastIterationEnded = results.Max(r => r.IterationFinished);
            }
                );

            // now just replay saved raw-data stream to those later-defined aggregations.
            JsonStreamAggregator.Replay("raw.json", histogramTimeline, histogramKpi, streamAggregator);

            Console.WriteLine($"StreamAggregator, last iteration  finished: {lastIterationEnded:g}");
            Console.WriteLine("---------------- Histogram KPI -----------");
            Console.WriteLine(JsonConvert.SerializeObject(histogramKpi.BuildResultsObjects(), Formatting.Indented));
            Console.WriteLine("---------------- Histogram timeline -----------");
            Console.WriteLine(JsonConvert.SerializeObject(histogramTimeline.BuildResultsObjects(), Formatting.Indented));
        }
示例#4
0
        // ***
        private static void AggregateFromMultipleSources()
        {
            IEnumerable <ReplayResult <object> > pc1    = JsonStreamAggregator.Load <object>("masterdata.json");
            IEnumerable <ReplayResult <object> > pc2    = JsonStreamAggregator.Load <object>("masterdata.json");
            IEnumerable <ReplayResult <object> > merged = pc1.Concat(pc2);

            HistogramAggregator aggregator = new HistogramAggregator()
                                             .Add(new CountMetric())
                                             .Add(new TransactionsPerSecMetric());

            StreamAggregator.Replay(merged);

            Console.WriteLine("## Merged demo ##");
            Console.WriteLine(JsonConvert.SerializeObject(aggregator.BuildResultsObjects(), Formatting.Indented));
            Console.ReadKey();
        }
        public void Given_2_Aggregators_GetLatest_Returns_2_Sorted_Items_Ascending_By_Date()
        {
            var yesterday = DateTime.UtcNow.AddDays(-1);
            var beforeYesterday = DateTime.UtcNow.AddDays(-2);

            var fakeAggregator1 = new Mock<IItemAggregator>();
            fakeAggregator1.Setup(a => a.GetLatest(It.IsAny<DateTime>())).Returns(this.BuildItems(yesterday));
            var fakeAggregator2 = new Mock<IItemAggregator>();
            fakeAggregator2.Setup(a => a.GetLatest(It.IsAny<DateTime>())).Returns(this.BuildItems(beforeYesterday));

            var streamAggregator = new StreamAggregator(fakeAggregator1.Object, fakeAggregator2.Object);

            var items = streamAggregator.GetLatest(DateTime.MinValue).ToList();

            Assert.AreEqual(items.Count, 2);
            Assert.AreEqual(items[0].Published, beforeYesterday);
            Assert.AreEqual(items[1].Published, yesterday);
        }
示例#6
0
        public void Given_2_Aggregators_GetLatest_Returns_2_Sorted_Items_Ascending_By_Date()
        {
            var yesterday       = DateTime.UtcNow.AddDays(-1);
            var beforeYesterday = DateTime.UtcNow.AddDays(-2);

            var fakeAggregator1 = new Mock <IItemAggregator>();

            fakeAggregator1.Setup(a => a.GetLatest(It.IsAny <DateTime>())).Returns(this.BuildItems(yesterday));
            var fakeAggregator2 = new Mock <IItemAggregator>();

            fakeAggregator2.Setup(a => a.GetLatest(It.IsAny <DateTime>())).Returns(this.BuildItems(beforeYesterday));

            var streamAggregator = new StreamAggregator(fakeAggregator1.Object, fakeAggregator2.Object);

            var items = streamAggregator.GetLatest(DateTime.MinValue).ToList();

            Assert.AreEqual(items.Count, 2);
            Assert.AreEqual(items[0].Published, beforeYesterday);
            Assert.AreEqual(items[1].Published, yesterday);
        }
示例#7
0
        public static void Run()
        {
            HistogramAggregator aggregator = new HistogramAggregator()
                                             .Add(new CountMetric())
                                             .Add(new FuncMetric <TimeSpan>("max", TimeSpan.Zero, (s, result) => s < result.IterationStarted ? result.IterationStarted : s));

            AssertIterationIdsAggregator idsValidator = new AssertIterationIdsAggregator();

            int streamCount = 0;
            StreamAggregator streamAggregator = new StreamAggregator(results => streamCount = results.Count());

            CountingScenarioFactory factory = new CountingScenarioFactory();

            StrategyBuilder strategy = new StrategyBuilder()
                                       .SetScenario(factory)
                                       .SetThreading(new FixedThreadCount(16))
                                       .SetLimit(new TimeLimit(TimeSpan.FromSeconds(12)))
                                       .SetAggregator(aggregator, idsValidator, streamAggregator);


            Stopwatch sw = new Stopwatch();

            sw.Start();
            strategy.Build().Run();
            sw.Stop();

            Console.WriteLine(JsonConvert.SerializeObject(aggregator.BuildResultsObjects(), Formatting.Indented));
            factory.PrintSum();
            int      expected      = factory.GetSum();
            int      actual        = (int)aggregator.BuildResults().Data[0][1];
            TimeSpan lastIteration = (TimeSpan)aggregator.BuildResults().Data[0][3];

            Console.WriteLine($@"TPS {expected / lastIteration.TotalSeconds:N}");
            Console.WriteLine($@"Last iteration ended at: {lastIteration:g}");
            Console.WriteLine($@"Aggregator catchup took: {(sw.Elapsed-lastIteration):g}");
            Console.WriteLine();
            Console.WriteLine($@"{expected}/{actual} & {streamCount} ? {expected==actual} && {expected==streamCount}");
            Console.WriteLine();
            idsValidator.PrintResults();
        }
        public static void Run()
        {
            IEnumerable <IResult> rawResults       = null;
            StreamAggregator      streamAggregator = new StreamAggregator(results => rawResults = results);

            HistogramAggregator aggregator = CreateAggregator();

            HistogramAggregator aggregagorOriginal = CreateAggregator();

            StrategyBuilder strategy = new StrategyBuilder()
                                       .SetScenario <BlankScenario>()
                                       .SetThreading(new FixedThreadCount(3))
                                       .SetLimit(new TimeLimit(TimeSpan.FromSeconds(5)))
                                       .SetAggregator(streamAggregator, aggregagorOriginal);

            strategy.Build().Run();

            StreamAggregator.Replay(rawResults, aggregator);

            Console.WriteLine(@"-------- FROM LIVE AGGREGATION --------");
            Console.WriteLine(JsonConvert.SerializeObject(aggregagorOriginal.BuildResultsObjects(), Formatting.Indented));
            Console.WriteLine(@"-------- FROM STREAM --------");
            Console.WriteLine(JsonConvert.SerializeObject(aggregator.BuildResultsObjects(), Formatting.Indented));
        }
示例#9
0
 public static void Replay(this IEnumerable<IResult> results, params IAggregator[] agregators)
 {
     StreamAggregator.Replay(results, agregators);
 }
示例#10
0
 public ConfigStorageProxy(Stream fileStorage)
 {
     _fileStorage      = fileStorage;
     _streamAggregator = new StreamAggregator(_fileStorage);
 }
示例#11
0
        public static void Replay <TUserData>(string jsonResultsFile, params IAggregator[] targetAggregators)
        {
            IEnumerable <IResult> resultsStream = Load <TUserData>(jsonResultsFile);

            StreamAggregator.Replay(resultsStream, targetAggregators);
        }