-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
app.php
145 lines (123 loc) · 3.46 KB
/
app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
use Workerman\Protocols\Http\Response;
use Workerman\Protocols\Http\Request;
function init()
{
global $world, $fortune, $update;
$pdo = new PDO(
'mysql:host=tfb-database;dbname=hello_world',
'benchmarkdbuser',
'benchmarkdbpass',
[
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]
);
$world = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$fortune = $pdo->prepare('SELECT id,message FROM Fortune');
$update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
}
function router(Request $request)
{
return match($request->path()) {
'/plaintext' => text(),
'/json' => json(),
'/db' => db(),
'/fortunes' => fortune(),
'/query' => query($request),
'/update' => updateraw($request),
// '/info' => info(),
default => new Response(404, [], 'Error 404'),
};
}
function text()
{
return new Response(200, [
'Content-Type' => 'text/plain',
'Date' => Header::$date
], 'Hello, World!');
}
function json()
{
return new Response(200, [
'Content-Type' => 'application/json',
'Date' => Header::$date
], json_encode(['message' => 'Hello, World!']));
}
function db()
{
global $world;
$world->execute([mt_rand(1, 10000)]);
return new Response(200, [
'Content-Type' => 'application/json',
'Date' => Header::$date
], json_encode($world->fetch()));
}
function query($request)
{
global $world;
$query_count = 1;
$q = (int) $request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
while ($query_count--) {
$world->execute([mt_rand(1, 10000)]);
$arr[] = $world->fetch();
}
return new Response(200, [
'Content-Type' => 'application/json',
'Date' => Header::$date
], json_encode($arr));
}
function updateraw($request)
{
global $world, $update;
$query_count = 1;
$q = (int) $request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
while ($query_count--) {
$id = mt_rand(1, 10000);
$world->execute([$id]);
$item = $world->fetch();
$update->execute(
[$item['randomNumber'] = mt_rand(1, 10000), $id]
);
$arr[] = $item;
}
// $pdo->beginTransaction();
// foreach($arr as $world) {
// $update->execute([$world['randomNumber'], $world['id']]);
// }
// $pdo->commit();
return new Response(200, [
'Content-Type' => 'application/json',
'Date' => Header::$date
], json_encode($arr));
}
function fortune()
{
global $fortune;
$fortune->execute();
$arr = $fortune->fetchAll(PDO::FETCH_KEY_PAIR);
$arr[0] = 'Additional fortune added at request time.';
asort($arr);
$html = '';
foreach ($arr as $id => $message) {
$message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
$html .= "<tr><td>$id</td><td>$message</td></tr>";
}
return new Response(200, [
'Date' => Header::$date
], "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>"
);
}
/* function info()
{
ob_start();
phpinfo();
return new Response(200, ['Content-Type' => 'text/plain'], ob_get_clean());
}
*/