common.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.commonState = exports.READY = void 0;
  4. exports.isTransferable = isTransferable;
  5. exports.isMovable = isMovable;
  6. exports.markMovable = markMovable;
  7. exports.createHistogramSummary = createHistogramSummary;
  8. exports.toHistogramIntegerNano = toHistogramIntegerNano;
  9. exports.maybeFileURLToPath = maybeFileURLToPath;
  10. exports.getAvailableParallelism = getAvailableParallelism;
  11. const node_url_1 = require("node:url");
  12. const node_os_1 = require("node:os");
  13. const symbols_1 = require("./symbols");
  14. // States wether the worker is ready to receive tasks
  15. exports.READY = '_WORKER_READY';
  16. /**
  17. * True if the object implements the Transferable interface
  18. *
  19. * @export
  20. * @param {unknown} value
  21. * @return {*} {boolean}
  22. */
  23. function isTransferable(value) {
  24. return (value != null &&
  25. typeof value === 'object' &&
  26. symbols_1.kTransferable in value &&
  27. symbols_1.kValue in value);
  28. }
  29. /**
  30. * True if object implements Transferable and has been returned
  31. * by the Piscina.move() function
  32. *
  33. * TODO: narrow down the type of value
  34. * @export
  35. * @param {(unknown & PiscinaMovable)} value
  36. * @return {*} {boolean}
  37. */
  38. function isMovable(value) {
  39. return isTransferable(value) && value[symbols_1.kMovable] === true;
  40. }
  41. function markMovable(value) {
  42. Object.defineProperty(value, symbols_1.kMovable, {
  43. enumerable: false,
  44. configurable: true,
  45. writable: true,
  46. value: true
  47. });
  48. }
  49. // State of Piscina pool
  50. exports.commonState = {
  51. isWorkerThread: false,
  52. workerData: undefined
  53. };
  54. function createHistogramSummary(histogram) {
  55. const { mean, stddev, min, max } = histogram;
  56. return {
  57. average: mean / 1000,
  58. mean: mean / 1000,
  59. stddev,
  60. min: min / 1000,
  61. max: max / 1000,
  62. p0_001: histogram.percentile(0.001) / 1000,
  63. p0_01: histogram.percentile(0.01) / 1000,
  64. p0_1: histogram.percentile(0.1) / 1000,
  65. p1: histogram.percentile(1) / 1000,
  66. p2_5: histogram.percentile(2.5) / 1000,
  67. p10: histogram.percentile(10) / 1000,
  68. p25: histogram.percentile(25) / 1000,
  69. p50: histogram.percentile(50) / 1000,
  70. p75: histogram.percentile(75) / 1000,
  71. p90: histogram.percentile(90) / 1000,
  72. p97_5: histogram.percentile(97.5) / 1000,
  73. p99: histogram.percentile(99) / 1000,
  74. p99_9: histogram.percentile(99.9) / 1000,
  75. p99_99: histogram.percentile(99.99) / 1000,
  76. p99_999: histogram.percentile(99.999) / 1000
  77. };
  78. }
  79. function toHistogramIntegerNano(milliseconds) {
  80. return Math.max(1, Math.trunc(milliseconds * 1000));
  81. }
  82. function maybeFileURLToPath(filename) {
  83. return filename.startsWith('file:')
  84. ? (0, node_url_1.fileURLToPath)(new node_url_1.URL(filename))
  85. : filename;
  86. }
  87. // TODO: drop on v5
  88. function getAvailableParallelism() {
  89. if (typeof node_os_1.availableParallelism === 'function') {
  90. return (0, node_os_1.availableParallelism)();
  91. }
  92. try {
  93. return (0, node_os_1.cpus)().length;
  94. }
  95. catch {
  96. return 1;
  97. }
  98. }
  99. //# sourceMappingURL=common.js.map