|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +include __DIR__ . '/vendor/autoload.php'; |
| 4 | + |
| 5 | +use Ripple\Http\Server; |
| 6 | +use Ripple\Worker\Manager; |
| 7 | + |
| 8 | +use function Co\repeat; |
| 9 | +use function Co\wait; |
| 10 | + |
| 11 | +class Setup |
| 12 | +{ |
| 13 | + public static PDO $pdo; |
| 14 | + public static string $dateFormatted; |
| 15 | + |
| 16 | + public static PDOStatement $queryWorldWhereID; |
| 17 | + public static PDOStatement $updateWorldRandomNumber; |
| 18 | + public static PDOStatement $queryFortune; |
| 19 | + |
| 20 | + public static function dateRefresh(): void |
| 21 | + { |
| 22 | + try { |
| 23 | + $date = new DateTime('now', new DateTimeZone('GMT')); |
| 24 | + } catch (Throwable $e) { |
| 25 | + return; |
| 26 | + } |
| 27 | + Setup::$dateFormatted = $date->format('D, d M Y H:i:s T'); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * @return int |
| 33 | + */ |
| 34 | + public static function randomInt(): int |
| 35 | + { |
| 36 | + try { |
| 37 | + return \random_int(1, 10000); |
| 38 | + } catch (Throwable $e) { |
| 39 | + return mt_rand(1, 10000); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param mixed $value |
| 45 | + * |
| 46 | + * @return int |
| 47 | + */ |
| 48 | + public static function clamp(mixed $value): int |
| 49 | + { |
| 50 | + if (!\is_numeric($value) || $value < 1) { |
| 51 | + return 1; |
| 52 | + } |
| 53 | + if ($value > 500) { |
| 54 | + return 500; |
| 55 | + } |
| 56 | + return \intval($value); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param string $template |
| 61 | + * @param array $data |
| 62 | + * |
| 63 | + * @return string |
| 64 | + */ |
| 65 | + public static function render(string $template, array $data = []): string |
| 66 | + { |
| 67 | + foreach ($data as $key => $value) { |
| 68 | + $$key = $value; |
| 69 | + } |
| 70 | + |
| 71 | + \ob_start(); |
| 72 | + include $template; |
| 73 | + return \ob_get_clean(); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +$manager = new Manager(); |
| 78 | +$worker = new class() extends \Ripple\Worker { |
| 79 | + /*** @var \Ripple\Http\Server */ |
| 80 | + public Server $server; |
| 81 | + |
| 82 | + /** |
| 83 | + * @param \Ripple\Worker\Manager $manager |
| 84 | + * |
| 85 | + * @return void |
| 86 | + */ |
| 87 | + public function register(Manager $manager): void |
| 88 | + { |
| 89 | + $this->count = 64; |
| 90 | + $this->server = new Server('http://0.0.0.0:8080'); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function boot(): void |
| 97 | + { |
| 98 | + Setup::dateRefresh(); |
| 99 | + repeat(static fn () => Setup::dateRefresh(), 1); |
| 100 | + |
| 101 | + Setup::$pdo = new \PDO( |
| 102 | + 'mysql:host=tfb-database;port=3306;dbname=hello_world', |
| 103 | + 'benchmarkdbuser', |
| 104 | + 'benchmarkdbpass', |
| 105 | + [ |
| 106 | + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, |
| 107 | + \PDO::ATTR_EMULATE_PREPARES => false, |
| 108 | + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, |
| 109 | + ] |
| 110 | + ); |
| 111 | + |
| 112 | + Setup::$queryWorldWhereID = Setup::$pdo->prepare('SELECT id, randomNumber FROM World WHERE id = ?'); |
| 113 | + Setup::$updateWorldRandomNumber = Setup::$pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?'); |
| 114 | + Setup::$queryFortune = Setup::$pdo->prepare('SELECT * FROM `Fortune`'); |
| 115 | + $this->server->onRequest(fn (Server\Request $request) => $this->onRequest($request)); |
| 116 | + $this->server->listen(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param \Ripple\Http\Server\Request $request |
| 121 | + * |
| 122 | + * @return void |
| 123 | + */ |
| 124 | + public function onRequest(Server\Request $request): void |
| 125 | + { |
| 126 | + switch ($request->SERVER['REQUEST_URI']) { |
| 127 | + case '/json': |
| 128 | + $request->respondJson( |
| 129 | + ['message' => 'Hello, World!'], |
| 130 | + ['Date' => Setup::$dateFormatted] |
| 131 | + ); |
| 132 | + break; |
| 133 | + |
| 134 | + case '/db': |
| 135 | + $statement = Setup::$queryWorldWhereID; |
| 136 | + $statement->execute([Setup::randomInt()]); |
| 137 | + $request->respondJson($statement->fetch(), ['Date' => Setup::$dateFormatted]); |
| 138 | + break; |
| 139 | + |
| 140 | + case '/queries': |
| 141 | + $queries = Setup::clamp($request->GET['queries'] ?? 1); |
| 142 | + $results = []; |
| 143 | + $statement = Setup::$queryWorldWhereID; |
| 144 | + while ($queries--) { |
| 145 | + $statement->execute([Setup::randomInt()]); |
| 146 | + $results[] = $statement->fetch(); |
| 147 | + } |
| 148 | + $request->respondJson($results, ['Date' => Setup::$dateFormatted]); |
| 149 | + |
| 150 | + break; |
| 151 | + case '/fortunes': |
| 152 | + $rows = Setup::$pdo->query('SELECT * FROM `Fortune`')?->fetchAll(); |
| 153 | + $rows[] = ['id' => 0, 'message' => 'Additional fortune added at request time.']; |
| 154 | + \usort($rows, function ($a, $b) { |
| 155 | + return $a['message'] <=> $b['message']; |
| 156 | + }); |
| 157 | + |
| 158 | + $request->respondHtml( |
| 159 | + Setup::render('fortunes.php', ['rows' => $rows]), |
| 160 | + [ |
| 161 | + 'Date' => Setup::$dateFormatted, |
| 162 | + 'Content-Type' => 'text/html; charset=UTF-8' |
| 163 | + ] |
| 164 | + ); |
| 165 | + break; |
| 166 | + |
| 167 | + case '/updates': |
| 168 | + $queries = Setup::clamp($request->GET['queries'] ?? 1); |
| 169 | + $results = []; |
| 170 | + $statement = Setup::$queryWorldWhereID; |
| 171 | + $update = Setup::$updateWorldRandomNumber; |
| 172 | + while ($queries--) { |
| 173 | + $statement->execute([Setup::randomInt()]); |
| 174 | + $row = $statement->fetch(); |
| 175 | + $row['randomNumber'] = Setup::randomInt(); |
| 176 | + $results[] = $row; |
| 177 | + $update->execute([$row['randomNumber'], $row['id']]); |
| 178 | + } |
| 179 | + $request->respondJson($results, ['Date' => Setup::$dateFormatted]); |
| 180 | + break; |
| 181 | + |
| 182 | + case '/plaintext': |
| 183 | + $request->respond( |
| 184 | + 'Hello, World!', |
| 185 | + [ |
| 186 | + 'Content-Type' => 'text/plain; charset=utf-8', |
| 187 | + 'Date' => Setup::$dateFormatted |
| 188 | + ] |
| 189 | + ); |
| 190 | + break; |
| 191 | + |
| 192 | + default: |
| 193 | + $request->respond('Not Found', [], 404); |
| 194 | + } |
| 195 | + } |
| 196 | +}; |
| 197 | + |
| 198 | +$manager->addWorker($worker); |
| 199 | +$manager->run(); |
| 200 | +wait(); |
0 commit comments