【发布时间】:2016-10-25 09:37:24
【问题描述】:
我的问题是关于我的 NodeJS 应用程序的性能...
如果我的程序运行 12 次迭代,每次 1.250.000 = 15.000.000 次迭代 - 亚马逊的专用服务器需要以下时间来处理:
r3.large:2 个 vCPU、6.5 个 ECU、15 GB 内存 --> 123 分钟
4.8xlarge:36 个 vCPU、132 个 ECU、60 GB 内存 --> 102 分钟
我有一些类似于下面代码的代码...
start(); start(){ for(var i=0; i<12; i++){ function2(); // Iterates over a collection - which contains data split up in intervals - by date intervals. This function is actually also recursive - due to the fact - that is run through the data many time (MAX 50-100 times) - due to different intervals sizes... } } function2(){ return new Promise{ for(var i=0; i<1.250.000; i++){ return new Promise{ function3(); // This function simple iterate through all possible combinations - and call function3 - with all given values/combinations } } } } function3(){ return new Promise{ // This function simple make some calculations based on the given values/combination - and then return the result to function2 - which in the end - decides which result/combination was the best... }}
这等于每次迭代 0.411 毫秒 / 441 微秒!
当我查看任务栏中的性能和内存使用情况时……CPU 没有以 100% 的速度运行 - 但更像是 50%……整个时间? 内存使用量开始非常低 - 但 KEEPS 以 GB 为单位增长 - 每分钟直到进程完成 - 但是当我在 Windows CMD 中按 CTRL+C 时首先释放(分配的)内存......所以它就像 NodeJS 垃圾收集不能以最佳方式工作 - 或者可能是代码的设计再次简单......
当我执行应用程序时,我使用内存选项:
节点 --max-old-space-size="50000" server.js
请告诉我你能做的每一件事 - 让我的程序更快!
谢谢大家 - 非常感谢!
【问题讨论】:
-
如果您需要在一个紧密的循环中创建 15M 的 Promise,听起来您应该对应用程序进行大量重组,而不是专注于如何加快该循环的速度。你能提供更多关于你的应用在做什么的信息吗?为什么需要这么多承诺?
function3是做什么的? -
首先 - 我是 Node 新手...所以很可能会出现设计错误!简而言之......该程序不使用/访问数据库,不写入磁盘或任何需要多次迭代的东西......它只适用于一些简单的数组/对象......它只是进行大量计算/分析 -在大量数据上...从数据库中获取-在这之前的一步...原因-我使用承诺...是因为节点我设计为异步-尽管这是一个很好的方法做吗?我曾经使用过 .Net/C# 之类的同步代码...
-
将计算包装在 Promise 中并不一定会使它的性能好很多,尤其是当您创建这么多 Promise 时。如果您在 Google 上搜索 “节点繁重的计算”,您可能会发现一些关于如何在不同的子进程上拆分计算的好方法,利用比单个 Node 进程更多的 CPU 资源。还有various modules可以帮到你。
-
谢谢你,我明白了......而且我知道我可以做 1000 件事情......但我真的需要一些比我更有经验的人 - 告诉我 - 什么确切的解决方案会对我来说是最好的......我可以轻松地用错误的“解决方案”,框架等再使用一周......我需要具体的代码示例......
-
我明白了。如果您可以解释您需要执行的计算类型(我假设“12”和“1250000”指的是特定的东西),也许它会让人们更容易帮助您。
标签: node.js performance memory nested-loops es6-promise