gpt4 book ai didi

javascript - 使用 Web Workers 进行多核心负载管理怎么样?

转载 作者:行者123 更新时间:2023-12-01 15:33:22 24 4
gpt4 key购买 nike

当使用网络 worker 时,假设在四核系统或八核系统上,负载是如何处理的?

我的意思是,假设我有 4 个 worker 。他们将如何根据当前负载在 4 个 CPU 内核上进行调度?谁处理负载? (系统?)是否使用了完整的 4 个(或 8 个)CPU?

最佳答案

您基本上有两种选择来获得这个问题的答案,并且每个浏览器甚至每个浏览器/平台组合都可能有不同的答案。

选项 1) 是研究浏览器的代码并弄清楚它实际上是如何分配这些网络工作线程的。这可能不仅需要查看浏览器代码的功能,还需要了解特定操作系统如何处理与多个 CPU 相关的问题。显然,这只能由有权访问浏览器源代码(适用于开源浏览器)的人来完成。

选项 2) 是构建一个测试应用程序,看看你可以从测试应用程序中学到什么。

我承担了构建测试应用程序的任务,然后看看我可以在当前版本的流行浏览器中从中学到什么。如果您想在系统上使用 webWorkers 并查看 CPU 负载是如何工作的,这里是测试页面:

http://files.the-friend-family.com/demos/webworker.html

默认情况下,测试页面启动 8 个网络 worker 。您可以通过取消选中或选中页面上的复选框将其拨回任意数量的网络 worker 。然后,您可以在不同条件下检查系统 CPU 监视器。当检查 webworker 时,它正在运行计算以计算素数。为了允许向 webworker 发送消息,它运行 1 秒的计算,短暂暂停(毫秒)以查看是否有来自主浏览器页面的消息要处理,然后再运行 1 秒的计算,等等......每秒,它会使用最新的质数向主浏览器窗口发送消息。

这是我在装有 Windows 8 的 Quad Core i7-3770K CPU 上运行此页面时得出的结论。我的 Windows 8 操作系统上的系统监视器显示我的 Quad Core 有 8 个 CPU(关于超线程的东西使它在一个 quad-Core 上显示 8 个 CPU)核心处理器,我认为)。

即 11

8 workers running, 100% overall load, all CPUs show as saturated 100%
4 workers running, 65% overall load, all CPUs show some work, none show 100%
2 workers running, 32% overall load, 2 CPUs are near 100%, other CPUs have spikes
of activity
1 worker running, 18% overall load, 1 CPU shows most of the work, but other
CPUs have spikes of activity

Chrome 34
8 workers running, 100% overall load, all CPUs show as 100% all the time
4 workers running, 63% overall load, all CPUs show some work, none show 100%
2 workers running, 32% overall load, 1 CPU near 100%, second CPU shows
lots of work, others show sporadic spikes of activity
1 worker running, 17% overall load, 1 CPU shows most of the work,
but other CPUS have spikes of activity

火狐 29
8 workers running, 30% overall load, 4 CPUs show 40% load, others show 20% load
4 workers running, 40% overall load, 4 CPUs show 80% load, others show 5% load
2 workers running, 32% overall load, 2 CPUs show near 100% load, others show
spikes of activity
1 worker running, 18% overall load, 1 CPU near 100%, but also has lulls of
no activity where others CPUs are doing the work

因此,我得出的结论是,当有一个 web worker 时,它似乎主要在特定的核心上运行,但它并没有锁定到那个核心,它只是倾向于在那个核心上完成大部分工作。

对于两个网络 worker ,大多数工作似乎是由两个特定的核心完成的,但它看起来不像是硬绑定(bind),因为有时其他核心会做一些工作。

有四个网络 worker ,IE 和 Chrome 似乎在所有处理器之间平均分担工作,没有一个 Hook 。 Firefox 倾向于在大部分工作中使用某些处理器,而另一些则不是很多。

IE 和 Chrome 有 8 个网络 worker ,涉及 100% 的所有处理器。 Firefox 几乎似乎以某种方式保护了系统,因为当从 4 个 Web worker 变为 8 个 Web worker 时,整体 CPU 负载实际上下降了,而且没有一个 CPU 接近 100%。

网络 worker 似乎没有硬绑定(bind)到特定的核心。必须查看特定平台上特定浏览器的源代码,才能知道是浏览器代码还是操作系统管理核心使用。我猜想(尽管我实际上并不知道),浏览器正在为每个 Web Worker 使用操作系统级别的线程,然后取决于操作系统如何将其映射到可用内核与其他负载系统。

刚刚找到 this info on Firefox on MDN :

The Worker interface spawns real OS-level threads

关于javascript - 使用 Web Workers 进行多核心负载管理怎么样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com