getMaxWorkers.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = getMaxWorkers;
  6. function _os() {
  7. const data = _interopRequireDefault(require('os'));
  8. _os = function _os() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {default: obj};
  15. }
  16. /**
  17. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  18. *
  19. * This source code is licensed under the MIT license found in the
  20. * LICENSE file in the root directory of this source tree.
  21. */
  22. function getMaxWorkers(argv, defaultOptions) {
  23. if (argv.runInBand) {
  24. return 1;
  25. } else if (argv.maxWorkers) {
  26. return parseWorkers(argv.maxWorkers);
  27. } else if (defaultOptions && defaultOptions.maxWorkers) {
  28. return parseWorkers(defaultOptions.maxWorkers);
  29. } else {
  30. // In watch mode, Jest should be unobtrusive and not use all available CPUs.
  31. const cpus = _os().default.cpus() ? _os().default.cpus().length : 1;
  32. return Math.max(argv.watch ? Math.floor(cpus / 2) : cpus - 1, 1);
  33. }
  34. }
  35. const parseWorkers = maxWorkers => {
  36. const parsed = parseInt(maxWorkers.toString(), 10);
  37. if (
  38. typeof maxWorkers === 'string' &&
  39. maxWorkers.trim().endsWith('%') &&
  40. parsed > 0 &&
  41. parsed <= 100
  42. ) {
  43. const cpus = _os().default.cpus().length;
  44. const workers = Math.floor((parsed / 100) * cpus);
  45. return workers >= 1 ? workers : 1;
  46. }
  47. return parsed > 0 ? parsed : 1;
  48. };