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

Posted zhuyapeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了性能监控-cpu内存上下行网速线程相关的知识,希望对你有一定的参考价值。

需求:整机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内存上下行网速线程的主要内容,如果未能解决你的问题,请参考以下文章

JVM系统性能监控总结

TrafficMonitorr网速流量监控/CPU内存率查看工具

Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率

Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率

Ubuntu 16.04 标题栏实时显示上下行网速CPU及内存使用率--indicator-sysmonitor

如何用java代码来监控系统内存·cpu·线程占用情况,并生成日志