简体   繁体   English

计算CPU使用率

[英]Calculating CPU usage

Our business model requires to charge users based on CPU hours/usage. 我们的业务模式需要根据CPU小时/使用情况向用户收费。

Initially I was thinking about simply using StopWatch and then use elapsed time as a unit of measure. 最初我只考虑使用StopWatch ,然后使用经过时间作为度量单位。

But the problem I see with this approach is that in the day time when 5000 users are hitting the server, the elapsed time for the same piece of code will be higher than in the night time when only 50 users are hitting the server. 但我用这种方法看到的问题是,在5000个用户点击服务器的那一天,同一段代码的经过时间将高于仅有50个用户点击服务器的夜间。

Other option is to charge users based on the iterations performed on the data users passed in. There are couple of issues with this approach as well. 其他选项是根据对传入的数据用户执行的迭代向用户收费。此方法也存在一些问题。

  • Some functions would not require any iteration over data. 某些函数不需要对数据进行任何迭代。
  • CPU usage for x + y is lower than that of x + y * z . x + y CPU使用率低于x + y * z CPU使用率。

What is a good way to calculate CPU usage information in C#/.Net so that it remains the same for all users for the same operation regardless of the server's load? 在C#/ .Net中计算CPU使用率信息的好方法是什么,这样对于同一操作的所有用户,无论服务器的负载如何,它都保持不变?

Possibly, total amount of actual assembly level instructions? 可能是实际装配级别指令的总量? Is there a way to get that information in C#? 有没有办法在C#中获取该信息?

What other approached, recommendations can you share? 其他什么接近,建议你可以分享?

Thanks a lot for looking into this. 非常感谢您对此进行调查。

Execute whatever your users want to run in a separate process and look for its CPU time after the process is done. 执行您的用户想要在单独的进程中运行的任何内容,并在完成该过程后查找其CPU时间 This is also different from wallclock time as it only takes into account the time the process actually ran on the CPU. 这也与挂钟时间不同,因为它只考虑了进程实际在CPU上运行的时间。

Minimal example for those who are unwilling to read documentation: 那些不愿阅读文档的人的最小例子:

ProcessStartInfo psi = new ProcessStartInfo(@"C:\Blah\Foo.exe");
Process p = Process.Start(psi);
p.WaitForExit();
TimeSpan time = p.TotalProcessorTime;
p.Close();

You may want to run this asynchronously, though. 但是,您可能希望以异步方式运行它。

No need for estimation, guessing or black magic. 无需估计,猜测或黑魔法。 You cannot calculate it reliably anyway. 无论如何,你无法可靠地计算它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM