@jsenv/test 3.6.8 → 3.6.11

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,13 +1,12 @@
1
1
  import { readGitHubWorkflowEnv, startGithubCheckRun } from "@jsenv/github-check-run";
2
2
  import { applyNodeEsmResolution } from "@jsenv/node-esm-resolution";
3
- import { startMonitoringCpuUsage, startMonitoringMemoryUsage, getAvailableMemory, countAvailableCpus, getOsVersion } from "@jsenv/os-metrics";
4
3
  import { readdir, chmod, stat, lstat, chmodSync, statSync, lstatSync, promises, readFile as readFile$1, readdirSync, openSync, closeSync, unlinkSync, rmdirSync, mkdirSync, readFileSync, writeFileSync as writeFileSync$1, unlink, rmdir, existsSync } from "node:fs";
5
4
  import { takeCoverage } from "node:v8";
6
5
  import stripAnsi from "strip-ansi";
7
6
  import { pathToFileURL, fileURLToPath } from "node:url";
8
7
  import { createRequire } from "node:module";
9
8
  import { applyBabelPlugins } from "@jsenv/ast";
10
- import { memoryUsage } from "node:process";
9
+ import { cpuUsage, memoryUsage } from "node:process";
11
10
  import { createSupportsColor, isUnicodeSupported, clearTerminal, eraseLines } from "./jsenv_test_node_modules.js";
12
11
  import { measureTextWidth } from "@jsenv/terminal-text-size";
13
12
  import { createException } from "@jsenv/exception";
@@ -15,12 +14,12 @@ import crypto from "node:crypto";
15
14
  import { dirname } from "node:path";
16
15
  import { createServer } from "node:net";
17
16
  import { spawn, spawnSync, fork } from "node:child_process";
17
+ import { availableParallelism, cpus, totalmem, release, freemem } from "node:os";
18
18
  import { SOURCEMAP, generateSourcemapDataUrl } from "@jsenv/sourcemap";
19
19
  import { injectSupervisorIntoHTML, supervisorFileUrl } from "@jsenv/plugin-supervisor";
20
20
  import { findFreePort } from "@jsenv/server";
21
21
  import { Worker } from "node:worker_threads";
22
22
  import he from "he";
23
- import "node:os";
24
23
  import "node:tty";
25
24
 
26
25
  const createCallbackListNotifiedOnce = () => {
@@ -3734,6 +3733,265 @@ const ensureEmptyDirectory = async (source) => {
3734
3733
  );
3735
3734
  };
3736
3735
 
3736
+ const countAvailableCpus = () => {
3737
+ if (typeof availableParallelism === "function") {
3738
+ return availableParallelism();
3739
+ }
3740
+ const cpuArray = cpus();
3741
+ return cpuArray.length || 1;
3742
+ };
3743
+
3744
+ const getAvailableMemory = () => totalmem();
3745
+
3746
+ const getOsVersion = () => release();
3747
+
3748
+ const startMeasuringTotalCpuUsage = () => {
3749
+ let previousCpuArray = cpus();
3750
+ let previousMs = Date.now();
3751
+ let previousCpuUsage = cpuUsage();
3752
+
3753
+ const overall = {
3754
+ inactive: 100,
3755
+ active: 0,
3756
+ system: 0,
3757
+ user: 0,
3758
+ };
3759
+ const thisProcess = {
3760
+ active: 0,
3761
+ system: 0,
3762
+ user: 0,
3763
+ };
3764
+ const details = previousCpuArray.map(() => {
3765
+ return {
3766
+ inactive: 100,
3767
+ active: 0,
3768
+ system: 0,
3769
+ user: 0,
3770
+ };
3771
+ });
3772
+
3773
+ const samples = [];
3774
+ const interval = setInterval(() => {
3775
+ let cpuArray = cpus();
3776
+ const ms = Date.now();
3777
+ const ellapsedMs = ms - previousMs;
3778
+ const cpuUsageSampleArray = [];
3779
+ let overallSystemMs = 0;
3780
+ let overallUserMs = 0;
3781
+ let overallInactiveMs = 0;
3782
+ let overallActiveMs = 0;
3783
+ let overallMsEllapsed = 0;
3784
+ let index = 0;
3785
+ for (const cpu of cpuArray) {
3786
+ const previousCpuTimes = previousCpuArray[index].times;
3787
+ const cpuTimes = cpu.times;
3788
+ const systemMs = cpuTimes.sys - previousCpuTimes.sys;
3789
+ const userMs = cpuTimes.user - previousCpuTimes.user;
3790
+ const activeMs = systemMs + userMs;
3791
+ const inactiveMs = ellapsedMs - activeMs;
3792
+ const cpuUsageSample = {
3793
+ inactive: inactiveMs / ellapsedMs,
3794
+ active: activeMs / ellapsedMs,
3795
+ system: systemMs / ellapsedMs,
3796
+ user: userMs / ellapsedMs,
3797
+ };
3798
+ cpuUsageSampleArray.push(cpuUsageSample);
3799
+
3800
+ overallSystemMs += systemMs;
3801
+ overallUserMs += userMs;
3802
+ overallInactiveMs += inactiveMs;
3803
+ overallActiveMs += activeMs;
3804
+ overallMsEllapsed += ellapsedMs;
3805
+ index++;
3806
+ }
3807
+ const overallUsageSample = {
3808
+ inactive: overallInactiveMs / overallMsEllapsed,
3809
+ active: overallActiveMs / overallMsEllapsed,
3810
+ system: overallSystemMs / overallMsEllapsed,
3811
+ user: overallUserMs / overallMsEllapsed,
3812
+ };
3813
+ previousCpuArray = cpuArray;
3814
+ previousMs = ms;
3815
+
3816
+ const processCpuUsage = cpuUsage();
3817
+ const thisProcessSystemMs = Math.round(
3818
+ (processCpuUsage.system - previousCpuUsage.system) / 1000,
3819
+ );
3820
+ const thisProcessUserMs = Math.round(
3821
+ (processCpuUsage.user - previousCpuUsage.user) / 1000,
3822
+ );
3823
+ previousCpuUsage = processCpuUsage;
3824
+
3825
+ const thisProcessActiveMs = thisProcessSystemMs + thisProcessUserMs;
3826
+ const thisProcessInactiveMs = overallMsEllapsed - thisProcessActiveMs;
3827
+ const thisProcessSample = {
3828
+ inactive: thisProcessInactiveMs / overallMsEllapsed,
3829
+ active: thisProcessActiveMs / overallMsEllapsed,
3830
+ system: thisProcessSystemMs / overallMsEllapsed,
3831
+ user: thisProcessUserMs / overallMsEllapsed,
3832
+ };
3833
+ samples.push({
3834
+ cpuUsageSampleArray,
3835
+ overallUsageSample,
3836
+ thisProcessSample,
3837
+ });
3838
+ if (samples.length === 10) {
3839
+ {
3840
+ let index = 0;
3841
+ for (const detail of details) {
3842
+ let systemSum = 0;
3843
+ let userSum = 0;
3844
+ let inactiveSum = 0;
3845
+ let activeSum = 0;
3846
+ for (const sample of samples) {
3847
+ const { cpuUsageSampleArray } = sample;
3848
+ const cpuUsageSample = cpuUsageSampleArray[index];
3849
+ inactiveSum += cpuUsageSample.inactive;
3850
+ activeSum += cpuUsageSample.active;
3851
+ systemSum += cpuUsageSample.system;
3852
+ userSum += cpuUsageSample.user;
3853
+ }
3854
+ Object.assign(detail, {
3855
+ inactive: inactiveSum / samples.length,
3856
+ active: activeSum / samples.length,
3857
+ system: systemSum / samples.length,
3858
+ user: userSum / samples.length,
3859
+ });
3860
+ index++;
3861
+ }
3862
+ }
3863
+ {
3864
+ let overallSystemSum = 0;
3865
+ let overallUserSum = 0;
3866
+ let overallInactiveSum = 0;
3867
+ let overallActiveSum = 0;
3868
+ for (const sample of samples) {
3869
+ const { overallUsageSample } = sample;
3870
+ overallSystemSum += overallUsageSample.system;
3871
+ overallUserSum += overallUsageSample.user;
3872
+ overallInactiveSum += overallUsageSample.inactive;
3873
+ overallActiveSum += overallUsageSample.active;
3874
+ }
3875
+ Object.assign(overall, {
3876
+ inactive: overallInactiveSum / samples.length,
3877
+ active: overallActiveSum / samples.length,
3878
+ system: overallSystemSum / samples.length,
3879
+ user: overallUserSum / samples.length,
3880
+ });
3881
+ }
3882
+ {
3883
+ let thisProcessSystemSum = 0;
3884
+ let thisProcessUserSum = 0;
3885
+ let thisProcessInactiveSum = 0;
3886
+ let thisProcessActiveSum = 0;
3887
+ for (const sample of samples) {
3888
+ const { thisProcessSample } = sample;
3889
+ thisProcessSystemSum += thisProcessSample.system;
3890
+ thisProcessUserSum += thisProcessSample.user;
3891
+ thisProcessInactiveSum += thisProcessSample.inactive;
3892
+ thisProcessActiveSum += thisProcessSample.active;
3893
+ }
3894
+ Object.assign(thisProcess, {
3895
+ inactive: thisProcessInactiveSum / samples.length,
3896
+ active: thisProcessActiveSum / samples.length,
3897
+ system: thisProcessSystemSum / samples.length,
3898
+ user: thisProcessUserSum / samples.length,
3899
+ });
3900
+ }
3901
+ samples.length = 0;
3902
+ }
3903
+ }, 15);
3904
+ interval.unref();
3905
+
3906
+ return {
3907
+ overall,
3908
+ thisProcess,
3909
+ details,
3910
+ stop: () => {
3911
+ clearInterval(interval);
3912
+ },
3913
+ };
3914
+ };
3915
+
3916
+ const startMonitoringMetric = (measure) => {
3917
+ const metrics = [];
3918
+ const takeMeasure = () => {
3919
+ const value = measure();
3920
+ metrics.push(value);
3921
+ return value;
3922
+ };
3923
+
3924
+ const info = {
3925
+ start: takeMeasure(),
3926
+ min: null,
3927
+ max: null,
3928
+ median: null,
3929
+ end: null,
3930
+ };
3931
+ return {
3932
+ info,
3933
+ measure: takeMeasure,
3934
+ end: () => {
3935
+ info.end = takeMeasure();
3936
+ metrics.sort((a, b) => a - b);
3937
+ info.min = metrics[0];
3938
+ info.max = metrics[metrics.length - 1];
3939
+ info.median = medianFromSortedArray(metrics);
3940
+ metrics.length = 0;
3941
+ },
3942
+ };
3943
+ };
3944
+
3945
+ const medianFromSortedArray = (array) => {
3946
+ const length = array.length;
3947
+ const isOdd = length % 2 === 1;
3948
+ if (isOdd) {
3949
+ const medianNumberIndex = (length - 1) / 2;
3950
+ const medianNumber = array[medianNumberIndex];
3951
+ return medianNumber;
3952
+ }
3953
+ const rightMiddleNumberIndex = length / 2;
3954
+ const leftMiddleNumberIndex = rightMiddleNumberIndex - 1;
3955
+ const leftMiddleNumber = array[leftMiddleNumberIndex];
3956
+ const rightMiddleNumber = array[rightMiddleNumberIndex];
3957
+ const medianNumber = (leftMiddleNumber + rightMiddleNumber) / 2;
3958
+ return medianNumber;
3959
+ };
3960
+
3961
+ // https://gist.github.com/GaetanoPiazzolla/c40e1ebb9f709d091208e89baf9f4e00
3962
+
3963
+
3964
+ const startMonitoringCpuUsage = () => {
3965
+ const cpuUsage = startMeasuringTotalCpuUsage();
3966
+ const processCpuUsageMonitoring = startMonitoringMetric(() => {
3967
+ return cpuUsage.thisProcess.active;
3968
+ });
3969
+ const osCpuUsageMonitoring = startMonitoringMetric(() => {
3970
+ return cpuUsage.overall.active;
3971
+ });
3972
+ const result = [processCpuUsageMonitoring, osCpuUsageMonitoring];
3973
+ result.stop = cpuUsage.stop;
3974
+ return result;
3975
+ };
3976
+
3977
+ const startMonitoringMemoryUsage = () => {
3978
+ const processMemoryUsageMonitoring = startMonitoringMetric(() => {
3979
+ return memoryUsage().rss;
3980
+ });
3981
+ const osMemoryUsageMonitoring = startMonitoringMetric(() => {
3982
+ const total = totalmem();
3983
+ const free = freemem();
3984
+ return total - free;
3985
+ });
3986
+ const stop = () => {
3987
+ processMemoryUsageMonitoring.stop();
3988
+ osMemoryUsageMonitoring.stop();
3989
+ };
3990
+ const result = [processMemoryUsageMonitoring, osMemoryUsageMonitoring];
3991
+ result.stop = stop;
3992
+ return result;
3993
+ };
3994
+
3737
3995
  const normalizeFileByFileCoveragePaths = (
3738
3996
  fileByFileCoverage,
3739
3997
  rootDirectoryUrl,
@@ -6367,6 +6625,14 @@ const executeTestPlan = async ({
6367
6625
  const [processMemoryUsageMonitoring, osMemoryUsageMonitoring] =
6368
6626
  memoryMonitoring;
6369
6627
 
6628
+ const interval = setInterval(() => {
6629
+ processCpuUsageMonitoring.measure();
6630
+ processMemoryUsageMonitoring.measure();
6631
+ }, 500).unref();
6632
+ operation.addEndCallback(() => {
6633
+ clearInterval(interval);
6634
+ });
6635
+
6370
6636
  const timingsOrigin = Date.now();
6371
6637
  const takeTiming = () => {
6372
6638
  return Date.now() - timingsOrigin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/test",
3
- "version": "3.6.8",
3
+ "version": "3.6.11",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -44,7 +44,7 @@
44
44
  "@jsenv/ast": "6.6.7",
45
45
  "@jsenv/exception": "1.1.7",
46
46
  "@jsenv/filesystem": "4.14.6",
47
- "@jsenv/github-check-run": "0.2.21",
47
+ "@jsenv/github-check-run": "0.2.22",
48
48
  "@jsenv/node-esm-resolution": "1.2.0",
49
49
  "@jsenv/humanize": "1.4.0",
50
50
  "@jsenv/plugin-supervisor": "1.6.12",
@@ -168,6 +168,14 @@ export const executeTestPlan = async ({
168
168
  const [processMemoryUsageMonitoring, osMemoryUsageMonitoring] =
169
169
  memoryMonitoring;
170
170
 
171
+ const interval = setInterval(() => {
172
+ processCpuUsageMonitoring.measure();
173
+ processMemoryUsageMonitoring.measure();
174
+ }, 500).unref();
175
+ operation.addEndCallback(() => {
176
+ clearInterval(interval);
177
+ });
178
+
171
179
  const timingsOrigin = Date.now();
172
180
  const takeTiming = () => {
173
181
  return Date.now() - timingsOrigin;