问题描述
我正在考虑使用WebGL和相关技术在JavaScript中编写游戏。我想让游戏尽可能智能,所以我正在研究监控CPU /内存使用情况。
I'm considering writing a game in JavaScript using WebGL and associated technologies. I would like to make the game as intelligent as possible, so I'm looking into monitoring CPU/memory usage.
例如:
- 对于高CPU使用率,缩小图形或将计算卸载到服务器
- 对于高内存使用率,将数据卸载到存储服务器(以及后来的检索)
我想获取Chrome提供的数据任务管理器
。我知道如何跟踪FPS,这可以带来一些灵活性,但我希望获得尽可能多的信息。主要用例是低功耗模式,其中CPU尽可能少地用于(笔记本电脑)或用户浏览论坛时的空闲模式等。
I would like to get the data that Chrome offers in it's Task Manager
. I know how to track FPS, and that can lead to some flexibility, but I would like to be have as much information as possible. The main use case is for a 'low power' mode where the CPU is utilized as little as possible (for laptops) or an idle mode when the user is browsing forums, etc.
我知道如何使用分析器,但我想从JavaScript访问这些工具。
I know how to use profilers, but I would like access to these tools from JavaScript.
这可能吗?如果没有,你知道它是否已被提议用于标准化吗?
Is this possible? If not, do you know if it has been proposed for standardization?
我愿意接受扩展,只要它可以从JavaScript查询,但是如果存在本机功能,我想避免使用它。我正在尝试定位最新版本的Firefox和Chrome,但如果有人支持,我可以将自己限制在一个浏览器中。
I would be willing to live with an extension, as long as it could be queried from JavaScript, but I'd like to avoid it if a native feature exists. I'm trying to target recent versions of Firefox and Chrome, but I could restrict myself to a single browser if one supports this.
推荐答案
没有直接的javascript调用来获取这些信息(这可能是一个严重的安全问题)。但是对于你的问题有一个解决方案,你可以使用工作池,这些工作池是javascript的litterally线程来模拟一个函数,该函数将在后台运行一些计算以计算CPU使用率。
但是,由于你正在构建一个3D应用程序,我不建议这样做,因为它会不必要地花费你大量的CPU使用量来了解当前的CPU使用率,这就像用一台子机器杀死一只苍蝇枪。
Well there is no direct javascript call to get such information (which would have been a serious security problem). But there's a solution for your problem, you can use worker pools which are litterally threads for javascript to emulate a function that will run some calculations in the background to compute the CPU usage.But since you're building a 3D application I will not advise to do this because it will unnecessarily cost you a lot of CPU usage to know the current level of CPU usage, which would be like killing a fly with a submachine gun.
我建议你做的只是关注每秒帧数,因为它们与你的应用程序有关,并且是你需要的确切基准指示。不关心cpu负载,你的应用程序并不直接依赖于它,特别是如果你有一个双核或四核处理器。您还应该查看应用程序的GPU使用情况以及如何在兼容的浏览器上充分利用GPU(最新的Chromes使用GPU加速)。
What I advise you to do however is to only focus on frame per seconds because they are related to your application and are the exact benchmarking indication you need. Don't care about cpu load, your application doesn't directly depend on that, especially if you got a dual-core or quad-core processor. You should perhaps also look at GPU usage for your application and how you can fully take benefit of the GPU on a compatible browser (latest Chromes uses GPU acceleration).
祝你好运与你的游戏!
这篇关于Javascript-动态监控CPU /内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!