javascript 获取 CPU/GPU/内存信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15464896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get CPU/GPU/memory information
提问by Alex Nester
I need to get any information about the CPU/GPU/memory.The number of cores, memory value, memory and cpu usage... I found a way to do this for IE:How to Use JavaScript to Find Hardware Information
我需要获取有关 CPU/GPU/内存的任何信息。内核数、内存值、内存和 CPU 使用率...我找到了一种为 IE 执行此操作的方法:如何使用 JavaScript 查找硬件信息
solutions for other browsers I do not know. Any idea how to do it? maybe webgl has access to information about your computer? or flash? or any other technology?
我不知道其他浏览器的解决方案。知道怎么做吗?也许 webgl 可以访问有关您计算机的信息?或闪光?或任何其他技术?
Thank you very much
非常感谢你
回答by Thomas
This code will print GPU infos an will list all info you can have with the performance object of this browser (there is no standard for the BOM so it changes for each browser).
此代码将打印 GPU 信息,并列出您可以使用此浏览器的性能对象获得的所有信息(BOM 没有标准,因此它因每个浏览器而异)。
<html>
<body>
<canvas id="glcanvas" width="0" height="0"></canvas>
<script>
var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {};
document.write("<br>");
for (var value in performance) {
document.write(value + "<br>");
}
document.write("<br><br><br>");
var canvas;
canvas = document.getElementById("glcanvas");
var gl = canvas.getContext("experimental-webgl");
document.write(gl.getParameter(gl.RENDERER) + "<br>");
document.write(gl.getParameter(gl.VENDOR) + "<br>");
document.write(getUnmaskedInfo(gl).vendor + "<br>");
document.write(getUnmaskedInfo(gl).renderer + "<br>");
function getUnmaskedInfo(gl) {
var unMaskedInfo = {
renderer: '',
vendor: ''
};
var dbgRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
if (dbgRenderInfo != null) {
unMaskedInfo.renderer = gl.getParameter(dbgRenderInfo.UNMASKED_RENDERER_WEBGL);
unMaskedInfo.vendor = gl.getParameter(dbgRenderInfo.UNMASKED_VENDOR_WEBGL);
}
return unMaskedInfo;
}
</script>
</body>
Output in Chrome :
Chrome 中的输出:
onresourcetimingbufferfull
onwebkitresourcetimingbufferfull
timing
navigation
memory
now
getEntries
getEntriesByType
getEntriesByName
clearResourceTimings
setResourceTimingBufferSize
webkitClearResourceTimings
webkitSetResourceTimingBufferSize
mark
clearMarks
measure
clearMeasures
addEventListener
removeEventListener
dispatchEvent
WebKit WebGL
WebKit
NVIDIA Corporation
NVIDIA GeForce GTX 775M OpenGL Engine
Output in Firfox :
Filfox 中的输出:
now
getEntries
getEntriesByType
getEntriesByName
clearResourceTimings
setResourceTimingBufferSize
mark
clearMarks
measure
clearMeasures
toJSON
timing
navigation
onresourcetimingbufferfull
Mozilla
Mozilla
Output in Safari :
Safari 中的输出:
navigation
timing
now
WebKit WebGL
WebKit
NVIDIA Corporation
NVIDIA GeForce GTX 775M OpenGL Engine
回答by Bas van Dijk
回答by Aaron Becker
I wrote this quick script to get the cpu speed:
我写了这个快速脚本来获得 CPU 速度:
var _speedconstant = 8.9997e-9; //if speed=(c*a)/t, then constant=(s*t)/a and time=(a*c)/s
var d = new Date();
var amount = 150000000;
var estprocessor = 1.7; //average processor speed, in GHZ
console.log("JSBenchmark by Aaron Becker, running loop "+amount+" times. Estimated time (for "+estprocessor+"ghz processor) is "+(Math.round(((_speedconstant*amount)/estprocessor)*100)/100)+"s");
for (var i = amount; i>0; i--) {}
var newd = new Date();
var accnewd = Number(String(newd.getSeconds())+"."+String(newd.getMilliseconds()));
var accd = Number(String(d.getSeconds())+"."+String(d.getMilliseconds()));
var di = accnewd-accd;
//console.log(accnewd,accd,di);
if (d.getMinutes() != newd.getMinutes()) {
di = (60*(newd.getMinutes()-d.getMinutes()))+di}
spd = ((_speedconstant*amount)/di);
console.log("Time: "+Math.round(di*1000)/1000+"s, estimated speed: "+Math.round(spd*1000)/1000+"GHZ");
Note that this depends on browser tabs, memory use, etc. but I found it pretty accurate if you only run it once, say at the loading of a page.
请注意,这取决于浏览器选项卡、内存使用等。但我发现如果您只运行一次它就非常准确,比如在加载页面时。
This may not be accurate for desktop devices, especially PCs, but I use it in my website only when other solutions like the first one fail, for getting the average speed of mobile devices (allows me to estimate cores used) using only client-side JS. It may not be the best, but it does pretty well.
对于桌面设备,尤其是 PC,这可能不准确,但我仅在其他解决方案(如第一个解决方案)失败时才在我的网站上使用它,以便仅使用客户端获得移动设备的平均速度(允许我估计使用的内核) JS。它可能不是最好的,但它做得很好。
If you would like you can change the _speedconstant to change the speed, just calculate it with the equation (knowncpuspeed*knowntimetocomplete)/knowncycles. Hope you find this useful!
如果您希望可以更改 _speedconstant 以更改速度,只需使用等式 (knowncpuspeed*knowntimetocomplete)/knowncycles 来计算它。希望你觉得这个有用!
UPDATE 10/19/17: Changed _speedconstant for the new chrome V8 JS engine and added section about what I use it for.
2017 年 10 月 19 日更新:为新的 chrome V8 JS 引擎更改了 _speedconstant,并添加了有关我使用它的内容的部分。