Ancestry.Daisy.Program.AggregatePerformanceMeasurments.Aggregate C# (CSharp) Method

Aggregate() public method

public Aggregate ( PerformanceMeasurments other ) : AggregatePerformanceMeasurments
other PerformanceMeasurments
return AggregatePerformanceMeasurments
        public AggregatePerformanceMeasurments Aggregate(PerformanceMeasurments other)
        {
            return new AggregatePerformanceMeasurments()
            {
                ExecutionTime = ExecutionTime + other.ExecutionTime,
                OpsExecuted = OpsExecuted + other.OpsExecuted,
                StatementsExecutedByScope = StatementsExecutedByScope
                                            .Concat(other.StatementsExecutedByScope)
                                            .GroupBy(x => x.Key)
                                            .ToDictionary(x => x.Key, y => y.Sum(z => z.Value)),
                Executions = Executions + ((other is AggregatePerformanceMeasurments) ? ((AggregatePerformanceMeasurments)other).Executions : 1),
                StatementsExecuted = StatementsExecuted
                                            .Concat(other.StatementsExecuted)
                                            .GroupBy(x => x.Key)
                                            .ToDictionary(x => x.Key, x => x.Sum(y=> y.Value))
            };
        }

Usage Example

        public void ItAggregatesPerformanceMeasurements()
        {
            var agg = new AggregatePerformanceMeasurments();
            Assert.AreEqual(0, agg.Executions);

            var agg2 = agg.Aggregate(new PerformanceMeasurments() {
                ExecutionTime = 2,
                OpsExecuted = 3,
                StatementsExecutedByScope = new Dictionary<Type, int>()
                {
                    {typeof(int), 2},
                    {typeof(string), 6}
                }
            });

            Assert.AreEqual(2, agg2.ExecutionTime);
            Assert.AreEqual(3, agg2.OpsExecuted);
            Assert.AreEqual(6, agg2.StatementsExecutedByScope[typeof(string)]);
            Assert.AreEqual(1, agg2.Executions);

            var agg3 = agg2.Aggregate(new PerformanceMeasurments() {
                ExecutionTime = 2,
                OpsExecuted = 3,
                StatementsExecutedByScope = new Dictionary<Type, int>()
                {
                    {typeof(int), 2},
                    {typeof(string), 6}
                }
            });

            Assert.AreEqual(4, agg3.ExecutionTime);
            Assert.AreEqual(6, agg3.OpsExecuted);
            Assert.AreEqual(12, agg3.StatementsExecutedByScope[typeof(string)]);
            Assert.AreEqual(2, agg3.Executions);
        }
AggregatePerformanceMeasurments