public PingUnitResult Start() { this.Caches = new List <PingResult>(); for (int i = 0; i < this.Args.Count; i++) { long start = DateTime.Now.Ticks; var item = PingHelper.Send(this.Args.Address, this.Args.Port, this.Args.PingType, this.Args.Timeout); this.Caches.Add(item); long spent = (DateTime.Now.Ticks - start) / 10000; if (item == null || item.Status != System.Net.NetworkInformation.IPStatus.Success) { if (item == null) { Console.WriteLine("Timeout."); } else { Console.WriteLine(item.Status); } } else { Console.WriteLine("From {0} {1}: {2}ms", item.Address, this.Args.PingType, item.RoundtripTime); } int sleepTimeout = this.Args.Interval - (int)spent; if (sleepTimeout > 0) { Thread.Sleep(sleepTimeout); } } var ur = PingUnitResult.GetResult(this.Args.Address, this.Caches, this.Args.PingType); return(ur); }
public static PingUnitResult GetResult(IPAddress address, IEnumerable <PingResult> results, PingTypes pingType) { PingUnitResult result = new PingUnitResult() { Address = address, PingType = pingType, }; if (results == null || !results.Any()) { result.LostPercent = 1; return(result); } var successes = results.Where(m => m != null && m.Status == IPStatus.Success); result.SentCount = results.Count(); result.ReceivedCount = successes.Count(); result.LostPercent = (float)(result.SentCount - result.ReceivedCount) / (float)result.SentCount; if (successes != null && successes.Any()) { var ascResults = successes.OrderBy(m => m.RoundtripTime); var minItem = ascResults.FirstOrDefault(); var maxItem = ascResults.LastOrDefault(); result.MinRoundtripTime = minItem.RoundtripTime; result.MaxRoundtripTime = maxItem.RoundtripTime; result.AverageRoundtripTime = successes.Average(m => m.RoundtripTime); } return(result); }