How to get cpu utilization data for phantomJS browser? - c#

public static readonly PerformanceCounter theCPUCounter =
new PerformanceCounter("Processor", "% Processor Time", "_Total");
public static readonly PerformanceCounter theMemCounter =
new PerformanceCounter("Memory", "Available MBytes");
public static readonly PerformanceCounter theCPUCounterPhantomJS =
new PerformanceCounter("Processor", "% Processor Time", Process.GetProcessesByName("phantomjs").ToString());
public static void getCurrentCpuUsage()
{
Console.WriteLine(theCPUCounter.NextValue() + " %");
}
public static void getAvailableRAM()
{
Console.WriteLine(theMemCounter.NextValue() + " MB");
}
public static void getCurrentCpuUsagePhantomJS()
{
Console.WriteLine(theCPUCounterPhantomJS.NextValue() + " %");
}
I want cpu utilization data for PhantomJS browser. Here is the code that I am using. I just want to know that what parameter I can enter instead of Process.GetProcessesByName("phantomjs").ToString(). I tried many approach but it fails and gave me exception System.InvalidOperationException : Instance 'System.Diagnostics.Process[]' does not exist in the specified Category. .The name of the process is phantomjs.exe that shows in task manager. Please advice.

See the docs - this is how you should instantiate the performance counter in your case:
new PerformanceCounter(
"Process",
"% Processor Time",
"phantomjs");
Process.GetProcessesByName returns an array of processes, and so the ToString results in "System.Diagnostics.Process[]", which is not a valid instance name, not under the Processor category, and not under the Process category.

Related

Unity C# System Diagnostics PerformanceCounter CPU RAM

Looking to get the CPU/RAM %, I implemented the code below, however the counters always return CPU 100, RAM 0. I've tried 2sec invokes, same result. Googling this issue results in a thousand responses saying it's caused by no delay (which it usually is in the code samples seen), however given the invokes I've used, doesn't seem to be the cause here?
public class ScriptName : MonoBehaviour
{
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
void Start()
{
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
cpuCounter.NextValue();
ramCounter.NextValue();
Invoke("UpdatePerformance", 1);
}
public void UpdatePerformance()
{
float cpu = cpuCounter.NextValue(); // Ouputs 100
float ram = ramCounter.NextValue(); // Outputs 0
Invoke("UpdatePerformance", 1); // 2 sec delay, same output
}
}
Thanks

get current cpu usage using c# in asp.net framework

I would like to find out the current CPU usage using the codes below(c# in asp.net framework). However it gives me "0% of CPU usage" when I try to run the program. When I check my task manager, I found out that the actual total CPU usage is more than 5%. Does anyone know what is wrong with the code below?
public partial class cpuUsage : System.Web.UI.Page
{
PerformanceCounter cpu;
protected void Page_Load(object sender, EventArgs e)
{
cpu = new PerformanceCounter();
cpu.CategoryName = "Processor";
cpu.CounterName = "% Processor Time";
cpu.InstanceName = "_Total";
lblCPUUsage.Text = getCurrentCpuUsage();
}
public string getCurrentCpuUsage()
{
return cpu.NextValue() + "%";
}
}
The first value returned by a PerformanceCounter will always be 0. You'll need a Timer or Thread that keeps monitoring the value in the background. This code for example will output the correct value every second (don't use this actual code, it's quick and dirty):
new Thread(() =>
{
var cpu = new PerformanceCounter
{
CategoryName = "Processor",
CounterName = "% Processor Time",
InstanceName = "_Total"
}
while (true)
{
Debug.WriteLine("{0:0.0}%", cpu.NextValue());
Thread.Sleep(1000);
}
}).Start();
Make sure to read the remarks of the PerformanceCounter.NextValue method:
If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.

Creating a new System.Diagnostics.PerformanceCounter is very slow

I have a simple monitoring application that is getting some values from PerfMon counters. Even when testing on the local machine, it is taking over 30 seconds to create a new PerformanceCounter object.
using System;
using System.Diagnostics;
namespace test_slow_perfmon
{
class Program
{
static void Main(string[] args)
{
Stopwatch w = new Stopwatch();
w.Start();
PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total", "localhost");
w.Stop();
Console.WriteLine(string.Format("Creating a counter took {0}ms", w.Elapsed.TotalMilliseconds));
}
}
}
Output from that indicates over 32s to create each counter.
What can I do (if anything) to speed up the creation of the counters?
30 seconds sounds to me suspiciously like a timeout, indicating to me that this could be some sort of network issue.
Try creating your perfmon counter using the constructor that doesn't specify a hostname and see if that helps:
PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total");

Source of the inbuilt method getAvailableRam

I need the code or the definition of the method getAvailableRam used in C# to find the memory usage of the computer.
class Program
{
static void Main()
{
var ramCounter = new PerformanceCounter("Memory", "Available MBytes");
Console.WriteLine("{0} Mb", ramCounter.NextValue());
}
}

Performance counter C# and HyperPi

I am in attempt to get CPU Usage reading correctly. The code below is executed without problem but I can't get CPU Usage reading at 100% when I run Hyper PI which is a multicores CPU stress test. What did I do wrong? Thank you for your time.
Alternatively, is it possible to get CPU Usage readings from WMI?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace TestHarnessCPUUsage
{
class Program
{
static void Main(string[] args)
{
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
ramCounter.CategoryName = "Memory";
ramCounter.CounterName = "Available MBytes";
Console.WriteLine(cpuCounter.NextValue()
+ "%");
Console.WriteLine(ramCounter.NextValue()
+ "MB");
Console.ReadKey();
}
}
}
That's the kind of perf counter you have to sample yourself. Processor time is a measure over an interval. It tells you how long the processor was turned off, versus it running at 100%. The ratio is the value you get, times 100. One second is the typical interval time, like TaskMgr.exe and Perfmon uses. The smaller you make the interval, the 'noisier' it gets. Down to one sample, like you did, where you get no data at all since there wasn't an interval. Fix:
using System;
using System.Diagnostics;
class Program {
static void Main(string[] args) {
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
cpuCounter.NextValue();
while (!Console.KeyAvailable) {
System.Threading.Thread.Sleep(1000);
Console.WriteLine(cpuCounter.NextValue());
}
}
}

Categories