Run the subscriber script:
node realtime-latency-benchmark.js subThen you can run publisher scripts:
node realtime-latency-benchmark.js pub| const | |
| PipeRunner = require('./pipeRunner'); | |
| const now = (unit) => { | |
| const hrTime = process.hrtime(); | |
| switch (unit) { | |
| case 'milli': |
| const | |
| PipeRunner = require('./pipeRunner'); | |
| const now = (unit) => { | |
| const hrTime = process.hrtime(); | |
| switch (unit) { | |
| case 'milli': |
| node bench/returning-promises.js | |
| standard x 3,096,388 ops/sec ±0.77% (91 runs sampled) | |
| async wrapped x 1,853,672 ops/sec ±1.12% (81 runs sampled) | |
| await then return x 1,074,238 ops/sec ±1.96% (53 runs sampled) | |
| Fastest is standard |
| # https://stackoverflow.com/questions/60910274/int32-vs-float64-performances-in-crystal | |
| require "benchmark" | |
| def percentage_diff(number_a, number_b) | |
| small_number = (number_a > number_b) ? number_b : number_a | |
| big_number = (number_a < number_b) ? number_b : number_a | |
| (big_number - small_number) / small_number | |
| end |
| const app = express(); | |
| const log = message => console.log(message); | |
| const emailService = new EmailService(logger); | |
| app.get('/', (request, response) => { | |
| const requestId = uuid(); | |
| log(`[${requestId}] Start processing`); | |
| await emailService.notify(request.body.emails, requestId); |
| class EmailService { | |
| constructor (log) { | |
| this.log = log; | |
| } | |
| async notify (emails, requestId) { | |
| for (const email of emails) { | |
| this.log(`[${requestId}] Send email: ${email}`); | |
| sendGrid.send(email); | |
| } |
| const { AsyncLocalStorage } = require('async_hooks'); | |
| const asyncLocalStorage = new AsyncLocalStorage(); | |
| app.get('/', (request, response) => { | |
| const requestId = uuid(); | |
| asyncLocalStorage.run(requestId, async () => { | |
| // entering asynchronous context | |
| log('Start processing'); | |
| await emailService.notify(request.body.emails); |
| const log = message => { | |
| const requestId = asyncLocalStorage.getStore(); | |
| if (requestId) { | |
| console.log(`[${requestId}] ${message}`); | |
| } | |
| else { | |
| console.log(message); | |
| } | |
| }; |