private void Assert_AppMetricsCollection_CorrectlyInitialized(IEnumerable <AppMetric> expectedMetrics,
                                                                      AppMetricsCollection metrics)
        {
            IEnumerable <AppMetricId> actualMetricIds = metrics
                                                        .ExportedMetrics;

            Assert.AreEqual(expectedMetrics.Count(),
                            actualMetricIds.Count());

            foreach (AppMetric expectedMetric in expectedMetrics)
            {
                CollectionAssert.Contains(actualMetricIds, expectedMetric.Id);
            }

            foreach (AppMetric m in expectedMetrics)
            {
                AppMetric metric = metrics.QueryMetric(m.Id);
                Assert.NotNull(metric);
                Assert.AreEqual(m.Value, metric.Value);
            }

            Assert.AreEqual(expectedMetrics.Count(), metrics
                            .CollectMetrics()
                            .Count());
        }
        private List <AppMetric> MergeAppMetrics(AppMetricsCollection[] collections)
        {
            List <AppMetric> metrics =
                new List <AppMetric>();

            foreach (AppMetricsCollection col in collections)
            {
                foreach (AppMetric metric in col.CollectMetrics())
                {
                    AppMetric existingMetric = metrics.FirstOrDefault(m
                                                                      => m.Id.Equals(metric.Id));

                    if (existingMetric != null)
                    {
                        existingMetric.Add(metric.Value);
                    }
                    else
                    {
                        metrics.Add(metric.Copy());
                    }
                }
            }

            return(metrics);
        }
Ejemplo n.º 3
0
        private async Task <List <AppMetric> > GetDbAppMetricsAsync()
        {
            List <AppMetric> dbMetrics =
                new List <AppMetric>();

            using (NpgsqlConnection conn = await OpenDbConnectionAsync(ConnectionString))
                using (NpgsqlCommand cmd = new NpgsqlCommand($"SELECT * from {TestOptions.DefaultMapping.MetricsTableName}", conn))
                {
                    using (NpgsqlDataReader rdr = await cmd.ExecuteReaderAsync())
                    {
                        while (await rdr.ReadAsync())
                        {
                            AppMetricId mId = new AppMetricId(
                                rdr.GetString(rdr.GetOrdinal("metric_id")),
                                rdr.GetString(rdr.GetOrdinal("metric_category"))
                                );

                            AppMetric metric = new AppMetric(mId,
                                                             rdr.GetInt64(rdr.GetOrdinal("metric_value")));

                            dbMetrics.Add(metric);
                        }
                    }

                    await conn.CloseAsync();
                }

            return(dbMetrics);
        }
Ejemplo n.º 4
0
        public Task Handle(ExtractSent notification, CancellationToken cancellationToken)
        {
            var metric = new AppMetric(notification.Version, notification.Name,
                                       JsonConvert.SerializeObject(notification));

            _repository.Clear(notification.Name, "NoSent");
            _repository.Create(metric);
            _repository.SaveChanges();
            return(Task.CompletedTask);
        }
Ejemplo n.º 5
0
        public void Test_CanUpdate_Parallel(int nThreads)
        {
            Faker faker = new Faker();

            Thread[] threads = new Thread[nThreads];

            List <long> threadValues =
                new List <long>(nThreads);

            ConcurrentBag <long> prevValues =
                new ConcurrentBag <long>();

            long initialValue = faker.Random
                                .Long();

            for (int i = 0; i < nThreads; i++)
            {
                threadValues.Add(faker.Random.Long());
            }

            AppMetric metric = new AppMetric(faker.PickRandom(AppMetricId.BuiltInAppMetricIds),
                                             initialValue);

            for (int i = 0; i < nThreads; i++)
            {
                long threadValue = threadValues[i];

                Thread setterThread = new Thread(()
                                                 => prevValues.Add(metric.Update(threadValue)));

                threads[i] = setterThread;
                setterThread.Start();
            }

            foreach (Thread t in threads)
            {
                t.Join();
            }

            prevValues.Add(metric.Update(faker.Random.Long()));

            Assert.AreEqual(threadValues.Count + 1,
                            prevValues.Count);

            CollectionAssert.Contains(prevValues, initialValue);
            foreach (long threadVal in threadValues)
            {
                CollectionAssert.Contains(prevValues, threadVal);
            }
        }
Ejemplo n.º 6
0
        public Task Handle(HandshakeEnd notification, CancellationToken cancellationToken)
        {
            var session = _repository.GetSession(notification.EndName);

            notification.UpdateSession(session);

            var metric = new AppMetric(notification.Version, notification.Name,
                                       JsonConvert.SerializeObject(notification));

            _repository.Clear(notification.Name);
            _repository.Create(metric);
            _repository.SaveChanges();
            return(Task.CompletedTask);
        }
