crypto.randombytes同步与异步性能

发布于 2025-02-12 17:17:51 字数 1016 浏览 1 评论 0原文

noreflow noreferrer“> crypto.randomombytes 具有Sync and sync and sync and sync and sync and assync功能。我创建了一个库,该库具有一种使用crypto.randombytes进行微服务到微服务通信生成16个字符唯一TransactionId的方法。它由100多个微服务使用。微服务由nodejs提供动力。

想了解我是否使用异步一个或同步一个,就像我对两者进行基准测试时一样,我的结果令人惊讶。

异步方法:

% cat asyncRandomBytes.js 
crypto = require("crypto");
for (var i = 0; i < 2000000; i++) {
  crypto.randomBytes(16, function(err) {});
}
% time node asyncRandomBytes.js

node asyncRandomBytes.js  20.79s user 13.67s system 210% cpu 16.336 total

同步方法:

% cat syncRandomBytes.js 
crypto = require("crypto");
for (var i = 0; i < 2000000; i++) {
  crypto.randomBytes(16);
}
% time node syncRandomBytes.js 

node syncRandomBytes.js  3.92s user 1.41s system 132% cpu 4.017 total

异步一个人花费更多的时间。我应该在生产中使用异步一个,其中单核计算机上有多个API调用(每次服务20K rpm)?

crypto.randomBytes has both sync and async functionality. I have created a library which has a method to generate 16 character unique transactionId using crypto.randomBytes for microservice-to-microservice communication. It's used by 100+ microservices. The microservices are powered by nodejs.

Wanted to understand should I use async one or sync one as when I benchmarked both, I got surprising result.

Async approach:

% cat asyncRandomBytes.js 
crypto = require("crypto");
for (var i = 0; i < 2000000; i++) {
  crypto.randomBytes(16, function(err) {});
}
% time node asyncRandomBytes.js

node asyncRandomBytes.js  20.79s user 13.67s system 210% cpu 16.336 total

Sync approach:

% cat syncRandomBytes.js 
crypto = require("crypto");
for (var i = 0; i < 2000000; i++) {
  crypto.randomBytes(16);
}
% time node syncRandomBytes.js 

node syncRandomBytes.js  3.92s user 1.41s system 132% cpu 4.017 total

The async one is taking more time. Should I use async one in production where we have multiple API calls (20K rpm per service) on a single core machine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2025-02-19 17:17:51

async

如果收到更多请求,则可以及时处理它可以导致Nodejs服务器崩溃。 (在内存之外,或由于GC而悬挂在100%CPU上)

您可以实现一个限制器,以防止开放的新异步调用,以使过程不会崩溃,但这可以导致超时。

同步

如果您使用同步并获取更多请求,则可以处理它可以导致Timouts,但服务器将还活着。

替代方案

您应该考虑使用更多实例,也可以在多个CPU内部共享负载,或者如果您只有一个核心,它也可以减少GC的负载。 (使用您选择的流程经理)

tipp:对于node.js,您可以 pm2 作为Process Manager

gc =垃圾收集器

Async

If you get more requests then you can process in time it can lead the nodejs server to crash. (Out of Memory, or Hanging at 100% CPU because of the GC)

You can implement a limiter that prevents open new async calls so that the process cant crash but this can lead to timeouts.

Sync

If you use sync and get more requests then you can process it can lead to timouts but the server will be alive.

Alternative

You should consider to use more instances too share the load over multiple CPU cores or if you only have one core it can also reduce the load for the GC. (Use a process manager of your choice)

Tipp: For Node.js you can pm2 as Process Manager

GC = Garbage Collector

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文