@jsenv/test 3.0.8 → 3.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,10 @@
1
- import os, { cpus, totalmem, freemem, availableParallelism } from "node:os";
1
+ import os, { cpus, totalmem, availableParallelism } from "node:os";
2
+ import process$1, { cpuUsage, memoryUsage } from "node:process";
2
3
  import { readdir, chmod, stat, lstat, promises, readFile as readFile$1, writeFileSync as writeFileSync$1, mkdirSync, unlink, openSync, closeSync, rmdir, chmodSync, statSync, lstatSync, unlinkSync, readdirSync, rmdirSync, readFileSync, existsSync } from "node:fs";
3
4
  import { takeCoverage } from "node:v8";
4
5
  import stripAnsi from "strip-ansi";
5
6
  import { URL_META, createException } from "./js/exception.js";
6
7
  import stringWidth from "string-width";
7
- import process$1, { memoryUsage } from "node:process";
8
8
  import tty from "node:tty";
9
9
  import { pathToFileURL, fileURLToPath } from "node:url";
10
10
  import { dirname } from "node:path";
@@ -3292,9 +3292,10 @@ process.platform === "linux";
3292
3292
  // https://gist.github.com/GaetanoPiazzolla/c40e1ebb9f709d091208e89baf9f4e00
3293
3293
 
3294
3294
 
