码迷,mamicode.com
首页 > 编程语言 > 详细

性能监控-cpu、内存、上下行网速、线程

时间:2020-05-25 15:23:01      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:system   上传   监控   ble   pst   tin   ati   性能   vat   

需求:整机cpu,内存,网络流量;每个服务占用cpu,内存,线程数

public class CPU
    {
        public void Run()
        {
            string name = "Service";
            //Process[] p =Process.GetProcesses() ;
            Process[] p1 = Process.GetProcessesByName("tService");

            PerformanceCounter cpuCounter1 = new PerformanceCounter("Process", "% Processor Time", name);//% User Time  % Processor Time
            PerformanceCounter ramCounter1 = new PerformanceCounter("Process", "Working Set - Private", name);
            //PerformanceCounter ramCounter1 = new PerformanceCounter("Memory", "Available MBytes", name);

            //IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();

            Console.WriteLine(name + "电脑CPU使用率:" + cpuCounter1.NextValue() + "%");
            Console.WriteLine(name + "电脑可使用内存:" + ramCounter1.NextValue() / 1024 / 1024 + "MB");
            //Console.WriteLine($"接收数据包:{ipstat.ReceivedPackets/1024}Kbps");
            //Console.WriteLine($"发送数据包:{ipstat.ReceivedPacketsDelivered / 1024}Kbps");
            Console.WriteLine();

            PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");//可以用内存    % Committed Bytes In Use 内存使用率

            Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + "%");
            Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB");
            Console.WriteLine();

            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();
            long receivedPBegin = ipstat.ReceivedPackets;
            long ReceivedPDBegin = ipstat.ReceivedPacketsDelivered;

            while (true)
            {
                System.Threading.Thread.Sleep(1000);

                ipstat = properties.GetIPv4GlobalStatistics();
                Console.WriteLine("电脑CPU使用率:" + cpuCounter.NextValue() + " %");
                Console.WriteLine("电脑可使用内存:" + ramCounter.NextValue() + "MB");
                long receivedPackets = ipstat.ReceivedPackets;
                long receivedPacketsD = ipstat.ReceivedPacketsDelivered;
                Console.WriteLine($"接收数据包:{receivedPackets - receivedPBegin}");
                Console.WriteLine($"发送数据包:{receivedPacketsD - ReceivedPDBegin}");
                Console.WriteLine();

                Console.WriteLine(name + "电脑CPU使用率:" + cpuCounter1.NextValue() + "%");
                Console.WriteLine(name + "服务内存===:" + ramCounter1.NextValue() / 1024 / 1024 + "MB");
                Console.WriteLine(name + "线程数量:" + Process.GetProcessesByName(name)[0].Threads.Count);
                Console.WriteLine();
                receivedPBegin = receivedPackets;
                ReceivedPDBegin = receivedPacketsD;

            }
        }

    }

上下行网速:

        public void Run2()
        {
            //初始化Counter
            PerformanceCounterCategory pcCategory = new PerformanceCounterCategory("Network Interface");
            string[] iNames = pcCategory.GetInstanceNames();//此函数第一次执行特别耗时(不知道为什么)
            PerformanceCounter[] pCounters = pcCategory.GetCounters(iNames[0]);//iNames[0]为"ASIX AX88772C USB2.0 to Fast Ethernet Adapter";iNames[1]为"Intel[R] Ethernet Connection [7] I219-V"
            //pCounters.                                                                   //给网络监控计数器赋值
            mCounter = pCounters[0];
            mCounter.NextValue();//初始值  

            while (true)
            {
                System.Threading.Thread.Sleep(1000);
                double SpeedKbps = mCounter.NextValue() * 8 / 1000;
                if ((SpeedKbps / 1000) > 1)
                {
                    Console.WriteLine(String.Format("{0:f1} Mbps", SpeedKbps / 1000)); //得到该适配器的上传速度
                }
                else
                {
                    Console.WriteLine(String.Format("{0:f1} Kbps", SpeedKbps)); //得到该适配器的上传速度
                }

            }
        }

 

性能监控-cpu、内存、上下行网速、线程

标签:system   上传   监控   ble   pst   tin   ati   性能   vat   

原文地址:https://www.cnblogs.com/zhuyapeng/p/12956771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!