3

What is the process here to obtain CPU and memory usage for a process? What values do I have to pass?

Process p = new Process();
PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);

while (true)
{
    Thread.Sleep(500);
    double ram = ramCounter.NextValue();
    double cpu = cpuCounter.NextValue();
    Console.WriteLine("RAM: " + (ram / 1024 / 1024) + " MB; CPU: " + (cpu) + " %");
    Console.ReadLine();
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    @CodeCaster, your edit really changes the intent of the question, getting the performance for a console app is very different than getting the performance for a MVC app and the way the question is written it makes it look like the OP wants it for a console app now which is not what the original tags and title indicated. – Scott Chamberlain Jun 27 '16 at 20:02
  • @Scott it's not my problem that the OP can't properly express themselves. Saying "MVC" in the title and applying the "model-view-controller" tag does, in my opinion, not mean _"Translate this Console app code to MVC for me"_. If it does, the OP is free to edit that phrase into the question. – CodeCaster Jun 27 '16 at 20:05
  • @Scott that being said, I've applied the "asp.net-mvc" tag, given the existing answer now assumes that. (And, again in my opinion, if someone is going to answer a [question in this state](http://stackoverflow.com/revisions/38046532/2), they should edit it to make it more readable anyway). – CodeCaster Jun 27 '16 at 20:08
  • 2
    Next time when you copy code from an answer (or question) make sure to link to it and give proper attribution: http://stackoverflow.com/a/3545253/578411 – rene Jun 27 '16 at 20:08

1 Answers1

2

For the Process you can use the static method GetCurrentProcess. The result goes into the performance counter.

In the following controller the PerformanceCounters are created once and then re-used. A timer guarantees 500 milliseconds has passed before the first call can be made.

public class PerformanceController : Controller
{
    static PerformanceCounter ramCounter;
    static PerformanceCounter cpuCounter;

    static Timer timer;
    static ManualResetEvent waiter = new ManualResetEvent(false);

    static Performance lastMeasure = new Performance(); // the Model (in Mvc)

    static PerformanceController()
    {
        // Get the current process
        using (var p = Process.GetCurrentProcess())
        {
            ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
            cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
        }
        // make sure some time has passed before first NextValue call
        timer = new Timer(s =>
        {
            waiter.Set();
        }, null, 500, Timeout.Infinite);

        // clean-up
        AppDomain.CurrentDomain.DomainUnload += (s, e) => {
            var time = (IDisposable)timer;
            if (time != null) time.Dispose();
            var wait = (IDisposable)waiter;
            if (wait != null) wait.Dispose();
            var rc = (IDisposable)ramCounter;
            if (rc != null) rc.Dispose();
            var cc = (IDisposable)cpuCounter;
            if (cc != null) cc.Dispose();
        };
    }

    private static  Performance GetReading()
    {
        // wait for the first reading 
        waiter.WaitOne();
        // maybe cache its values for a few seconds
        lastMeasure.Cpu = cpuCounter.NextValue();
        lastMeasure.Ram = ramCounter.NextValue();
        return lastMeasure;
    }

    //
    // GET: /Performance/
    public ActionResult Index()
    {
        return View(GetReading());
    }
}

The performance model is really simple:

public class Performance
{
    public double Ram { get; set; }
    public double Cpu { get; set; }
}

And the following view completes the implementation

@model MvcApplication1.Models.Performance
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div><span>Ram</span><span>@Model.Ram</span> </div>
<div><span>Cpu</span><span>@Model.Cpu</span> </div>
rene
  • 41,474
  • 78
  • 114
  • 152