Просмотр исходного кода

chore(dev-server): Add simple memory profiler

Michael Bromley 3 лет назад
Родитель
Сommit
9524d763dd
1 измененных файлов с 25 добавлено и 0 удалено
  1. 25 0
      packages/dev-server/memory-profiler.ts

+ 25 - 0
packages/dev-server/memory-profiler.ts

@@ -0,0 +1,25 @@
+/* tslint:disable */
+/**
+ * Used to profile peak memory usage for perf optimization purposes
+ *
+ * Add it to the index.ts or index-worker.ts:
+ * ```ts
+ * import { profileMemory } from './memory-profiler';
+ *
+ * profileMemory();
+ * ```
+ */
+export function profileMemory() {
+    let max = 0;
+    setInterval(() => {
+        const rss = process.memoryUsage().rss;
+        if (max < rss) {
+            max = rss;
+            console.log(`Peak: ${inMb(max)}`);
+        }
+    }, 500);
+}
+
+function inMb(bytes: number) {
+    return `${Math.round((bytes / 1024 / 1024) * 100) / 100}MB;`;
+}