3295
- const startMeasuringCpuUsage = () => {
3295
+ const startMeasuringTotalCpuUsage = () => {
3296
3296
  let previousCpuArray = cpus();
3297
3297
  let previousMs = Date.now();
3298
+ let previousCpuUsage = cpuUsage();
3298
3299
 
3299
3300
  const overall = {
3300
3301
  inactive: 100,
@@ -3302,6 +3303,12 @@ const startMeasuringCpuUsage = () => {
3302
3303
  system: 0,
3303
3304
  user: 0,
3304
3305
  };
3306
+
3307
+ const thisProcess = {
3308
+ active: 0,
3309
+ system: 0,
3310
+ user: 0,
3311
+ };
3305
3312
  const details = previousCpuArray.map(() => {
3306
3313
  return {
3307
3314
  inactive: 100,
@@ -3358,6 +3365,19 @@ const startMeasuringCpuUsage = () => {
3358
3365
  overallUsageSample,
3359
3366
  });
3360
3367
 
3368
+ const processCpuUsage = cpuUsage();
3369
+ const thisProcessSystemMs = Math.round(
3370
+ (processCpuUsage.system - previousCpuUsage.system) / 1000,
3371
+ );
3372
+ const thisProcessUserMs = Math.round(
3373
+ (processCpuUsage.user - previousCpuUsage.user) / 1000,
3374
+ );
3375
+ previousCpuUsage = processCpuUsage;
3376
+ const thisProcessActiveMs = thisProcessSystemMs + thisProcessUserMs;
3377
+ thisProcess.active = thisProcessActiveMs / overallActiveMs;
3378
+ thisProcess.system = thisProcessSystemMs / overallSystemMs;
3379
+ thisProcess.user = thisProcessUserMs / overallUserMs;
3380
+
3361
3381
  if (samples.length === 10) {
3362
3382
  let index = 0;
3363
3383
  for (const detail of details) {
@@ -3407,6 +3427,7 @@ const startMeasuringCpuUsage = () => {
3407
3427
 
3408
3428
  return {
3409
3429
  overall,
3430
+ thisProcess,
3410
3431
  details,
3411
3432
  stop: () => {
3412
3433
  clearInterval(interval);
@@ -5671,8 +5692,8 @@ const coverageDefault = {
5671
5692
  };
5672
5693
  const parallelDefault = {
5673
5694
  max: "80%", // percentage resolved against the available cpus
5674
- maxCpu: "90%",
5675
- maxMemory: "90%",
5695
+ maxCpu: "80%",
5696
+ maxMemory: "50%",
5676
5697
  };
5677
5698
 
5678
5699
  const executeTestPlan = async ({
@@ -5780,7 +5801,7 @@ const executeTestPlan = async ({
5780
5801
  let finalizeCoverage;
5781
5802
 
5782
5803
  try {
5783
- const cpuUsage = startMeasuringCpuUsage();
5804
+ const cpuUsage = startMeasuringTotalCpuUsage();
5784
5805
  operation.addEndCallback(cpuUsage.stop);
5785
5806
 
5786
5807
  let logger;
@@ -5886,7 +5907,8 @@ const executeTestPlan = async ({
5886
5907
  const maxMemory = parallel.maxMemory;
5887
5908
  if (typeof maxMemory === "string") {
5888
5909
  const maxMemoryAsRatio = assertPercentageAndConvertToRatio(maxMemory);
5889
- parallel.maxMemory = Math.round(maxMemoryAsRatio * totalmem());
5910
+ const totalMemory = totalmem();
5911
+ parallel.maxMemory = Math.round(maxMemoryAsRatio * totalMemory);
5890
5912
  } else if (typeof maxMemory !== "number") {
5891
5913
  throw new TypeError(
5892
5914
  `parallel.maxMemory must be a number or a percentage, got ${maxMemory}`,
@@ -6424,17 +6446,15 @@ To fix this warning:
6424
6446
  // if nothing is executing these limitations don't apply
6425
6447
  executionExecutingSet.size > 0
6426
6448
  ) {
6427
- const availableMemory = freemem();
6428
- const totalMemory = totalmem();
6429
- const usedMemory = totalMemory - availableMemory;
6430
- if (usedMemory > parallel.maxMemory) {
6449
+ const memoryUsedByThisProcess = memoryUsage().rss;
6450
+ if (memoryUsedByThisProcess > parallel.maxMemory) {
6431
6451
  // retry after Xms in case memory usage decreases
6432
6452
  const promise = operation.wait(200);
6433
6453
  promises.push(promise);
6434
6454
  break;
6435
6455
  }
6436
6456
 
6437
- if (cpuUsage.overall.active > parallel.maxCpu) {
6457
+ if (cpuUsage.thisProcess.active > parallel.maxCpu) {
6438
6458
  // retry after Xms in case cpu usage decreases
6439
6459
  const promise = operation.wait(200);
6440
6460
  promises.push(promise);
@@ -6450,7 +6470,10 @@ To fix this warning:
6450
6470
  break;
6451
6471
  }
6452
6472
  if (execution) {
6453
- const promise = start(execution);
6473
+ const promise = (async () => {
6474
+ await start(execution);
6475
+ await startAsMuchAsPossible();
6476
+ })();
6454
6477
  promises.push(promise);
6455
6478
  }
6456
6479
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/test",
3
- "version": "3.0.8",
3
+ "version": "3.0.10",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -2,7 +2,8 @@
2
2
  *
3
3
  */
4
4
 
5
- import { cpus, availableParallelism, freemem, totalmem } from "node:os";
5
+ import { cpus, availableParallelism, totalmem } from "node:os";
6
+ import { memoryUsage } from "node:process";
6
7
  import { existsSync } from "node:fs";
7
8
  import { takeCoverage } from "node:v8";
8
9
  import stripAnsi from "strip-ansi";
@@ -19,7 +20,7 @@ import {
19
20
  readGitHubWorkflowEnv,
20
21
  } from "@jsenv/github-check-run";
21
22
 
22
- import { startMeasuringCpuUsage } from "../helpers/cpu_usage.js";
23
+ import { startMeasuringTotalCpuUsage } from "../helpers/cpu_usage.js";
23
24
  import { createCallOrderer } from "../helpers/call_orderer.js";
24
25
  import { generateCoverage } from "../coverage/generate_coverage.js";
25
26
  import { assertAndNormalizeWebServer } from "./web_server_param.js";
@@ -87,8 +88,8 @@ const coverageDefault = {
87
88
  };
88
89
  const parallelDefault = {
89
90
  max: "80%", // percentage resolved against the available cpus
90
- maxCpu: "90%",
91
- maxMemory: "90%",
91
+ maxCpu: "80%",
92
+ maxMemory: "50%",
92
93
  };
93
94
 
94
95
  export const executeTestPlan = async ({
@@ -196,7 +197,7 @@ export const executeTestPlan = async ({
196
197
  let finalizeCoverage;
197
198
 
198
199
  try {
199
- const cpuUsage = startMeasuringCpuUsage();
200
+ const cpuUsage = startMeasuringTotalCpuUsage();
200
201
  operation.addEndCallback(cpuUsage.stop);
201
202
 
202
203
  let logger;
@@ -302,7 +303,8 @@ export const executeTestPlan = async ({
302
303
  const maxMemory = parallel.maxMemory;
303
304
  if (typeof maxMemory === "string") {
304
305
  const maxMemoryAsRatio = assertPercentageAndConvertToRatio(maxMemory);
305
- parallel.maxMemory = Math.round(maxMemoryAsRatio * totalmem());
306
+ const totalMemory = totalmem();
307
+ parallel.maxMemory = Math.round(maxMemoryAsRatio * totalMemory);
306
308
  } else if (typeof maxMemory !== "number") {
307
309
  throw new TypeError(
308
310
  `parallel.maxMemory must be a number or a percentage, got ${maxMemory}`,
@@ -840,17 +842,15 @@ To fix this warning:
840
842
  // if nothing is executing these limitations don't apply
841
843
  executionExecutingSet.size > 0
842
844
  ) {
843
- const availableMemory = freemem();
844
- const totalMemory = totalmem();
845
- const usedMemory = totalMemory - availableMemory;
846
- if (usedMemory > parallel.maxMemory) {
845
+ const memoryUsedByThisProcess = memoryUsage().rss;
846
+ if (memoryUsedByThisProcess > parallel.maxMemory) {
847
847
  // retry after Xms in case memory usage decreases
848
848
  const promise = operation.wait(200);
849
849
  promises.push(promise);
850
850
  break;
851
851
  }
852
852
 
853
- if (cpuUsage.overall.active > parallel.maxCpu) {
853
+ if (cpuUsage.thisProcess.active > parallel.maxCpu) {
854
854
  // retry after Xms in case cpu usage decreases
855
855
  const promise = operation.wait(200);
856
856
  promises.push(promise);
@@ -866,7 +866,10 @@ To fix this warning:
866
866
  break;
867
867
  }
868
868
  if (execution) {
869
- const promise = start(execution);
869
+ const promise = (async () => {
870
+ await start(execution);
871
+ await startAsMuchAsPossible();
872
+ })();
870
873
  promises.push(promise);
871
874
  }
872
875
  }
@@ -1,10 +1,12 @@
1
1
  // https://gist.github.com/GaetanoPiazzolla/c40e1ebb9f709d091208e89baf9f4e00
2
2
 
3
3
  import { cpus } from "node:os";
4
+ import { cpuUsage } from "node:process";
4
5
 
5
- export const startMeasuringCpuUsage = () => {
6
+ export const startMeasuringTotalCpuUsage = () => {
6
7
  let previousCpuArray = cpus();
7
8
  let previousMs = Date.now();
9
+ let previousCpuUsage = cpuUsage();
8
10
 
9
11
  const overall = {
10
12
  inactive: 100,
@@ -12,6 +14,12 @@ export const startMeasuringCpuUsage = () => {
12
14
  system: 0,
13
15
  user: 0,
14
16
  };
17
+
18
+ const thisProcess = {
19
+ active: 0,
20
+ system: 0,
21
+ user: 0,
22
+ };
15
23
  const details = previousCpuArray.map(() => {
16
24
  return {
17
25
  inactive: 100,
@@ -68,6 +76,19 @@ export const startMeasuringCpuUsage = () => {
68
76
  overallUsageSample,
69
77
  });
70
78
 
79
+ const processCpuUsage = cpuUsage();
80
+ const thisProcessSystemMs = Math.round(
81
+ (processCpuUsage.system - previousCpuUsage.system) / 1000,
82
+ );
83
+ const thisProcessUserMs = Math.round(
84
+ (processCpuUsage.user - previousCpuUsage.user) / 1000,
85
+ );
86
+ previousCpuUsage = processCpuUsage;
87
+ const thisProcessActiveMs = thisProcessSystemMs + thisProcessUserMs;
88
+ thisProcess.active = thisProcessActiveMs / overallActiveMs;
89
+ thisProcess.system = thisProcessSystemMs / overallSystemMs;
90
+ thisProcess.user = thisProcessUserMs / overallUserMs;
91
+
71
92
  if (samples.length === 10) {
72
93
  let index = 0;
73
94
  for (const detail of details) {
@@ -117,6 +138,7 @@ export const startMeasuringCpuUsage = () => {
117
138
 
118
139
  return {
119
140
  overall,
141
+ thisProcess,
120
142
  details,
121
143
  stop: () => {
122
144
  clearInterval(interval);
@@ -140,3 +162,35 @@ export const ratioAsPercentage = (ratio) => {
140
162
  const percentage = `${percentageAsNumberRounded}%`;
141
163
  return percentage;
142
164
  };
165
+
166
+ export const startMeasuringCpuUsage = () => {
167
+ let previousHrtime = process.hrtime();
168
+ let previousCpuUsage = process.cpuUsage();
169
+
170
+ const info = {
171
+ used: 0,
172
+ };
173
+
174
+ const interval = setInterval(() => {
175
+ const hrtime = process.hrtime(previousHrtime);
176
+ previousHrtime = hrtime;
177
+ const cpuUsage = process.cpuUsage(previousCpuUsage);
178
+ previousCpuUsage = cpuUsage;
179
+
180
+ const ellapsedMs = hrtime[0] * 1000 + hrtime[1] / 1000000;
181
+ const userMs = Math.round(cpuUsage.user / 1000);
182
+ const systemMs = Math.round(cpuUsage.system / 1000);
183
+ const activeMs = userMs + systemMs;
184
+ const usedRatio = activeMs / ellapsedMs;
185
+ const used = Math.round(usedRatio * 100) / 100;
186
+ info.used = used;
187
+ }, 15);
188
+ interval.unref();
189
+
190
+ return {
191
+ info,
192
+ stop: () => {
193
+ clearInterval(interval);
194
+ },
195
+ };
196
+ };
@@ -1,7 +1,15 @@
1
- import { startMeasuringCpuUsage, formatUsage } from "./cpu_usage.js";
1
+ import {
2
+ startMeasuringCpuUsage,
3
+ startMeasuringTotalCpuUsage,
4
+ formatUsage,
5
+ } from "./cpu_usage.js";
2
6
 
3
7
  const cpuUsage = startMeasuringCpuUsage();
8
+ setInterval(() => {
9
+ console.log(cpuUsage.info.used);
10
+ }, 400);
4
11
 
12
+ const totalCpuUsage = startMeasuringTotalCpuUsage();
5
13
  setInterval(() => {
6
- console.log(formatUsage(cpuUsage.overall));
14
+ console.log(formatUsage(totalCpuUsage.overall));
7
15
  }, 400);