Conditions | 7 |
Paths | 48 |
Total Lines | 63 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
9 | public function collect(array $params = []) |
||
10 | { |
||
11 | /**@var \Swoole\Http\Server $swoole */ |
||
12 | $swoole = app('swoole'); |
||
13 | $stats = $swoole->stats(); |
||
14 | // Get worker_num/task_worker_num from setting for the old Swoole. |
||
15 | $setting = $swoole->setting; |
||
16 | if (!isset($stats['worker_num'])) { |
||
17 | $stats['worker_num'] = $setting['worker_num']; |
||
18 | } |
||
19 | if (!isset($stats['task_worker_num'])) { |
||
20 | $stats['task_worker_num'] = isset($setting['task_worker_num']) ? $setting['task_worker_num'] : 0; |
||
21 | } |
||
22 | $metrics = [ |
||
23 | [ |
||
24 | 'name' => 'swoole_cpu_num', |
||
25 | 'type' => 'gauge', |
||
26 | 'value' => swoole_cpu_num(), |
||
27 | ], |
||
28 | [ |
||
29 | 'name' => 'swoole_start_time', |
||
30 | 'type' => 'gauge', |
||
31 | 'value' => $stats['start_time'], |
||
32 | ], |
||
33 | [ |
||
34 | 'name' => 'swoole_connection_num', |
||
35 | 'type' => 'gauge', |
||
36 | 'value' => $stats['connection_num'], |
||
37 | ], |
||
38 | [ |
||
39 | 'name' => 'swoole_request_count', |
||
40 | 'type' => 'gauge', |
||
41 | 'value' => $stats['request_count'], |
||
42 | ], |
||
43 | [ |
||
44 | 'name' => 'swoole_worker_num', |
||
45 | 'type' => 'gauge', |
||
46 | 'value' => $stats['worker_num'], |
||
47 | ], |
||
48 | [ |
||
49 | 'name' => 'swoole_idle_worker_num', |
||
50 | 'type' => 'gauge', |
||
51 | 'value' => isset($stats['idle_worker_num']) ? $stats['idle_worker_num'] : 0, |
||
52 | ], |
||
53 | [ |
||
54 | 'name' => 'swoole_task_worker_num', |
||
55 | 'type' => 'gauge', |
||
56 | 'value' => $stats['task_worker_num'], |
||
57 | ], |
||
58 | [ |
||
59 | 'name' => 'swoole_task_idle_worker_num', |
||
60 | 'type' => 'gauge', |
||
61 | 'value' => isset($stats['task_idle_worker_num']) ? $stats['task_idle_worker_num'] : 0, |
||
62 | ], |
||
63 | [ |
||
64 | 'name' => 'swoole_tasking_num', |
||
65 | 'type' => 'gauge', |
||
66 | 'value' => $stats['tasking_num'], |
||
67 | ], |
||
68 | ]; |
||
69 | foreach ($metrics as $metric) { |
||
70 | $key = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], $metric['name'], $metric['type'], '']); |
||
71 | apcu_store($key, $metric['value'], $this->config['apcu_key_max_age']); |
||
72 | } |
||
74 | } |