Beispiel #1
0
        public void AverageLatency_PingCountIsThree()
        {
            //Assign
            Statistic stats   = new Statistic();
            Response  result1 = new Response()
            {
                AddressOrIp = "www.google.com",
                Latency     = 0,
                Status      = Response.StatusMessage.SUCCESS,
            };
            Response result2 = new Response()
            {
                AddressOrIp = "www.google.com",
                Latency     = 50,
                Status      = Response.StatusMessage.SUCCESS,
            };
            Response result3 = new Response()
            {
                AddressOrIp = "www.google.com",
                Latency     = 100,
                Status      = Response.StatusMessage.SUCCESS,
            };

            double expectedResult = 50;
            double actualResult;

            //act
            stats.Add(result1);
            stats.Add(result2);
            stats.Add(result3);
            actualResult = stats.AverageLatency;

            //assert
            Assert.AreEqual(expectedResult, actualResult);
        }
Beispiel #2
0
        public void Add_ResultStatusFailureAndSuccess()
        {
            //Assign
            Statistic stats  = new Statistic();
            Response  result = new Response()
            {
                AddressOrIp = "www.google.com",
                Latency     = 1,
                Status      = Response.StatusMessage.FAILURE,
            };
            Response result2 = new Response()
            {
                AddressOrIp = "www.google.com",
                Latency     = 5,
                Status      = Response.StatusMessage.SUCCESS,
            };
            bool expectedResult = true;
            bool actualResult;

            //act
            stats.Add(result);
            stats.Add(result2);
            actualResult = (stats.MaxLatency == 5 && stats.PacketsLost == 1 && stats.PacketsSent == 2 && stats.AverageLatency == 5);

            //assert
            Assert.AreEqual(expectedResult, actualResult);
        }
Beispiel #3
0
        void Run()
        {
            var tmp = new List <StatItem>();
            var r   = new Random(1);

            while (true)
            {
                tmp.Clear();
                Statistic.Clear();
                tmp = MonitorManager.GetCurrencyStatictic(TickPeriod.ToInt(), ConnectionString);

                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.RUB));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.USD));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.EUR));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.CHF));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.JPY));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.GBP));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.CAD));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.CNY));
                Statistic.Add(GetByCurr(tmp, CurrencyTypeEnum.BRL));

                ClearStatServer();

                StatServer = MonitorManager.GetServerStatictic(TickPeriod.ToInt(), ConnectionString);

                RiseShowValueAndPriceEvent();
                Thread.Sleep(TickPeriod.ToInt());
            }
        }
Beispiel #4
0
        public void Condition_states_serialize_correctly()
        {
            var stats = new Statistic();

            stats.Add(new EnabledCondition {
                ID = 17
            }, true);
            stats.Add(new DisabledCondition {
                ID = 13
            }, false);

            var token      = JObject.Parse(JsonConvert.SerializeObject(stats));
            var conditions = token[nameof(stats.ConditionStates)].ToObject <Dictionary <int, bool> >();

            conditions.ShouldBe(new Dictionary <int, bool>
            {
                { 17, true },
                { 13, false }
            });
        }
Beispiel #5
0
        protected override void RunCore()
        {
            if (!RunAndCatch(() => { var inst = Instance; }, "constructor"))
            {
                return;
            }

            // Try creating a delegate before starting any Stopwatch, so that for
            // benchmarking purposes we avoid measuring the cost of reflection.
            Action invokeTest = null;

            if (Method.ReturnType == typeof(void))
            {
                try {
                    invokeTest = (Action)Delegate.CreateDelegate(typeof(Action), Instance, Method);
                } catch { }
            }

            // TODO: Setup method

            object    result   = null;
            Statistic runTimes = null;
            TimeSpan  runTime;

            if (MinTrials.HasValue || RepeatForMs.HasValue)
            {
                int minTrials   = MinTrials ?? 0;
                int repeatForMs = RepeatForMs ?? 0;
                int trial       = 0;
                runTimes = new Statistic();
                SimpleTimer timer = new SimpleTimer();
                do
                {
                    result = RunOnce(invokeTest, out runTime);
                    runTimes.Add(runTime.TotalMilliseconds);
                } while (++trial < minTrials && timer.Millisec < repeatForMs);
                RunTime = TimeSpan.FromMilliseconds(timer.Millisec);
            }
            else
            {
                result  = RunOnce(invokeTest, out runTime);
                RunTime = runTime;
            }

            if (Status == TestStatus.Running)
            {
                Summary = BuildSummary(result, runTimes);
            }

            // TODO: Teardown method
        }
