Ejemplo n.º 1
0
        private void WriteData(long sampleSize, double dataPerSecond, TimeSpan duration, double?maxSamplesToKeepPerSecond, Action <Datum, double, DataRateCalculator.SamplingAction> calculatedDataRateKeepCallback)
        {
            DataRateCalculator dataRateCalculator = new DataRateCalculator(sampleSize, maxSamplesToKeepPerSecond);

            long           numData        = (long)(dataPerSecond * duration.TotalSeconds);
            double         interDataTime  = duration.TotalSeconds / numData;
            DateTimeOffset startTimestamp = DateTimeOffset.UtcNow;

            dataRateCalculator.Start(startTimestamp);
            for (long i = 0; i < numData; ++i)
            {
                DateTimeOffset     simulatedCurrentTime = startTimestamp.AddSeconds((i + 1) * interDataTime);
                AccelerometerDatum datum = new AccelerometerDatum(simulatedCurrentTime, 1, 1, 1);
                DataRateCalculator.SamplingAction samplingAction = dataRateCalculator.Add(datum);

                double?calculatedDataPerSecond = dataRateCalculator.GetDataPerSecond();

                if (i < sampleSize - 1)
                {
                    Assert.IsNull(calculatedDataPerSecond);
                }
                else
                {
                    Assert.NotNull(calculatedDataPerSecond);
                    calculatedDataRateKeepCallback?.Invoke(datum, calculatedDataPerSecond.Value, samplingAction);
                }
            }
        }
Ejemplo n.º 2
0
        public void DropNullDatumTest()
        {
            DataRateCalculator samplingRateCalculator = new DataRateCalculator(100);

            samplingRateCalculator.Start();
            Assert.AreEqual(samplingRateCalculator.Add(null), DataRateCalculator.SamplingAction.Drop);
        }
Ejemplo n.º 3
0
        public void ImmediateDataTest()
        {
            InitServiceHelper();

            DataRateCalculator calculator = new DataRateCalculator(10, 1);
            AccelerometerDatum datum      = new AccelerometerDatum(DateTimeOffset.UtcNow, 1, 1, 1);

            calculator.Start(datum.Timestamp);
            for (int i = 0; i < calculator.SampleSize * 2; ++i)
            {
                DataRateCalculator.SamplingAction action = calculator.Add(datum);

                if (i < calculator.SampleSize)
                {
                    Assert.AreEqual(action, DataRateCalculator.SamplingAction.Keep);
                }
                else
                {
                    Assert.AreEqual(action, DataRateCalculator.SamplingAction.Drop);
                }
            }
        }
Ejemplo n.º 4
0
        public void SamplingRateTest()
        {
            InitServiceHelper();

            DataRateCalculator samplingRateCalculator = new DataRateCalculator(100);

            samplingRateCalculator.Start();

            double   nominalDataPerSecond             = 192;
            double   nominalSamplingDataRatePerSecond = 10;
            TimeSpan duration = TimeSpan.FromSeconds(100);

            WriteData(100, nominalDataPerSecond, duration, nominalSamplingDataRatePerSecond, (datum, calculatedDataPerSecond, action) =>
            {
                if (action == DataRateCalculator.SamplingAction.Keep)
                {
                    Assert.AreEqual(samplingRateCalculator.Add(datum), DataRateCalculator.SamplingAction.Keep);
                }
            });

            Assert.True(Math.Abs(samplingRateCalculator.GetDataPerSecond().Value - nominalSamplingDataRatePerSecond) <= 1);
        }