From 86b6d97bfd5b30496de033e9e5ddbc565444a147 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Fri, 9 Feb 2024 23:12:53 +0300 Subject: [PATCH] Added MemoryLeakTest --- tests/unit/MemoryLeakTest.php | 117 ++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 tests/unit/MemoryLeakTest.php diff --git a/tests/unit/MemoryLeakTest.php b/tests/unit/MemoryLeakTest.php new file mode 100644 index 0000000..a5fc89b --- /dev/null +++ b/tests/unit/MemoryLeakTest.php @@ -0,0 +1,117 @@ +in(self::DIRECTORIES_TO_LOAD)->name('*.php') as $file) { + /** @psalm-suppress UnresolvableInclude */ + require_once $file->getPathname(); + } + + self::$phpParser = (new ParserFactory())->createForHostVersion(); + } + + private static function cleanUpParser(): void + { + self::$phpParser->parse(''); + } + + public function testTyphoonReflectorClassExistsIsNotLeaking(): void + { + foreach (self::CLASSES as $class) { + $memory = memory_get_usage(); + + TyphoonReflector::build(phpParser: self::$phpParser)->classExists($class); + self::cleanUpParser(); + + self::assertLessThanOrEqual($memory, memory_get_usage()); + } + } + + public function testTyphoonReflectorReflectIsNotLeaking(): void + { + // warm up + TyphoonReflector::build(phpParser: self::$phpParser)->reflectClass(Type\NamedObjectType::class); + self::cleanUpParser(); + + foreach (self::CLASSES as $class) { + $memory = memory_get_usage(); + + TyphoonReflector::build(phpParser: self::$phpParser)->reflectClass($class); + self::cleanUpParser(); + + self::assertLessThanOrEqual($memory, memory_get_usage()); + } + } + + public function testReflectionSessionClassExistsIsNotLeaking(): void + { + $session = TyphoonReflector::build(phpParser: self::$phpParser)->startSession(); + $session->classExists(Type\NamedObjectType::class); + $session->flush(); + self::cleanUpParser(); + + foreach (self::CLASSES as $class) { + $memory = memory_get_usage(); + + $session->classExists($class); + $session->flush(); + self::cleanUpParser(); + + self::assertLessThanOrEqual($memory, memory_get_usage()); + } + } + + public function testReflectionSessionReflectIsNotLeaking(): void + { + $session = TyphoonReflector::build(phpParser: self::$phpParser)->startSession(); + $session->reflectClass(Type\NamedObjectType::class); + $session->flush(); + self::cleanUpParser(); + + foreach (self::CLASSES as $class) { + $memory = memory_get_usage(); + + $session->reflectClass($class); + $session->flush(); + self::cleanUpParser(); + + self::assertLessThanOrEqual($memory, memory_get_usage()); + } + } +}