From d01408f9e08e3e0c5d8d84bf8f9c31d3a27dc56f Mon Sep 17 00:00:00 2001 From: Stefan Blaginov Date: Thu, 1 Feb 2024 14:33:52 +0000 Subject: [PATCH] feat(models): adds a heap size check Signed-off-by: Stefan Blaginov Signed-off-by: Stefan Blaginov --- lib/common/benchmarkModelGenerator.js | 5 +++++ test/common/benchmarkModelGenerator.js | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/common/benchmarkModelGenerator.js b/lib/common/benchmarkModelGenerator.js index 4f4e6c5..0cd95dc 100644 --- a/lib/common/benchmarkModelGenerator.js +++ b/lib/common/benchmarkModelGenerator.js @@ -14,6 +14,7 @@ 'use strict'; +const v8 = require('v8'); const { Blob } = require('buffer'); const DEFAULT_CONCERTO_METAMODEL_VERSION = '1.0.0'; @@ -349,6 +350,10 @@ class BenchmarkModelGenerator { nDeclarations = 1, nProperties = 1, }) { + if (generateUpToSize > v8.getHeapStatistics().total_heap_size) { + throw new Error('The requested model size is exceeding the total_heap_size and cannot be created.'); + } + const model = { $class: `concerto.metamodel@${concertoMetamodelVersion}.Model`, decorators: [], diff --git a/test/common/benchmarkModelGenerator.js b/test/common/benchmarkModelGenerator.js index 333b1ae..8833c1a 100644 --- a/test/common/benchmarkModelGenerator.js +++ b/test/common/benchmarkModelGenerator.js @@ -231,5 +231,18 @@ describe('benchmarkModelGenerator', function () { }); }).should.throw('growBy can be either set to "declarations" or "properties".'); }); + + it('should throw an error if the requested model size exceeds the total_heap_size', function () { + const benchmarkModelGenerator = new BenchmarkModelGenerator(); + benchmarkModelGenerator.should.not.be.null; + + (() => { + benchmarkModelGenerator.generateConcertoModels({ + generateUpToSize: 100000000000, + growBy: 'properties', + nDeclarations: 5, + }); + }).should.throw('The requested model size is exceeding the total_heap_size and cannot be created.'); + }); }); });