Ejemplo n.º 7
0
        public static List <AppMetric> RandomAppMetrics(this Faker faker, IEnumerable <AppMetricId> forMetricIds,
                                                        long minValue = 0,
                                                        long maxValue = long.MaxValue)
        {
            List <AppMetric> metrics =
                new List <AppMetric>();

            foreach (AppMetricId mId in forMetricIds)
            {
                AppMetric newMetric = faker.RandomAppMetric(mId, minValue, maxValue);
                metrics.Add(newMetric);
            }

            return(metrics);
        }
        private void Assert_CorrectJoinQueryAppMetrics(IEnumerable <AppMetricId> checkMetricIds,
                                                       AppMetricsCollection[] collectionBatches)
        {
            foreach (AppMetricId metricId in checkMetricIds)
            {
                long expectedValue = SumMetricValues(collectionBatches,
                                                     metricId);

                AppMetric metric = AppMetricsCollection.JoinQueryMetric(metricId,
                                                                        collectionBatches);

                Assert.NotNull(metric);
                Assert.AreEqual(expectedValue, metric.Value);
            }
        }
        public void Test_CanCreateAppMetricsCollection_FromAppMetrics()
        {
            int   iMetric = 0;
            Faker faker   = new Faker();

            AppMetric[] expecteMetrics = new AppMetric[AppMetricId.BuiltInAppMetricIds.Count()];

            foreach (AppMetricId mId in AppMetricId.BuiltInAppMetricIds)
            {
                expecteMetrics[iMetric++] = new AppMetric(mId, faker.Random.Long(0));
            }

            AppMetricsCollection metrics = new AppMetricsCollection(expecteMetrics);

            Assert_AppMetricsCollection_CorrectlyInitialized(expecteMetrics, metrics);
        }
Ejemplo n.º 10
0
        public void Test_CanJoinWith()
        {
            Faker faker  = new Faker();
            long  value1 = faker.Random.Long(1, 1000);
            long  value2 = faker.Random.Long(1, 1000);

            AppMetricId metricId = faker.PickRandom(AppMetricId.BuiltInAppMetricIds);

            AppMetric metric1 = new AppMetric(metricId,
                                              value1);
            AppMetric metric2 = new AppMetric(metricId,
                                              value2);

            AppMetric metric = metric1.JoinWith(metric2);

            Assert.AreEqual(value1 + value2, metric.Value);
            Assert.AreNotSame(metric, metric1);
            Assert.AreNotSame(metric, metric2);
        }
Ejemplo n.º 11
0
        public Task Handle(ExtractLoaded notification, CancellationToken cancellationToken)
        {
            var cargoes = _repository.LoadCargo().ToList();

            if (notification.Name == "CareTreatment")
            {
                var detainedCargoes = _repository.LoadDetainedCargo().ToList();
                cargoes.AddRange(detainedCargoes);
            }

            notification.AddCargo(cargoes);

            var metric = new AppMetric(notification.Version, notification.Name,
                                       JsonConvert.SerializeObject(notification));

            _repository.Clear(notification.Name, "NoLoaded");
            _repository.Create(metric);
            _repository.SaveChanges();
            return(Task.CompletedTask);
        }
Ejemplo n.º 12
0
        public void Test_CanSetMax_Parallel(int nThreads)
        {
            Faker faker = new Faker();

            Thread[] threads = new Thread[nThreads];

            List <long> threadValues =
                new List <long>(nThreads);

            long initialValue = faker.Random
                                .Long(100, 10000);

            for (int i = 0; i < nThreads; i++)
            {
                threadValues.Add(initialValue + i + 1);
            }

            AppMetric metric = new AppMetric(faker.PickRandom(AppMetricId.BuiltInAppMetricIds),
                                             long.MinValue);

            for (int i = 0; i < nThreads; i++)
            {
                long threadValue = threadValues[i];

                Thread maxThread = new Thread(()
                                              => metric.Max(threadValue));

                threads[i] = maxThread;
                maxThread.Start();
            }

            foreach (Thread t in threads)
            {
                t.Join();
            }

            Assert.AreEqual(threadValues.Max(),
                            metric.Value);
        }
        public void Test_CanJoinQueryMetric_AbsentFromAllProviders(int nCollections)
        {
            AppMetricId[] allMetricIds = AllBuiltInMetricIds;

            foreach (AppMetricId testMetricId in allMetricIds)
            {
                AppMetricId[] metricIds = AllBuiltInMetricIds
                                          .Where(m => !m.Equals(testMetricId))
                                          .ToArray();

                List <AppMetricId>[] metricIdsBatches = metricIds
                                                        .MultiplyCollection(nCollections);

                AppMetricsCollection[] collections =
                    GetCollectionsFromMetricIdsBatchesWithCustomValues(metricIdsBatches);

                AppMetric testMetric = AppMetricsCollection.JoinQueryMetric(testMetricId,
                                                                            collections);

                Assert.IsNull(testMetric);
            }
        }
Ejemplo n.º 14
0
        public void Test_CanDecrement_Parallel(int nThreads)
        {
            Faker faker = new Faker();

            Thread[] threads = new Thread[nThreads];

            AppMetric metric = new AppMetric(faker.PickRandom(AppMetricId.BuiltInAppMetricIds),
                                             nThreads);

            for (int i = 0; i < nThreads; i++)
            {
                Thread addThread = new Thread(() => metric.Decrement());
                threads[i] = addThread;
                addThread.Start();
            }

            foreach (Thread t in threads)
            {
                t.Join();
            }

            Assert.AreEqual(0, metric.Value);
        }
Ejemplo n.º 15
0
        public void Test_CanAdd_Parallel(int nThreads)
        {
            Faker faker = new Faker();

            Thread[] threads    = new Thread[nThreads];
            long     valueToAdd = faker.Random.Long(1, 10);

            AppMetric metric = new AppMetric(faker.PickRandom(AppMetricId.BuiltInAppMetricIds),
                                             0);

            for (int i = 0; i < nThreads; i++)
            {
                Thread addThread = new Thread(() => metric.Add(valueToAdd));
                threads[i] = addThread;
                addThread.Start();
            }

            foreach (Thread t in threads)
            {
                t.Join();
            }

            Assert.AreEqual(nThreads * valueToAdd, metric.Value);
        }