Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram: extract cluster lazy loading method to make it testable #38563

Merged
merged 1 commit into from
May 9, 2021

Conversation

pd4d10
Copy link
Contributor

@pd4d10 pd4d10 commented May 6, 2021

There is no need to ensure cluster loaded in bindServerHandle because
it's already loaded before calling it.

This PR makes cluster as an argument to remind developers that cluster
needs to be loaded before calling this function.

Update:
The lazy load parts in bindServerHandle is unreachable because cluster has been loaded before calling it. Extract it to a seperated method to make the code testable.

Refs:
https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202

@github-actions github-actions bot added dgram Issues and PRs related to the dgram subsystem / UDP. needs-ci PRs that need a full CI run. labels May 6, 2021
Copy link
Contributor

@RaisinTen RaisinTen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the raw cluster object, what do you think about using lazyLoadCluster()?

function lazyLoadCluster() {
  return cluster ??= require('cluster');
}

That way, the check for whether the module has been loaded is abstracted away and the original internal API too does not change.

@pd4d10
Copy link
Contributor Author

pd4d10 commented May 6, 2021

Instead of using the raw cluster object, what do you think about using lazyLoadCluster()?

function lazyLoadCluster() {
  return cluster ??= require('cluster');
}

That way, the check for whether the module has been loaded is abstracted away and the original internal API too does not change.

Good idea. In this way the lazy load method can be tested.

@pd4d10 pd4d10 changed the title dgram: refactor to remove unreachable code dgram: extract cluster lazy loading method to make it testable May 6, 2021
@pd4d10 pd4d10 requested a review from RaisinTen May 6, 2021 16:42
@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented May 7, 2021

@RaisinTen RaisinTen added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 7, 2021
@jasnell jasnell added fast-track PRs that do not need to wait for 48 hours to land. and removed needs-ci PRs that need a full CI run. labels May 7, 2021
@github-actions
Copy link
Contributor

github-actions bot commented May 7, 2021

Fast-track has been requested by @jasnell. Please 👍 to approve.

Copy link
Member

@Trott Trott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any of this touches a hot code path, but just to be extra cautious, I'll kick off a benchmark CI.

@Trott
Copy link
Member

Trott commented May 8, 2021

Totally unnecessary benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1017/

Results
                                                                       confidence improvement accuracy (*)    (**)   (***)
