function bootstrapParser() { global $argv; // Not using autoloader. Not needed for such a small piece of code. // why require_once is here and not on top of the file? because I would only need it here. Lets keep the memory // footprint small and include only the bare minimum code we have to. require_once 'LogParser.php'; $parser = new LogParser(__DIR__ . '/../data/sample.log'); $parser->parse($argv[1], $argv[2]); return true; }
public function testParseWithValidFormatAndData() { $tmpHandle = tmpfile(); $fileContent = 'at=info method=GET path=/api/users/0 dyno=web.13 connect=10ms service=20ms '; // even if I have lines with invalid format as long as these don't match the requestUri and method // I wouldn't need to worry $fileContent .= PHP_EOL . 'at=info method=GET path=/api/users/1/get_friends_score dyno=web.1'; for ($i = 1; $i <= 20; $i++) { $fileContent .= PHP_EOL . 'at=info method=GET path=/api/users/1 dyno=web.' . $i % 3; $fileContent .= ' connect=' . rand(1, 9) * $i . 'ms service=' . rand(20, 50) * $i . 'ms '; } fwrite($tmpHandle, $fileContent); $metaData = stream_get_meta_data($tmpHandle); $tmpFilename = $metaData['uri']; $parser = new LogParser($tmpFilename); ob_start(); $parser->parse('GET', '/api/users/{user_id}'); fclose($tmpHandle); $contents = ob_get_clean(); $this->assertContains('Number of times request was made: 21', $contents); $this->assertContains('Most active Dyno(s): web.1, web.2', $contents); $this->assertContains('Least active Dyno(s): web.13', $contents); $this->assertContains('Min. Response Time: ', $contents); $this->assertContains('Max. Response Time: ', $contents); $this->assertContains('Mean Response Time: ', $contents); $this->assertContains('Median Response Time: ', $contents); $this->assertContains('Mode Response Time(s): ', $contents); }
<?php /** * Cкрипт парсинга файлов логов */ header('Content-Type: text/html; charset=UTF-8'); require_once 'class/LogParser.class.php'; try { $lpObj = new LogParser(); $lpObj->parse(); echo 'Логи успешно сохранены в базу данных'; } catch (Exception $e) { echo $e->getMessage(); } unset($lpObj);