Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions generate_test_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

// Create a large test PHP file to test the streaming parser
$testCode = '<?php

namespace TestNamespace;

use Some\Large\Namespace\Class1;
use Another\Large\Namespace\Class2;
use Yet\Another\Large\Namespace\Class3;

/**
* Large test class for testing streaming parser
*
* This class has many methods and properties to test
* the memory efficiency of the streaming parser.
*/
class LargeTestClass
{
private $property1;
private $property2;
private $property3;

';

// Generate many methods
for ($i = 1; $i <= 100; $i++) {
$testCode .= "
/**
* Test method number $i
*
* This method performs various operations and calls
* multiple functions to test the parser capabilities.
*
* @param string \$param1 First parameter
* @param int \$param2 Second parameter
* @return mixed The result of the operation
*/
public function testMethod$i(\$param1, \$param2)
{
// Method body with various function calls
\$result = array();
\$result['data'] = json_encode(\$param1);
\$result['count'] = strlen(\$param2);
\$result['processed'] = trim(strtolower(\$param1));

// Call some functions
\$this->helperFunction();
static::staticFunction();
SomeClass::externalFunction();

// Add some logic
if (\$param2 > 0) {
for (\$j = 0; \$j < \$param2; \$j++) {
\$result['items'][] = 'item_' . \$j;
}
}

return \$result;
}
";
}

$testCode .= '

/**
* Helper function used by test methods
*/
private function helperFunction()
{
return "helper_result";
}

/**
* Static function for testing
*/
public static function staticFunction()
{
return "static_result";
}
}

';

// Add standalone functions
for ($i = 1; $i <= 50; $i++) {
$testCode .= "
/**
* Standalone function number $i
*
* @param mixed \$input Input parameter
* @return mixed Processed result
*/
function standaloneFunction$i(\$input)
{
\$result = process_data(\$input);
\$result = format_output(\$result);
return sanitize_result(\$result);
}

";
}

$testCode .= '
// End of test file
?>';

// Write test file
file_put_contents('/vercel/sandbox/test_large_file.php', $testCode);

echo "Generated large test file: " . strlen($testCode) . " bytes\n";
echo "File saved to: /vercel/sandbox/test_large_file.php\n";
45 changes: 42 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,61 @@ private function handleParseRequest()

// Create parser and process code
try {
$parser = new CodeParser();
// Determine if we should use streaming parser based on file size
$codeSize = strlen($requestData['code']);
$useStreaming = $codeSize > 100 * 1024; // 100KB threshold

if ($useStreaming) {
$parser = new StreamingCodeParser();

// Set up progress tracking for large files
$progressData = [];
$parser->setProgressCallback(function($message, $percentage, $memoryUsage) use (&$progressData) {
$progressData[] = [
'message' => $message,
'percentage' => $percentage,
'memory_mb' => round($memoryUsage / 1024 / 1024, 2),
'timestamp' => microtime(true)
];
});
} else {
$parser = new CodeParser();
}

// Set default empty string for modifiedLines if not provided
$modifiedLines = '';
$modifiedLines = $requestData['modifiedLines'] ?? '';

$startTime = microtime(true);
$result = $parser->parseCode(
$requestData['code'],
$modifiedLines,
$requestData['fileType']
);
$endTime = microtime(true);

// Add performance metrics
$result['performance'] = [
'parsing_time_ms' => round(($endTime - $startTime) * 1000, 2),
'file_size_bytes' => $codeSize,
'parser_used' => $useStreaming ? 'streaming' : 'standard',
'memory_peak_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 2)
];

// Add progress data for streaming parser
if ($useStreaming && !empty($progressData)) {
$result['progress_log'] = $progressData;
}

// Return result
http_response_code(200);
echo json_encode($result);
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
echo json_encode([
'error' => $e->getMessage(),
'error_type' => get_class($e),
'memory_usage_mb' => round(memory_get_usage(true) / 1024 / 1024, 2)
]);
}
}

Expand Down
Loading