Beispiel #6
0
        public void Add_AddressResultIsNull()
        {
            //Assign
            Statistic stats  = new Statistic();
            Response  result = new Response()
            {
                AddressOrIp = null,
            };
            bool expectedResult = true;
            bool actualResult;

            //act

            stats.Add(result);
            actualResult = (stats.MaxLatency == 0 && stats.PacketsLost == 0 && stats.PacketsSent == 0 && stats.AverageLatency == 0);

            //assert
            Assert.AreEqual(expectedResult, actualResult);
        }
Beispiel #7
0
        public void Add_LatencyResultIsNegative()
        {
            //Assign
            Statistic stats  = new Statistic();
            Response  result = new Response()
            {
                AddressOrIp = "www.google.com",
                Status      = Response.StatusMessage.SUCCESS,
                Latency     = -1,
            };
            bool expectedResult = true;
            bool actualResult;

            //act
            stats.Add(result);
            actualResult = (stats.MaxLatency == 0 && stats.PacketsLost == 0 && stats.PacketsSent == 1 && stats.AverageLatency == 1);

            //assert
            Assert.AreEqual(expectedResult, actualResult);
        }
Beispiel #8
0
        public void loadFromFile()
        {
            if (!Directory.Exists(logPath_))
            {
                return;
            }

            // 总流量统计文件
            ///
            /// 文件结构
            /// LastUpdate [date] [time]
            /// UP [readable string] [amount]
            /// DOWN [readable string] [amount]
            /// 每行每个数据空格分隔
            ///
            var overallPath = Path.Combine(logPath_, Global.StatisticLogOverall);

            if (File.Exists(overallPath))
            {
                try
                {
                    using (var overallReader = new StreamReader(overallPath))
                    {
                        while (!overallReader.EndOfStream)
                        {
                            var line = overallReader.ReadLine();
                            if (line.StartsWith("LastUpdate"))
                            {
                            }
                            else if (line.StartsWith("UP"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 3)
                                {
                                    return;
                                }
                                TotalUp = ulong.Parse(datas[2]);
                            }
                            else if (line.StartsWith("DOWN"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 3)
                                {
                                    return;
                                }
                                TotalDown = ulong.Parse(datas[2]);
                            }
                            else if (line.StartsWith("*"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 6)
                                {
                                    return;
                                }
                                var name      = datas[1];
                                var address   = datas[2];
                                var port      = int.Parse(datas[3]);
                                var path      = datas[4];
                                var totalUp   = ulong.Parse(datas[5]);
                                var totalDown = ulong.Parse(datas[6]);

                                var index = Statistic.FindIndex(item => item.address == address && item.port == port);
                                if (index != -1)
                                {
                                    Statistic[index].totalUp   = totalUp;
                                    Statistic[index].totalDown = totalDown;
                                }
                                else
                                {
                                    var s = new Mode.ServerStatistics(name, address, port, path, totalUp, totalDown, 0, 0);
                                    Statistic.Add(s);
                                }
                            }
                        }
                    }
                }
                catch { }
            }

            // 当天流量记录文件
            var dailyPath = Path.Combine(logPath_, $"{DateTime.Now.ToLongDateString()}.txt");

            if (File.Exists(dailyPath))
            {
                try
                {
                    using (var dailyReader = new StreamReader(dailyPath))
                    {
                        while (!dailyReader.EndOfStream)
                        {
                            var line = dailyReader.ReadLine();
                            if (line.StartsWith("LastUpdate"))
                            {
                            }
                            else if (line.StartsWith("*"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 6)
                                {
                                    return;
                                }
                                var name      = datas[1];
                                var address   = datas[2];
                                var port      = int.Parse(datas[3]);
                                var path      = datas[4];
                                var todayUp   = ulong.Parse(datas[5]);
                                var todayDown = ulong.Parse(datas[6]);

                                var index = Statistic.FindIndex(item => item.address == address && item.port == port);
                                if (index != -1)
                                {
                                    Statistic[index].todayUp   = todayUp;
                                    Statistic[index].todayDown = todayDown;
                                }
                                else
                                {
                                    var s = new Mode.ServerStatistics(name, address, port, path, 0, 0, todayUp, todayDown);
                                    Statistic.Add(s);
                                }
                            }
                        }
                    }
                }
                catch { }
            }
        }
