xref: /third_party/node/test/common/v8.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst assert = require('assert');
31cb0ef41Sopenharmony_ciconst { GCProfiler } = require('v8');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cifunction collectGCProfile({ duration }) {
61cb0ef41Sopenharmony_ci  return new Promise((resolve) => {
71cb0ef41Sopenharmony_ci    const profiler = new GCProfiler();
81cb0ef41Sopenharmony_ci    profiler.start();
91cb0ef41Sopenharmony_ci    setTimeout(() => {
101cb0ef41Sopenharmony_ci      resolve(profiler.stop());
111cb0ef41Sopenharmony_ci    }, duration);
121cb0ef41Sopenharmony_ci  });
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifunction checkGCProfile(data) {
161cb0ef41Sopenharmony_ci  assert.ok(data.version > 0);
171cb0ef41Sopenharmony_ci  assert.ok(data.startTime >= 0);
181cb0ef41Sopenharmony_ci  assert.ok(data.endTime >= 0);
191cb0ef41Sopenharmony_ci  assert.ok(Array.isArray(data.statistics));
201cb0ef41Sopenharmony_ci  // If the array is not empty, check it
211cb0ef41Sopenharmony_ci  if (data.statistics.length) {
221cb0ef41Sopenharmony_ci    // Just check the first one
231cb0ef41Sopenharmony_ci    const item = data.statistics[0];
241cb0ef41Sopenharmony_ci    assert.ok(typeof item.gcType === 'string');
251cb0ef41Sopenharmony_ci    assert.ok(item.cost >= 0);
261cb0ef41Sopenharmony_ci    assert.ok(typeof item.beforeGC === 'object');
271cb0ef41Sopenharmony_ci    assert.ok(typeof item.afterGC === 'object');
281cb0ef41Sopenharmony_ci    // The content of beforeGC and afterGC is same, so we just check afterGC
291cb0ef41Sopenharmony_ci    assert.ok(typeof item.afterGC.heapStatistics === 'object');
301cb0ef41Sopenharmony_ci    const heapStatisticsKeys = [
311cb0ef41Sopenharmony_ci      'externalMemory',
321cb0ef41Sopenharmony_ci      'heapSizeLimit',
331cb0ef41Sopenharmony_ci      'mallocedMemory',
341cb0ef41Sopenharmony_ci      'peakMallocedMemory',
351cb0ef41Sopenharmony_ci      'totalAvailableSize',
361cb0ef41Sopenharmony_ci      'totalGlobalHandlesSize',
371cb0ef41Sopenharmony_ci      'totalHeapSize',
381cb0ef41Sopenharmony_ci      'totalHeapSizeExecutable',
391cb0ef41Sopenharmony_ci      'totalPhysicalSize',
401cb0ef41Sopenharmony_ci      'usedGlobalHandlesSize',
411cb0ef41Sopenharmony_ci      'usedHeapSize',
421cb0ef41Sopenharmony_ci    ];
431cb0ef41Sopenharmony_ci    heapStatisticsKeys.forEach((key) => {
441cb0ef41Sopenharmony_ci      assert.ok(item.afterGC.heapStatistics[key] >= 0);
451cb0ef41Sopenharmony_ci    });
461cb0ef41Sopenharmony_ci    assert.ok(typeof item.afterGC.heapSpaceStatistics === 'object');
471cb0ef41Sopenharmony_ci    const heapSpaceStatisticsKeys = [
481cb0ef41Sopenharmony_ci      'physicalSpaceSize',
491cb0ef41Sopenharmony_ci      'spaceAvailableSize',
501cb0ef41Sopenharmony_ci      'spaceName',
511cb0ef41Sopenharmony_ci      'spaceSize',
521cb0ef41Sopenharmony_ci      'spaceUsedSize',
531cb0ef41Sopenharmony_ci    ];
541cb0ef41Sopenharmony_ci    heapSpaceStatisticsKeys.forEach((key) => {
551cb0ef41Sopenharmony_ci      const value = item.afterGC.heapSpaceStatistics[0][key];
561cb0ef41Sopenharmony_ci      assert.ok(key === 'spaceName' ? typeof value === 'string' : value >= 0);
571cb0ef41Sopenharmony_ci    });
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciasync function testGCProfiler() {
621cb0ef41Sopenharmony_ci  const data = await collectGCProfile({ duration: 5000 });
631cb0ef41Sopenharmony_ci  checkGCProfile(data);
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_cimodule.exports = {
671cb0ef41Sopenharmony_ci  collectGCProfile,
681cb0ef41Sopenharmony_ci  checkGCProfile,
691cb0ef41Sopenharmony_ci  testGCProfiler,
701cb0ef41Sopenharmony_ci};
71