utility.service.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import _ = require("lodash")
  2. export class UtilityService {
  3. // Check Heap size
  4. // const v8 = require('v8');
  5. // const heapStats = v8.getHeapStatistics();
  6. // const maxHeapSize = heapStats.heap_size_limit / (1024 * 1024); // Convert to MB
  7. // console.log(`Current maximum heap size: ${maxHeapSize.toFixed(2)} MB`);
  8. // const used = process.memoryUsage().heapUsed / 1024 / 1024;
  9. // const total = process.memoryUsage().heapTotal / 1024 / 1024;
  10. // console.log(`Heap memory usage: ${used.toFixed(2)} MB`);
  11. // console.log(`Total heap size: ${total.toFixed(2)} MB`);
  12. public callFromOtherClass() {
  13. const t0 = performance.now()
  14. let i
  15. for (i = 0; i <= 6000000000; i++) {
  16. }
  17. const t1 = performance.now()
  18. const timeTakenInSeconds = (t1 - t0) / 1000;
  19. console.log(`Time taken: ${timeTakenInSeconds} seconds to run this function`);
  20. }
  21. public checkHeapSize(): any {
  22. let currentHeapSize = process.memoryUsage().heapUsed / 1024 / 1024;
  23. let allocatedHeapSize = 512;
  24. let percentage = (currentHeapSize / allocatedHeapSize) * 100;
  25. console.log(`Consumer_! Heap currentHeapSize: ${currentHeapSize.toFixed(2)} MB. Percentage: ${percentage} %`);
  26. return percentage
  27. }
  28. public checkMaxHeap() {
  29. let v8 = require('v8');
  30. let heapStats = v8.getHeapStatistics();
  31. let maxHeapSize = heapStats.heap_size_limit / (1024 * 1024); // Convert to MB
  32. console.log(`Current maximum heap size: ${maxHeapSize.toFixed(2)} MB`);
  33. }
  34. public checkCPU() {
  35. let os = require('os')
  36. let cpuUsage = os.cpus()
  37. console.log(cpuUsage)
  38. }
  39. }