public void CalculateBaseStatisticsTest() { // Arrange // Create a sequence of 10 intervals with packet counts 100, 200, 300, ..., 1000 BindingList<BatchIntervalMarked> batchIntervals = new BindingList<BatchIntervalMarked>(); for (int i = 0; i < 10; i++) { BatchIntervalMarked bim = new BatchIntervalMarked(); bim.BatchIntervalId = i; bim.CaptureBatchId = 0; bim.IntervalNumber = i + 1; bim.PacketCount = (i + 1) * 100; bim.Marked = CaptureState.Unknown; batchIntervals.Add(bim); } int expectedCount = 10; int expectedMean = 550; int expectedMin = 100; int expectedMax = 1000; decimal expectedVar = 91666.667M; decimal expectedStdDev = 302.765M; // Act BaseStatistics bs = new BaseStatistics(batchIntervals); // Assert Assert.AreEqual(bs.Count, expectedCount); Assert.AreEqual(Convert.ToDecimal(bs.Mean), expectedMean); Assert.AreEqual(bs.Minimum, expectedMin); Assert.AreEqual(bs.Maximum, expectedMax); Assert.AreEqual(Convert.ToDecimal(Math.Round(bs.Variance,3)), expectedVar); Assert.AreEqual(Convert.ToDecimal(Math.Round(bs.StdDev,3)), expectedStdDev); }
public BatchStatistics CalculateBatchStatistics(BindingList<BatchIntervalMarked> batchIntervals, CaptureState captureState, BatchType batchType) { decimal batchIntervalsMean = 0; decimal batchIntervalsTrimmedMean = 0; // Trim zero packets from the batch interval BindingList<BatchIntervalMarked> batchIntervalsTrimmed = new BindingList<BatchIntervalMarked>(); foreach (BatchIntervalMarked bim in batchIntervals) { if (bim.PacketCount > AnalysisConfiguration.HistogramBinSize) { batchIntervalsTrimmed.Add(bim); } } // Calculate statistics for the batch BatchStatistics bs = new BatchStatistics(); if (AnalysisConfiguration.TrimSmallPackets) { BaseStatistics stats = new BaseStatistics(batchIntervalsTrimmed); bs.IntervalCountTrimmed = stats.Count; bs.PacketCountMaximum = stats.Maximum; bs.PacketCountMinimum = stats.Minimum; bs.PacketCountMean = stats.Mean; bs.PacketCountStandardDeviation = stats.StdDev; // Calculate both means for updating the capture batch intervals batchIntervalsTrimmedMean = stats.Mean; batchIntervalsMean = Convert.ToDecimal((from t in batchIntervals select t.PacketCount).Average()); // Get both counts for the batch bs.IntervalCountTrimmed = stats.Count < 0 ? 0 : stats.Count; bs.IntervalCount = batchIntervals.Count < 0 ? 0 : batchIntervals.Count; } else { BaseStatistics stats = new BaseStatistics(batchIntervals); bs.IntervalCount = stats.Count; bs.PacketCountMaximum = stats.Maximum; bs.PacketCountMinimum = stats.Minimum; bs.PacketCountMean = stats.Mean; bs.PacketCountStandardDeviation = stats.StdDev; // Calculate both means for updating the capture batch intervals batchIntervalsMean = bs.PacketCountMean; batchIntervalsTrimmedMean = Convert.ToDecimal((from t in batchIntervalsTrimmed select t.PacketCount).Average()); // Get both counts for the batch bs.IntervalCount = stats.Count < 0 ? 0 : stats.Count; bs.IntervalCountTrimmed = batchIntervalsTrimmed.Count < 0 ? 0 : batchIntervalsTrimmed.Count; } // Update the batch mean - only for single batches, not cumulative batches CurrentCaptureFile captureFile = new CurrentCaptureFile(); ProcessCapturePackets pcp = new ProcessCapturePackets(); captureFile = pcp.GetCurrentCaptureFile(_CaptureFileName); int captureBatchId = captureFile.CaptureBatchId; if (batchType == BatchType.Single && captureBatchId != 0) { try { //ProcessCapturePackets pcp = new ProcessCapturePackets(); //if (!pcp.UpdateBatchMean(Convert.ToInt32(captureBatchId), bs.PacketCountMean)) if (!pcp.UpdateBatchMean(Convert.ToInt32(captureBatchId), batchIntervalsMean, batchIntervalsTrimmedMean)) { throw new Exception("Error updating batch mean for CaptureBatchId " + captureBatchId); } } catch (Exception ex) { throw new Exception("Error updating batch mean for CaptureBatchId " + captureBatchId + ": " + ex.Message); } } // Save the statistics to the database for display on the Analysis tab (save to DisplayStatistics table) SaveDisplayStatistics(bs, captureBatchId, captureState, batchType, true); return bs; }