Guest User

Untitled

a guest
Jan 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. const { Worker } = require("worker_threads");
  2. const path = require("path");
  3. const os = require("os");
  4. const ora = require("ora");
  5.  
  6. // this is how we can get the count of cpu's the computer has,
  7. // using a larger number may result in the app crushing
  8. const cpuCount = os.cpus().length;
  9.  
  10. // create some big array
  11. const elements = 5000000;
  12. console.log(`generating ${elements} random numbers...`)
  13. const bigArray = Array(elements)
  14. .fill()
  15. .map(() => Math.random());
  16.  
  17. // we get the path of the script
  18. const workerScript = path.join(__dirname, "./sorter.js");
  19.  
  20. // we turn the worker activation into a promise
  21. const sortArrayWithWorker = arr => {
  22. return new Promise((resolve, reject) => {
  23. const worker = new Worker(workerScript, { workerData: arr });
  24. worker.on("message", resolve);
  25. worker.on("error", reject);
  26. });
  27. };
  28.  
  29. // this function will distribute the array across workers
  30. async function distributeLoadAcrossWorkers(workers) {
  31. // how many elements each worker should sort
  32. const segmentsPerWorker = Math.round(bigArray.length / workers);
  33. const promises = Array(workers)
  34. .fill()
  35. .map((_, index) => {
  36. let arrayToSort;
  37. if (index === 0) {
  38. // the first segment
  39. arrayToSort = bigArray.slice(0, segmentsPerWorker);
  40. } else if (index === workers - 1) {
  41. // the last segment
  42. arrayToSort = bigArray.slice(segmentsPerWorker * index);
  43. } else {
  44. // intermediate segments
  45. arrayToSort = bigArray.slice(segmentsPerWorker * index,segmentsPerWorker * (index + 1))
  46. }
  47. return sortArrayWithWorker(arrayToSort)
  48. });
  49. // merge all the segments of the array
  50. const segmentsResults = await Promise.all(promises);
  51. return segmentsResults.reduce((acc, arr) => acc.concat(arr), []);
  52. }
  53.  
  54. // this is the main function (it's only to allow the use of async await for simplicity)
  55. async function run() {
  56. const spinner = ora("Loading unicorns").start();
  57. spinner.color = "yellow";
  58. spinner.text = "sorting... this may take a while...";
  59.  
  60. // sort with a single worker
  61. const start1 = Date.now();
  62. const result1 = await distributeLoadAcrossWorkers(1);
  63. console.log(
  64. `sorted ${result1.length} items, with 1 worker in ${Date.now() - start}ms`
  65. );
  66.  
  67. // sort with no worker at all
  68. let start2 = Date.now();
  69. const result2 = bigArray.sort((a, b) => a - b);
  70. console.log(
  71. `sorted ${result2.length} items, without workers in ${Date.now() - start2}ms`
  72. );
  73.  
  74. // sort with multiple workers, based on the cpu count
  75. const start3 = Date.now();
  76. const result3 = await distributeLoadAcrossWorkers(cpuCount);
  77. console.log(
  78. `sorted ${result3.length} items, with ${cpuCount} workers in ${Date.now() - start3}ms`
  79. );
  80.  
  81. spinner.stop();
  82. console.log("\n done");
  83. }
  84.  
  85. run();
Add Comment
Please, Sign In to add comment