Skip to content

Commit da1f15e

Browse files
committed
Speed up running the framework validation test suite
1. We're parsing thousands of files and there are many tokens per file. The overhead of all the assert statements is noticeable. Optimizing out the check speeds this up 3x from 90 seconds to 30 seconds. 2. Don't write files only to unlink them later. Instead, save the failed file only on test suite failure
1 parent ecc2be1 commit da1f15e

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

tests/ParserFrameworkValidationTests.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,33 @@ public function testFrameworkErrors($testCaseFile, $frameworkName) {
4040
$parser = new \Microsoft\PhpParser\Parser();
4141
$sourceFile = $parser->parseSourceFile($fileContents);
4242

43-
$directory = __DIR__ . "/output/$frameworkName/";
44-
if (!file_exists($dir = __DIR__ . "/output")) {
45-
mkdir($dir);
46-
}
47-
if (!file_exists($directory)) {
48-
mkdir($directory);
49-
}
50-
$outFile = $directory . basename($testCaseFile);
51-
file_put_contents($outFile, $fileContents);
52-
53-
foreach ($sourceFile->getDescendantNodesAndTokens() as $child) {
54-
if ($child instanceof Token) {
55-
$this->assertNotEquals(\Microsoft\PhpParser\TokenKind::Unknown, $child->kind, "input: $testCaseFile\r\nexpected: ");
56-
$this->assertNotInstanceOf(\Microsoft\PhpParser\SkippedToken::class, $child, "input: $testCaseFile\r\nexpected: ");
57-
$this->assertNotInstanceOf(\Microsoft\PhpParser\MissingToken::class, $child, "input: $testCaseFile\r\nexpected: ");
43+
try {
44+
foreach ($sourceFile->getDescendantNodesAndTokens() as $child) {
45+
if ($child instanceof Token) {
46+
if (get_class($child) === Token::class && $child->kind !== \Microsoft\PhpParser\TokenKind::Unknown) {
47+
// NOTE: This parsing many tokens each from 10000+ files - the PHPUnit assert method calls are slow and
48+
// Without this optimization, the test suite takes 93 seconds.
49+
// With this optimization, the method takes 30 seconds.
50+
continue;
51+
}
52+
$this->assertNotEquals(\Microsoft\PhpParser\TokenKind::Unknown, $child->kind, "input: $testCaseFile\r\nexpected: ");
53+
$this->assertNotInstanceOf(\Microsoft\PhpParser\SkippedToken::class, $child, "input: $testCaseFile\r\nexpected: ");
54+
$this->assertNotInstanceOf(\Microsoft\PhpParser\MissingToken::class, $child, "input: $testCaseFile\r\nexpected: ");
55+
}
56+
}
57+
} catch (Throwable $e) {
58+
$directory = __DIR__ . "/output/$frameworkName/";
59+
if (!file_exists($dir = __DIR__ . "/output")) {
60+
mkdir($dir);
61+
}
62+
if (!file_exists($directory)) {
63+
mkdir($directory);
5864
}
65+
$outFile = $directory . basename($testCaseFile);
66+
file_put_contents($outFile, $fileContents);
67+
throw $e;
5968
}
6069

61-
unlink($outFile);
6270
// echo json_encode($parser->getErrors($sourceFile));
6371
}
6472
}

0 commit comments

Comments
 (0)