Beispiel #9
0
        public void LoadFromFile()
        {
            if (!Directory.Exists(logPath_))
            {
                return;
            }

            // 总流量统计文件
            ///
            /// 文件结构
            /// LastUpdate [date] [time]
            /// UP [readable string] [amount]
            /// DOWN [readable string] [amount]
            /// 每行每个数据空格分隔

            try
            {
                Utils.SaveLog(logPath_ + Global.StatisticLogOverall);
                var overallPath = Path.Combine(logPath_, Global.StatisticLogOverall);
                if (File.Exists(overallPath))
                {
                    using (var overallReader = new StreamReader(overallPath))
                    {
                        while (!overallReader.EndOfStream)
                        {
                            var line = overallReader.ReadLine();
                            if (line.StartsWith("LastUpdate"))
                            {
                            }
                            else if (line.StartsWith("UP"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 3)
                                {
                                    return;
                                }
                                TotalUp = ulong.Parse(datas[2]);
                            }
                            else if (line.StartsWith("DOWN"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 3)
                                {
                                    return;
                                }
                                TotalDown = ulong.Parse(datas[2]);
                            }
                            else if (line.StartsWith("*"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 8)
                                {
                                    return;
                                }
                                var name      = datas[1];
                                var address   = datas[2];
                                var port      = int.Parse(datas[3]);
                                var path      = datas[4];
                                var host      = datas[5];
                                var totalUp   = ulong.Parse(datas[6]);
                                var totalDown = ulong.Parse(datas[7]);

                                var temp  = new ServerStatistics(name, address, port, path, host, 0, 0, 0, 0);
                                var index = Statistic.FindIndex(item => Utils.IsIdenticalServer(item, temp));
                                if (index != -1)
                                {
                                    Statistic[index].totalUp   = totalUp;
                                    Statistic[index].totalDown = totalDown;
                                }
                                else
                                {
                                    var s = new Mode.ServerStatistics(name, address, port, path, host, totalUp, totalDown, 0, 0);
                                    Statistic.Add(s);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.SaveLog(ex.Message, ex);
            }

            try
            {
                Utils.SaveLog(logPath_ + $"{DateTime.Now.ToString("yyyy-MM-dd")}.txt");
                var dailyPath = Path.Combine(logPath_, $"{DateTime.Now.ToString("yyyy-MM-dd")}.txt");
                if (File.Exists(dailyPath))
                {
                    using (var dailyReader = new StreamReader(dailyPath))
                    {
                        while (!dailyReader.EndOfStream)
                        {
                            var line = dailyReader.ReadLine();
                            if (line.StartsWith("LastUpdate"))
                            {
                            }
                            else if (line.StartsWith("*"))
                            {
                                var datas = line.Split(' ');
                                if (datas.Length < 8)
                                {
                                    return;
                                }
                                var name      = datas[1];
                                var address   = datas[2];
                                var port      = int.Parse(datas[3]);
                                var path      = datas[4];
                                var host      = datas[5];
                                var todayUp   = ulong.Parse(datas[6]);
                                var todayDown = ulong.Parse(datas[7]);

                                var temp  = new ServerStatistics(name, address, port, path, host, 0, 0, 0, 0);
                                var index = Statistic.FindIndex(item => Utils.IsIdenticalServer(item, temp));
                                if (index != -1)
                                {
                                    Statistic[index].todayUp   = todayUp;
                                    Statistic[index].todayDown = todayDown;
                                }
                                else
                                {
                                    var s = new Mode.ServerStatistics(name, address, port, path, host, 0, 0, todayUp, todayDown);
                                    Statistic.Add(s);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.SaveLog(ex.Message, ex);
            }
        }
Beispiel #10
0
 public void ReceiveDamage(Damage dam)
 {
     life.Add(-dam.damage);
 }
Beispiel #11
0
 protected bool Report(Statistic stats, bool state)
 {
     stats.Add(this, state);
     return(state);
 }