简体   繁体   English

计算最小/最大/平均ping响应

[英]Calculating minimum/maximum/average ping reponse

So here is an application which tests each server of popular RuneScape game. 因此,这是一个测试流行的RuneScape游戏的每台服务器的应用程序。 I am running ping on each of 139 servers and add lag value to array. 我在139台服务器上的每台上运行ping并将滞后值添加到数组。 While looping through list entries I can calculate the average, minimum, and maximum lag for each server. 在遍历列表条目时,我可以计算每个服务器的平均,最小和最大延迟。

Now, in my code when one of the unreachable servers is noted the lowest lag displays as 0. I'm not sure how to handle if my ping response has reached it's timeout and posts 0 to array list. 现在,在我的代码中,当记录到一台无法访问的服务器时,最低延迟显示为0。我不确定如果ping响应已达到超时并且将0发布到数组列表,该如何处理。

What can I do to avoid that 0 as my lowest ping when server is down? 当服务器关闭时,我该怎么做才能避免将0作为最低ping?

using System;
using System.Net.NetworkInformation;
using System.Collections.Generic;

namespace RuneScape_Ping_Tool
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write(Environment.NewLine + "Start the test? (y/n): ");

            if (Console.Read() == char.Parse("y"))
            {
                Console.WriteLine();
                Ping();
            }
        }

        static void Ping()
        {
            List<int> lag = new List<int>();

            for (int server = 1; server <= 139; server++)
            {
                string url = "world" + server.ToString() + ".runescape.com";

                Console.WriteLine("Checking world " + server + "...");

                Ping ping = new Ping();
                PingReply reply = ping.Send(url);

                lag.Add(int.Parse(reply.RoundtripTime.ToString()));
            }

            for (int i = 1; i <= 139; i++)
            {
                Console.WriteLine("World " + i + ": " + lag[i - 1]);
            }

            int average = 0;
            int highest = 1;
            int lowest = 1000;

            int highestWorld = 0;
            int lowestWorld = 0;

            for (int i = 1; i <= 139; i++)
            {
                average = average + lag[i - 1];
            }

            for (int i = 1; i <= 139; i++)
            {
                if (highest < lag[i - 1])
                {
                    highest = lag[i - 1];
                    highestWorld = i;
                }
            }

            for (int i = 1; i <= 139; i++)
            {
                if (lowest > lag[i - 1])
                {
                    lowest = lag[i - 1];
                    lowestWorld = i;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Average lag: " + average / 139);
            Console.WriteLine("Highest lag: " + highest + " in world " + highestWorld);
            Console.WriteLine("Lowest lag: " + lowest + " in world " + lowestWorld);

            Console.Write(Environment.NewLine + "Start the test? (y/n): ");

            if (Console.Read() == char.Parse("y"))
            {
                Console.WriteLine();
                Ping();
            }
        }
    }
}

Can you just skip over zeroes? 您可以跳过零吗? Also, you might as well put all the calculations inside a single loop. 同样,您也可以将所有计算放在一个循环中。

int n = 0 // number of data points

for (int i = 0; i < 139; ++i) {
  if (lag[i] == 0) {
    continue;  // skip 0 values
  }
  ++n;
  sum += lag[i];

  if (highest < lag[i]) {
    highest = lag[i];
    highestWorld = i + 1;
  }

  if (lowest > lag[i]) {
    lowest = lag[i];
    lowestWorld = i + 1;
  }

  average = sum / n;  // Note: you may want to round this.
}

You can get the min, max and average with these functions. 您可以使用这些功能获得最小值,最大值和平均值。

var nozeros = lag.Where(i => i > 0);
int lowest = nozeros.Min();
int lowestWorld = lag.IndexOf(lowest);
int highest = nozeros.Max();
int highestWorld = lag.IndexOf(highest);
int average = (int)nozeros.Average();

First consider running everything in parallel (4 at a time here): 首先考虑并行运行所有内容(此处一次运行4个):

var servers = Enumerable.Range(1, 139).Select(i => String.Format("world{0}.runescape.com",i));
var results = servers.AsParallel()
                     .WithDegreeOfParallelism(4)
                     .Select(server => new Ping().Send(server))
                     .ToList();

Then collect just the valid results, note using PingReply.Status rather than checking for 0 : 然后只收集有效结果,请注意使用PingReply.Status而不是检查0

var validResults = results.Where(r => r.Status == IPStatus.Success)
                          .Select(r => r.RoundtripTime);

Here's the info you need: 这是您需要的信息:

Console.WriteLine("Total Results: {0}", results.Count());
Console.WriteLine("Valid Results: {0}", validResults.Count());
Console.WriteLine("Min from Valid: {0}", validResults.Min());
Console.WriteLine("Max from Valid: {0}", validResults.Max());
Console.WriteLine("Avg from Valid: {0}", validResults.Average());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM