private static void GetBytesSentReceivedCounters(string[] args, string[] hosts, int nThreads, int nReads, int sleep) { DateTime startTime = DateTime.Now; Dictionary <string, float> dtSent = new Dictionary <string, float>(); Dictionary <string, float> dtReceived = new Dictionary <string, float>(); MySpace.DataMining.Threading.ThreadTools <string> .Parallel( new Action <string>( delegate(string slave) { if (string.Compare(slave, "localhost", true) == 0) { slave = System.Net.Dns.GetHostName(); } List <System.Diagnostics.PerformanceCounter> received = new List <System.Diagnostics.PerformanceCounter>(); List <System.Diagnostics.PerformanceCounter> sent = new List <System.Diagnostics.PerformanceCounter>(); lock (dtSent) { Console.WriteLine(); Console.WriteLine("Waiting to connect: {0}", slave); } System.Diagnostics.PerformanceCounterCategory cat = new System.Diagnostics.PerformanceCounterCategory("Network Interface", slave); string[] instances = cat.GetInstanceNames(); try { foreach (string s in instances) { if (s.ToLower().IndexOf("loopback") == -1) { received.Add(new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Received/sec", s, slave)); sent.Add(new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", s, slave)); } } lock (dtSent) { Console.WriteLine(); Console.WriteLine("Connected: {0}", slave); } //Initial reading. foreach (System.Diagnostics.PerformanceCounter pc in received) { pc.NextValue(); } foreach (System.Diagnostics.PerformanceCounter pc in sent) { pc.NextValue(); } float br = 0; float bs = 0; for (int i = 0; i < nReads; i++) { System.Threading.Thread.Sleep(sleep); foreach (System.Diagnostics.PerformanceCounter pc in received) { br += pc.NextValue(); } foreach (System.Diagnostics.PerformanceCounter pc in sent) { bs += pc.NextValue(); } } if (nReads > 1) { br = br / (float)nReads; bs = bs / (float)nReads; } lock (dtSent) { Console.WriteLine(); Console.WriteLine("Received {0}: {1}/s", slave, AELight.GetFriendlyByteSize((long)br)); Console.WriteLine("Sent {0}: {1}/s", slave, AELight.GetFriendlyByteSize((long)bs)); dtSent.Add(slave, bs); } lock (dtReceived) { dtReceived.Add(slave, br); } } catch (Exception e) { lock (dtSent) { Console.Error.WriteLine("Error while reading counter: {0}: {1}", slave, e.Message); } } } ), hosts, nThreads); //Write out total sent, received. double totalSent = 0; double totalReceived = 0; foreach (float f in dtSent.Values) { totalSent += f; } foreach (float f in dtReceived.Values) { totalReceived += f; } Console.WriteLine(); Console.WriteLine("Total Received: {0}/s", AELight.GetFriendlyByteSize((long)totalReceived)); Console.WriteLine("Total Sent: {0}/s", AELight.GetFriendlyByteSize((long)totalSent)); //Sort List <KeyValuePair <string, float> > sSent = new List <KeyValuePair <string, float> >(dtSent); sSent.Sort(CompareFloat); List <KeyValuePair <string, float> > sReceived = new List <KeyValuePair <string, float> >(dtReceived); sReceived.Sort(CompareFloat); //Max, min Console.WriteLine(); Console.WriteLine("Min Received: {0} {1}/s", sReceived[0].Key, AELight.GetFriendlyByteSize((long)sReceived[0].Value)); Console.WriteLine("Min Sent: {0} {1}/s", sSent[0].Key, AELight.GetFriendlyByteSize((long)sSent[0].Value)); Console.WriteLine("Max Received: {0} {1}/s", sReceived[sReceived.Count - 1].Key, AELight.GetFriendlyByteSize((long)sReceived[sReceived.Count - 1].Value)); Console.WriteLine("Max Sent: {0} {1}/s", sSent[sSent.Count - 1].Key, AELight.GetFriendlyByteSize((long)sSent[sSent.Count - 1].Value)); //Avg double avgSent = totalSent / (double)sSent.Count; double avgReceived = totalReceived / (double)sReceived.Count; Console.WriteLine(); Console.WriteLine("Avg Received: {0}/s", AELight.GetFriendlyByteSize((long)avgReceived)); Console.WriteLine("Avg Sent: {0}/s", AELight.GetFriendlyByteSize((long)avgSent)); //Dt Console.WriteLine(); Console.WriteLine("Perfmon Request Time: {0}", startTime.ToString()); Console.WriteLine("Perfmon End Time: {0}", DateTime.Now.ToString()); }
private static string GetFriendlyByteSize(double size) { return(AELight.GetFriendlyByteSize((long)size)); }