|
| 1 | +<?php |
| 2 | + |
| 3 | +use Workerman\Worker; |
| 4 | +use Workerman\Protocols\Http\Response; |
| 5 | + |
| 6 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 7 | +require_once __DIR__ . '/db.php'; |
| 8 | + |
| 9 | +// #### http worker #### |
| 10 | +$http_worker = new Worker('http://0.0.0.0:8080'); |
| 11 | +$http_worker->reusePort = true; |
| 12 | + |
| 13 | +// 1 process per CPU core |
| 14 | +$http_worker->count = (int) shell_exec('nproc'); |
| 15 | + |
| 16 | +// benchmark data |
| 17 | +define('JSON_DATA', json_decode(file_get_contents('/data/dataset.json'), true)); |
| 18 | +define('LARGE_JSON', largeJson()); |
| 19 | + |
| 20 | +function largeJson() |
| 21 | +{ |
| 22 | + $data = json_decode(file_get_contents('/data/dataset-large.json'), true); |
| 23 | + foreach ($data as &$item) { |
| 24 | + $item['total'] = $item['price'] * $item['quantity']; |
| 25 | + } |
| 26 | + |
| 27 | + return json_encode(['items' => $data, 'count' => count($data)]); |
| 28 | +} |
| 29 | + |
| 30 | +$http_worker->onWorkerStart = static function () { |
| 31 | + DB::Init(); |
| 32 | +}; |
| 33 | + |
| 34 | +// Data received |
| 35 | +$http_worker->onMessage = static function ($connection, $request) { |
| 36 | + switch ($request->path()) { |
| 37 | + case '/pipeline': |
| 38 | + $connection->headers = ['Content-Type' => 'text/plain']; |
| 39 | + return $connection->send('ok'); |
| 40 | + |
| 41 | + case '/baseline11': |
| 42 | + $sum = array_sum($request->get()); |
| 43 | + if($request->method() === 'POST') { |
| 44 | + $sum += $request->rawBody(); |
| 45 | + } |
| 46 | + |
| 47 | + $connection->headers = ['Content-Type' => 'text/plain']; |
| 48 | + return $connection->send($sum); |
| 49 | + |
| 50 | + case '/json': |
| 51 | + $total = []; |
| 52 | + foreach (JSON_DATA as $item) { |
| 53 | + $item['total'] = $item['price'] * $item['quantity']; |
| 54 | + $total[] = $item; |
| 55 | + } |
| 56 | + |
| 57 | + $connection->headers = ['Content-Type' => 'application/json']; |
| 58 | + return $connection->send(json_encode(['items' => $total, 'count' => count($total)])); |
| 59 | + |
| 60 | + // case '/upload': |
| 61 | + // $connection->headers = ['Content-Type' => 'text/plain']; |
| 62 | + // return $connection->send(strlen($request->rawBody())); |
| 63 | + |
| 64 | + case '/compression': |
| 65 | + if (str_contains($request->header('Accept-Encoding', ''), 'gzip')) { |
| 66 | + $connection->headers = [ |
| 67 | + 'Content-Type' => 'application/json', |
| 68 | + 'Content-Encoding' => 'gzip' |
| 69 | + ]; |
| 70 | + return $connection->send(gzencode(LARGE_JSON, 1)); |
| 71 | + } |
| 72 | + |
| 73 | + $resp = new Response(200, ['Content-Type' => 'application/json'], LARGE_JSON); |
| 74 | + return $connection->send($resp); |
| 75 | + |
| 76 | + case '/db': |
| 77 | + $connection->headers = ['Content-Type' => 'application/json']; |
| 78 | + return $connection->send( |
| 79 | + DB::query( |
| 80 | + $request->get('min', 10), |
| 81 | + $request->get('max', 50) |
| 82 | + ) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + // Serve static files |
| 87 | + // if (str_starts_with($request->path(), '/static/')) { |
| 88 | + // $response = (new Response())->withFile('/data' . $request->path()); |
| 89 | + // return $connection->send($response); |
| 90 | + // } |
| 91 | + |
| 92 | + return $connection->send(new Response( |
| 93 | + 404, |
| 94 | + ['Content-Type' => 'text/plain'], |
| 95 | + '404 Not Found') |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +// Run all workers |
| 100 | +Worker::runAll(); |
0 commit comments