-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperfmon.inc
560 lines (459 loc) · 16.4 KB
/
perfmon.inc
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* @file
* Stand-alone perfmon test system.
*/
// Define the version of this file in case it's used outside of its module.
define('PERFMON_VERSION', '7.x-1.1');
/**
* Public function for running Perfmon tests and returning results.
*
* @param array $testlist
* Array of tests to run, indexed by testname.
* @param bool $log
* Whether to log test processing using perfmon_log.
*
* @return array
* Results from running checklist, indexed by module namespace.
*/
function perfmon_run(array $testlist = NULL, $log = FALSE) {
if (!$testlist) {
$testlist = _perfmon_performance_tests();
}
$results = _perfmon_run($testlist, $log);
return $results;
}
/**
* Private function the review and returns the full results.
*/
function _perfmon_run($testlist, $log = FALSE) {
$results = array();
foreach ($testlist as $test_name => $arguments) {
$test_result = _perfmon_run_test($test_name, $arguments, $log);
if (!empty($test_result)) {
$results[$test_name] = $test_result;
}
}
return $results;
}
/**
* Run a single Security Review check.
*/
function _perfmon_run_test($test_name, $test, $log, $store = FALSE) {
$last_test = array();
if ($store) {
// Get the results of the last check.
$last_test = perfmon_get_last_test($test_name);
}
$test_result = array();
$return = array('result' => NULL);
$function = $test['callback'];
if (function_exists($function)) {
$return = call_user_func($function, $last_test);
}
$test_result = array_merge($test, $return);
$test_result['lastrun'] = REQUEST_TIME;
if ($log && !is_null($return['result'])) {
// Do not log if result is NULL.
$variables = array('!name' => $test_result['title']);
_perfmon_log($test_name, '!name tested', $variables, WATCHDOG_INFO);
}
return $test_result;
}
/**
* Core Perfmon checks.
*/
function _perfmon_performance_tests() {
$tests = array();
$tests['config'] = array(
'bold' => TRUE,
'title' => t('Performance score'),
'callback' => 'perfmon_test_server_configuration',
'description' => t('Drupal installation files and directories (except required) are not writable by the server.'),
);
$tests['cpu'] = array(
'title' => t('CPU (ops per second)'),
'callback' => 'perfmon_test_cpu',
'description' => t('Untrusted users are not allowed to input dangerous HTML tags.'),
);
$tests['files'] = array(
'title' => t('Files operations (ops per second)'),
'callback' => 'perfmon_test_filesop',
'description' => t('Dangerous tags were not found in any submitted content (fields).'),
);
$tests['db_read'] = array(
'title' => t('DB read operations (ops per second)'),
'callback' => 'perfmon_test_db_read',
'description' => t('Error reporting set to log only.'),
);
$tests['db_write'] = array(
'title' => t('DB write operations (ops per second)'),
'callback' => 'perfmon_test_db_write',
'description' => t('Private files directory is outside the web server root.'),
);
$tests['db_update'] = array(
'title' => t('DB update operations (ops per second)'),
'callback' => 'perfmon_test_db_update',
'description' => t('Only safe extensions are allowed for uploaded files and images.'),
);
return $tests;
}
/**
* Test configuration.
*/
function perfmon_test_server_configuration() {
global $_SESSION;
global $base_url;
$domain = $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$request_uri = '/perfmon.test.php';
$url = $base_url . $request_uri;
$execcount = 5;
$exectime = 0;
$count = 10;
$curl_arr = array();
$master = curl_multi_init();
$cookiestr = "";
foreach ($_COOKIE as $key => $value) {
$cookiestr = $cookiestr . $key . "=" . $value . "; ";
}
for ($k = 0; $k < $execcount; $k++) {
for ($i = 0; $i < $count; $i++) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_COOKIE, $cookiestr);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_arr[$i] = $ch;
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master, $running);
} while ($running > 0);
for ($i = 0; $i < $count; $i++) {
$info = curl_getinfo($curl_arr[$i]);
$exectime += $info['starttransfer_time'] - $info['pretransfer_time'];
curl_multi_remove_handle($master, $curl_arr[$i]);
curl_close($curl_arr[$i]);
}
sleep(2);
}
return array(
'result' => round(1 / ($exectime / $execcount / $count), 0),
'value' => '0',
);
}
/**
* Test CPU.
*/
function perfmon_test_cpu() {
$exectime = 0;
$count = 100000;
for ($i = 0; $i < $count; $i++) {
$starttime = microtime(TRUE);
$test = 0;
$test++;
$test--;
$test = 2 / 5;
$test = 2 * 5;
$endtime = microtime(TRUE);
$exectime += $endtime - $starttime;
}
return array('result' => round(1 / ($exectime / $count), 0), 'value' => '0');
}
/**
* Test file operations.
*/
function perfmon_test_filesop() {
$tempdir = file_directory_temp();
$count = 1000;
$exectime = microtime(TRUE);
for ($i = 0; $i < $count; $i++) {
$filecont = "<?php phpinfo(); print(''" . $i . "'')";
$filename = $tempdir . "/phptest" . $i . ".php";
$file = fopen($filename, "w");
fwrite($file, $filecont);
unlink($filename);
}
$exectime = microtime(TRUE) - $exectime;
return array('result' => round(1 / ($exectime / $count), 0), 'value' => '0');
}
/**
* Prepare to test mysql.
*/
function perfmon_test_db_prepare($testname, $count) {
for ($i = 0; $i < $count; $i++) {
db_insert('perfmon_test')
->fields(array(
'id' => $i,
'testname' => 'dbtest',
'data' => 'aaaaaaaaaaaaaaaaaaaaa',
))
->execute();
}
}
/**
* Test db read operations.
*/
function perfmon_test_db_read() {
$exectime = 0;
$count = 1000;
db_truncate('perfmon_test')->execute();
perfmon_test_db_prepare('test_db_read', $count);
$exectime = microtime(TRUE);
for ($i = 0; $i < $count; $i++) {
$query = db_select('perfmon_test', 'pt');
$query->fields('pt', array('id', 'testname', 'data'));
$query->condition('pt.id', $i, '=');
$query->execute();
}
$exectime = microtime(TRUE) - $exectime;
return array('result' => round(1 / ($exectime / $count), 0), 'value' => '0');
}
/**
* Test db write operations.
*/
function perfmon_test_db_write() {
$exectime = 0;
$count = 1000;
db_truncate('perfmon_test')->execute();
$exectime = microtime(TRUE);
perfmon_test_db_prepare('test_db_write', $count);
$exectime = microtime(TRUE) - $exectime;
return array('result' => round(1 / ($exectime / $count), 0), 'value' => '0');
}
/**
* Test db update operations.
*/
function perfmon_test_db_update() {
$exectime = 0;
$count = 1000;
db_truncate('perfmon_test')->execute();
perfmon_test_db_prepare('test_db_update', $count);
$exectime = microtime(TRUE);
for ($i = 0; $i < $count; $i++) {
$query = db_update('perfmon_test');
$query->fields(array(
'data' => 'bbbbbbbbbbbbbbbbbbbbb',
));
$query->condition('id', $i);
$query->execute();
}
$exectime = microtime(TRUE) - $exectime;
return array('result' => round(1 / ($exectime / $count), 0), 'value' => '0');
}
/**
* Get core performance tests.
*
* @return array
* Array of checks indexed by test name. e.g.:
* 'perfmon' => array(
* 'test_name' => array(...)
* )
*/
function perfmon_get_testlist() {
$tests = _perfmon_performance_tests();
return $tests;
}
/**
* Mysql variables.
*/
function perfmon_get_mysqlvariables() {
$mysqlvariables = db_query("SHOW GLOBAL VARIABLES")->fetchAll();
$result = perfmon_convert2array($mysqlvariables);
return $result;
}
/**
* Mysql status.
*/
function perfmon_get_mysqlstatus() {
$mysqlstatus = db_query("SHOW GLOBAL STATUS")->fetchAll();
$result = perfmon_convert2array($mysqlstatus);
return $result;
}
/**
* Convert 2 array.
*/
function perfmon_convert2array($variables) {
$result = array();
foreach ($variables as $parameter) {
$result[$parameter->Variable_name] = $parameter->Value;
}
return $result;
}
/**
* Convert seconds to time.
*/
function _perfmon_seconds_to_time($seconds) {
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
return $dtF->diff($dtT)->format(t('%a days, %h hours, %i minutes and %s seconds'));
}
/**
* Format bytes.
*/
function _perfmon_format_bytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
/**
* Describes mysql performance variables.
*/
function perfmon_get_mysql_performance_variables() {
$variables = perfmon_get_mysqlvariables();
$status = perfmon_get_mysqlstatus();
$performance_variables = array();
$uptime = $status['Uptime'];
$performance_variables['uptime'] = array(
'value' => $uptime,
'title' => t('MySQL Uptime'),
'display_value' => _perfmon_seconds_to_time($uptime),
'message' => t('MySQL Server Uptime'),
'class' => 'ok',
);
if ($uptime < 86400) {
$performance_variables['uptime']['message'] = t('MySQL started within last 24 hours - recommendations may be inaccurate');
$performance_variables['uptime']['class'] = 'error';
}
$memory_global = $variables['key_buffer_size'] + $variables['tmp_table_size'] + $variables['innodb_buffer_pool_size'] +
$variables['query_cache_size'] + $variables['innodb_additional_mem_pool_size'] + $variables['innodb_log_buffer_size'];
$performance_variables['memory_global'] = array(
'value' => $memory_global,
'title' => t('Global Memory'),
'display_value' => _perfmon_format_bytes($memory_global),
'message' => t('Global memory size (key_buffer_size + tmp_table_size + innodb_buffer_pool_size + innodb_additional_mem_pool_size + innodb_log_buffer_size + query_cache_size)'),
'class' => 'ok',
);
$memory_per_connection = $variables['read_buffer_size'] + $variables['read_rnd_buffer_size'] +
$variables['sort_buffer_size'] + $variables['join_buffer_size'];
$performance_variables['memory_per_connection'] = array(
'value' => $memory_per_connection,
'title' => t('Memory per connection'),
'display_value' => _perfmon_format_bytes($memory_per_connection),
'message' => t('Memory per connection (read_buffer_size + read_rnd_buffer_size + sort_buffer_size + thread_stack + join_buffer_size)'),
'class' => 'ok',
);
$performance_variables['max_connections'] = array(
'value' => $variables['max_connections'],
'title' => 'Max connections',
'display_value' => $variables['max_connections'],
'message' => t('Max user connections ( max_connections )'),
'class' => 'ok',
);
$max_memory = $performance_variables['memory_global']['value'] +
$performance_variables['max_connections']['value'] * $performance_variables['memory_per_connection']['value'];
$performance_variables['max_memory'] = array(
'value' => $max_memory,
'title' => 'Max. Memory',
'display_value' => _perfmon_format_bytes($max_memory),
'message' => t('Max Mysql memory (Global memory + Memory per connection * Max connections). Check that this variable lower than 85% of server RAM.'),
'class' => 'ok',
);
$performance_variables['query_cache_size'] = array(
'value' => $variables['query_cache_size'],
'title' => t('Query cache size'),
'display_value' => _perfmon_format_bytes($variables['query_cache_size']),
'message' => t('Query cache size ( query_cache_size ). Recommended 128M'),
'class' => 'ok',
);
if ($variables['query_cache_size'] != (1024 * 1024 * 128)) {
$performance_variables['query_cache_size']['message'] = t('Query cache size query_cache_size. Recommended 128M. Increasing the query_cache size over 128M may reduce performance.');
$performance_variables['query_cache_size']['class'] = 'error';
}
$query_cache_efficiency = $status['Qcache_hits'] / ($status['Com_select'] + $status['Qcache_hits']);
$performance_variables['query_cache_efficiency'] = array(
'value' => $query_cache_efficiency,
'title' => t('Query cache efficiency'),
'display_value' => round($query_cache_efficiency * 100, 2) . '%',
'message' => t('Query cache efficiency'),
'class' => 'ok',
);
if ($query_cache_efficiency < 0.2) {
$performance_variables['query_cache_efficiency']['message'] = t('Query cache efficiency. This parameter is low try to increase query_cache_limit.');
$performance_variables['query_cache_efficiency']['class'] = 'error';
}
$query_cache_prunes_per_day = $status['Qcache_lowmem_prunes'] / ($status['Uptime'] / 86400);
$performance_variables['query_cache_prunes_per_day'] = array(
'value' => $query_cache_prunes_per_day,
'title' => t('Query cache prunes per day'),
'display_value' => round($query_cache_prunes_per_day, 2),
'message' => t('Query cache prunes per day. If value grows rapidly you need to increase query_cache_size.'),
'class' => 'ok',
);
$total_sorts = $status['Sort_scan'] + $status['Sort_range'];
$performance_variables['total_sorts'] = array(
'value' => $total_sorts,
'title' => t('Total sorts'),
'display_value' => $total_sorts,
'message' => t('Total sorts count.'),
'class' => 'ok',
);
$total_sorts_temp = $status['Sort_merge_passes'] / $performance_variables['total_sorts']['value'];
$performance_variables['total_sorts_temp'] = array(
'value' => $total_sorts_temp,
'title' => t('Total sorts using disk'),
'display_value' => round($total_sorts_temp * 100, 2) . '%',
'message' => t('Sorts requiring temporary tables. If value grater than 10% then you need to increase sort_buffer_size and read_rnd_buffer_size.'),
'class' => 'ok',
);
if ($total_sorts_temp > 0.1) {
$performance_variables['total_sorts_temp']['class'] = 'error';
}
$created_tmp_tables_dsk = $status['Created_tmp_disk_tables'] / $status['Created_tmp_tables'];
$performance_variables['created_tmp_tables_dsk'] = array(
'value' => $created_tmp_tables_dsk,
'title' => t('Temporary tables created on disk'),
'display_value' => round($created_tmp_tables_dsk * 100, 2) . '%',
'message' => t('Temporary tables created on disk. If value grater than 30% then you need to increase tmp_table_size and tmp_table_size. When making adjustments, make tmp_table_size/max_heap_table_size equal.'),
'class' => 'ok',
);
if ($created_tmp_tables_dsk > 0.3) {
$performance_variables['created_tmp_tables_dsk']['class'] = 'error';
}
$open_files = $status['Open_files'] / $variables['open_files_limit'];
$performance_variables['open_files'] = array(
'value' => $open_files,
'title' => t('Open files'),
'display_value' => round($open_files * 100, 2) . '%',
'message' => t('Open file limit used. If value grater than 85% then you need to increase open_files_limit.'),
'class' => 'ok',
);
if ($open_files > 0.85) {
$performance_variables['open_files']['class'] = 'error';
}
$innodb_buffer_efficiency = 1 - $status['Innodb_buffer_pool_reads'] / $status['Innodb_buffer_pool_read_requests'];
$performance_variables['innodb_buffer_efficiency'] = array(
'value' => $innodb_buffer_efficiency,
'title' => t('Innodb buffer pool efficiency'),
'display_value' => round($innodb_buffer_efficiency * 100, 2) . '%',
'message' => t('InnoDB Buffer pool read cache effiency. If value lower than 95% then you need to increase innodb_buffer_pool_size.'),
'class' => 'ok',
);
if ($innodb_buffer_efficiency < 0.95) {
$performance_variables['innodb_buffer_efficiency']['class'] = 'error';
}
$performance_variables['innodb_flush_log_at_trx_commit'] = array(
'value' => $variables['innodb_flush_log_at_trx_commit'],
'title' => t('innodb_flush_log_at_trx_commit'),
'display_value' => $variables['innodb_flush_log_at_trx_commit'],
'message' => t('innodb_flush_log_at_trx_commit. Recommended value is 2.'),
'class' => 'ok',
);
if ($variables['innodb_flush_log_at_trx_commit'] != 2) {
$performance_variables['innodb_flush_log_at_trx_commit']['class'] = 'error';
}
return $performance_variables;
}
/**
* Log.
*/
function _perfmon_log($module, $check_name, $message, $variables, $type) {
module_invoke_all('perfmon_log', $module, $check_name, $message, $variables, $type);
}