dgram/array-vs-concat.js dur=5 type='concat' chunks=1 num=100 len=1024                 8.00 %      ±15.78% ±21.00% ±27.34%
dgram/array-vs-concat.js dur=5 type='concat' chunks=1 num=100 len=256                  0.45 %      ±12.49% ±16.62% ±21.63%
dgram/array-vs-concat.js dur=5 type='concat' chunks=1 num=100 len=512                 -4.97 %      ±15.09% ±20.08% ±26.13%
dgram/array-vs-concat.js dur=5 type='concat' chunks=1 num=100 len=64                   1.47 %       ±5.64%  ±7.51%  ±9.77%
dgram/array-vs-concat.js dur=5 type='concat' chunks=2 num=100 len=1024                13.31 %      ±18.14% ±24.13% ±31.41%
dgram/array-vs-concat.js dur=5 type='concat' chunks=2 num=100 len=256                 -6.94 %      ±10.92% ±14.54% ±18.92%
dgram/array-vs-concat.js dur=5 type='concat' chunks=2 num=100 len=512                 -9.03 %      ±16.36% ±21.77% ±28.35%
dgram/array-vs-concat.js dur=5 type='concat' chunks=2 num=100 len=64                   4.37 %       ±5.73%  ±7.65% ±10.01%
dgram/array-vs-concat.js dur=5 type='concat' chunks=4 num=100 len=1024                 7.34 %      ±19.17% ±25.51% ±33.21%
dgram/array-vs-concat.js dur=5 type='concat' chunks=4 num=100 len=256                 -1.29 %      ±14.79% ±19.68% ±25.62%
dgram/array-vs-concat.js dur=5 type='concat' chunks=4 num=100 len=512                 -5.58 %      ±17.42% ±23.18% ±30.17%
dgram/array-vs-concat.js dur=5 type='concat' chunks=4 num=100 len=64                   0.75 %       ±3.65%  ±4.85%  ±6.32%
dgram/array-vs-concat.js dur=5 type='concat' chunks=8 num=100 len=1024                 4.99 %      ±18.52% ±24.64% ±32.07%
dgram/array-vs-concat.js dur=5 type='concat' chunks=8 num=100 len=256                  0.33 %      ±12.84% ±17.08% ±22.23%
dgram/array-vs-concat.js dur=5 type='concat' chunks=8 num=100 len=512                 -9.71 %      ±16.64% ±22.15% ±28.82%
dgram/array-vs-concat.js dur=5 type='concat' chunks=8 num=100 len=64                   3.16 %       ±3.90%  ±5.19%  ±6.78%
dgram/array-vs-concat.js dur=5 type='multi' chunks=1 num=100 len=1024                 -1.46 %      ±12.50% ±16.63% ±21.65%
dgram/array-vs-concat.js dur=5 type='multi' chunks=1 num=100 len=256                   0.89 %      ±10.17% ±13.53% ±17.61%
dgram/array-vs-concat.js dur=5 type='multi' chunks=1 num=100 len=512                   0.24 %      ±10.33% ±13.75% ±17.90%
dgram/array-vs-concat.js dur=5 type='multi' chunks=1 num=100 len=64                   -0.06 %       ±3.37%  ±4.48%  ±5.84%
dgram/array-vs-concat.js dur=5 type='multi' chunks=2 num=100 len=1024                  3.17 %       ±5.85%  ±7.80% ±10.20%
dgram/array-vs-concat.js dur=5 type='multi' chunks=2 num=100 len=256                  -3.69 %       ±9.14% ±12.17% ±15.85%
dgram/array-vs-concat.js dur=5 type='multi' chunks=2 num=100 len=512                  -2.61 %       ±8.71% ±11.59% ±15.09%
dgram/array-vs-concat.js dur=5 type='multi' chunks=2 num=100 len=64                   -0.80 %       ±3.40%  ±4.54%  ±5.94%
dgram/array-vs-concat.js dur=5 type='multi' chunks=4 num=100 len=1024                 -1.57 %       ±5.46%  ±7.29%  ±9.54%
dgram/array-vs-concat.js dur=5 type='multi' chunks=4 num=100 len=256                  -0.96 %       ±6.37%  ±8.49% ±11.07%
dgram/array-vs-concat.js dur=5 type='multi' chunks=4 num=100 len=512                   0.66 %       ±3.42%  ±4.55%  ±5.92%
dgram/array-vs-concat.js dur=5 type='multi' chunks=4 num=100 len=64                    1.35 %       ±2.67%  ±3.56%  ±4.64%
dgram/array-vs-concat.js dur=5 type='multi' chunks=8 num=100 len=1024                  4.05 %       ±8.61% ±11.47% ±14.94%
dgram/array-vs-concat.js dur=5 type='multi' chunks=8 num=100 len=256                  -6.52 %      ±10.01% ±13.33% ±17.37%
dgram/array-vs-concat.js dur=5 type='multi' chunks=8 num=100 len=512                  -5.69 %       ±9.82% ±13.06% ±17.00%
dgram/array-vs-concat.js dur=5 type='multi' chunks=8 num=100 len=64                    0.56 %       ±4.09%  ±5.45%  ±7.09%
dgram/bind-params.js address='false' port='false' n=10000                             -0.03 %       ±5.28%  ±7.04%  ±9.17%
dgram/bind-params.js address='false' port='true' n=10000                              -2.77 %       ±3.97%  ±5.29%  ±6.88%
dgram/bind-params.js address='true' port='true' n=10000                               -4.45 %       ±4.64%  ±6.17%  ±8.04%
dgram/multi-buffer.js dur=5 type='recv' chunks=1 num=100 len=1024                      1.37 %      ±13.43% ±17.87% ±23.25%
dgram/multi-buffer.js dur=5 type='recv' chunks=1 num=100 len=256                      -5.82 %       ±9.66% ±12.85% ±16.73%
dgram/multi-buffer.js dur=5 type='recv' chunks=1 num=100 len=64                       -0.29 %       ±2.24%  ±2.98%  ±3.88%
dgram/multi-buffer.js dur=5 type='recv' chunks=2 num=100 len=1024                     -2.83 %       ±8.68% ±11.55% ±15.04%
dgram/multi-buffer.js dur=5 type='recv' chunks=2 num=100 len=256                       0.93 %       ±8.01% ±10.66% ±13.88%
dgram/multi-buffer.js dur=5 type='recv' chunks=2 num=100 len=64                        1.65 %       ±3.51%  ±4.68%  ±6.13%
dgram/multi-buffer.js dur=5 type='recv' chunks=4 num=100 len=1024                     -0.43 %       ±3.52%  ±4.69%  ±6.12%
dgram/multi-buffer.js dur=5 type='recv' chunks=4 num=100 len=256                       3.22 %       ±3.38%  ±4.52%  ±5.93%
dgram/multi-buffer.js dur=5 type='recv' chunks=4 num=100 len=64                       -2.96 %       ±3.71%  ±4.96%  ±6.49%
dgram/multi-buffer.js dur=5 type='recv' chunks=8 num=100 len=1024                     -6.57 %       ±8.92% ±11.88% ±15.48%
dgram/multi-buffer.js dur=5 type='recv' chunks=8 num=100 len=256                       0.21 %       ±9.23% ±12.28% ±15.99%
dgram/multi-buffer.js dur=5 type='recv' chunks=8 num=100 len=64                        1.94 %       ±4.87%  ±6.48%  ±8.45%
dgram/multi-buffer.js dur=5 type='send' chunks=1 num=100 len=1024                      5.05 %      ±13.85% ±18.43% ±23.98%
dgram/multi-buffer.js dur=5 type='send' chunks=1 num=100 len=256                       1.15 %      ±10.30% ±13.71% ±17.84%
dgram/multi-buffer.js dur=5 type='send' chunks=1 num=100 len=64                       -1.75 %       ±2.35%  ±3.15%  ±4.12%
dgram/multi-buffer.js dur=5 type='send' chunks=2 num=100 len=1024                     -5.01 %       ±8.76% ±11.65% ±15.16%
dgram/multi-buffer.js dur=5 type='send' chunks=2 num=100 len=256                       2.41 %       ±7.36%  ±9.79% ±12.74%
dgram/multi-buffer.js dur=5 type='send' chunks=2 num=100 len=64                        1.69 %       ±3.63%  ±4.83%  ±6.29%
dgram/multi-buffer.js dur=5 type='send' chunks=4 num=100 len=1024                     -0.46 %       ±5.25%  ±6.99%  ±9.12%
dgram/multi-buffer.js dur=5 type='send' chunks=4 num=100 len=256                       3.16 %       ±3.89%  ±5.20%  ±6.82%
dgram/multi-buffer.js dur=5 type='send' chunks=4 num=100 len=64                       -0.41 %       ±4.05%  ±5.38%  ±7.01%
dgram/multi-buffer.js dur=5 type='send' chunks=8 num=100 len=1024                      0.69 %      ±10.89% ±14.50% ±18.87%
dgram/multi-buffer.js dur=5 type='send' chunks=8 num=100 len=256                       8.60 %       ±9.46% ±12.63% ±16.52%
dgram/multi-buffer.js dur=5 type='send' chunks=8 num=100 len=64                        1.48 %       ±3.87%  ±5.16%  ±6.71%
dgram/offset-length.js dur=5 type='recv' num=100 len=1                                 0.84 %       ±1.88%  ±2.50%  ±3.25%
dgram/offset-length.js dur=5 type='recv' num=100 len=1024                              4.81 %      ±12.48% ±16.61% ±21.62%
dgram/offset-length.js dur=5 type='recv' num=100 len=256                               6.77 %      ±10.19% ±13.56% ±17.66%
dgram/offset-length.js dur=5 type='recv' num=100 len=64                               -0.26 %       ±2.97%  ±3.96%  ±5.16%
dgram/offset-length.js dur=5 type='send' num=100 len=1                                -1.36 %       ±2.28%  ±3.04%  ±3.96%
dgram/offset-length.js dur=5 type='send' num=100 len=1024                              8.38 %      ±10.28% ±13.68% ±17.83%
dgram/offset-length.js dur=5 type='send' num=100 len=256                        *    -12.43 %       ±9.51% ±12.66% ±16.49%
dgram/offset-length.js dur=5 type='send' num=100 len=64                               -0.96 %       ±1.93%  ±2.58%  ±3.37%
dgram/single-buffer.js dur=5 type='recv' num=100 len=1                                -0.10 %       ±3.29%  ±4.38%  ±5.70%
dgram/single-buffer.js dur=5 type='recv' num=100 len=1024                              2.33 %      ±15.12% ±20.12% ±26.19%
dgram/single-buffer.js dur=5 type='recv' num=100 len=256                              -3.00 %      ±15.35% ±20.43% ±26.59%
dgram/single-buffer.js dur=5 type='recv' num=100 len=64                               -0.89 %       ±3.76%  ±5.00%  ±6.50%
dgram/single-buffer.js dur=5 type='send' num=100 len=1                                -1.92 %       ±4.19%  ±5.58%  ±7.28%
dgram/single-buffer.js dur=5 type='send' num=100 len=1024                              0.56 %      ±13.72% ±18.25% ±23.75%
dgram/single-buffer.js dur=5 type='send' num=100 len=256                               4.81 %      ±18.09% ±24.07% ±31.33%
dgram/single-buffer.js dur=5 type='send' num=100 len=64                                1.70 %       ±3.76%  ±5.01%  ±6.53%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 75 comparisons, you can thus
expect the following amount of false-positive results:
  3.75 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.75 false positives, when considering a   1% risk acceptance (**, ***),
  0.07 false positives, when considering a 0.1% risk acceptance (***)

@aduh95 aduh95 removed the fast-track PRs that do not need to wait for 48 hours to land. label May 9, 2021
PR-URL: nodejs#38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
@aduh95
Copy link
Contributor

aduh95 commented May 9, 2021

Landed in 1b11398

@aduh95 aduh95 merged commit 1b11398 into nodejs:master May 9, 2021
@pd4d10 pd4d10 deleted the patch-9 branch May 9, 2021 07:17
targos pushed a commit that referenced this pull request May 17, 2021
PR-URL: #38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
targos pushed a commit that referenced this pull request May 30, 2021
PR-URL: #38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
targos pushed a commit that referenced this pull request Jun 5, 2021
PR-URL: #38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
targos pushed a commit that referenced this pull request Jun 5, 2021
PR-URL: #38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
targos pushed a commit that referenced this pull request Jun 11, 2021
PR-URL: #38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. dgram Issues and PRs related to the dgram subsystem / UDP.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants