-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfmon.module
230 lines (211 loc) · 6.89 KB
/
perfmon.module
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
<?php
/**
* @file
* TODO: Enter file description here.
*/
/**
* Implements hook_help().
*/
function perfmon_help($path, $arg) {
switch ($path) {
// Main module help for the block module.
case 'admin/help#block':
return '<p>' . t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Bartik, for example, implements the regions "Sidebar first", "Sidebar second", "Featured", "Content", "Header", "Footer", etc., and a block may appear in any one of these areas. The <a href="@blocks">blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('@blocks' => url('admin/structure/block'))) . '</p>';
// Help for another path in the block module.
case 'admin/structure/block':
return '<p>' . t('This page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the <em>Save blocks</em> button at the bottom of the page.') . '</p>';
}
}
/**
* Implements hook_menu().
*/
function perfmon_menu() {
$items = array();
$items['admin/reports/perfmon'] = array(
'title' => 'Performance Monitor',
'description' => 'Check performance of your site.',
'page callback' => 'perfmon_page',
'access arguments' => array('access performance monitor report'),
'file' => 'perfmon.pages.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/reports/perfmon/run'] = array(
'title' => 'Performance',
'access arguments' => array('access performance monitor report'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/reports/perfmon/run/%'] = array(
'title' => 'Perfmon run test',
'page callback' => 'perfmon_run_test',
'page arguments' => array(4),
'access arguments' => array('access performance monitor report'),
'file' => 'perfmon.pages.inc',
'type' => MENU_CALLBACK,
);
$items['admin/reports/perfmon/mysql'] = array(
'title' => 'MySQL',
'type' => MENU_LOCAL_TASK,
'page callback' => 'perfmon_mysql_page',
'file' => 'perfmon.pages.inc',
'access arguments' => array('access performance monitor report'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function perfmon_permission() {
return array(
'access performance monitor report' => array(
'title' => t('Access performance monitor report'),
'description' => t('View performance monitor report. Give only to trusted users.'),
),
'run permormance monitor checks' => array(
'title' => t('Run permormance monitor checks'),
'description' => t('Run permormance monitor checks'),
),
);
}
/**
* Implements hook_theme().
*/
function perfmon_theme($existing, $type, $theme, $path) {
return array(
'perfmon_reviewed' => array(
'variables' => array(
'items' => array(),
'header' => '',
'description' => '',
),
'file' => 'perfmon.pages.inc',
),
'perfmon_mysql_reviewed' => array(
'variables' => array(
'items' => array(),
'header' => '',
'description' => '',
),
'file' => 'perfmon.pages.inc',
),
);
}
/**
* Retrieve stored tests and results.
*
* @return array
* Array of checks with keys:
* testname - string test name
* result - int result of test
* lastrun - UNIX timestamp of last time check ran
*/
function perfmon_get_stored_results() {
$tests = array();
// Retrieve results from last run of the checklist.
$result = db_query("SELECT testname, result, lastrun FROM {perfmon}");
foreach ($result as $record) {
$tests[] = array(
'testname' => $record->testname,
'result' => $record->result,
'lastrun' => $record->lastrun,
);
}
return $tests;
}
/**
* Retrieve the result from the last run of test.
*
* @return array
* Return last results
*
* @see perfmon_get_stored_results()
*/
function perfmon_get_last_test($testname) {
$fields = array('testname', 'result', 'lastrun');
$result = db_select('perfmon', 'sr')->fields('sr', $fields)
->condition('testname', $testname)
->execute()->fetchAssoc();
if (!empty($result)) {
return $result;
}
return FALSE;
}
/**
* Run the perfmon test and store the results.
*/
function perfmon_run_store($tests) {
// Call private function to perform the actual test.
$results = _perfmon_run($tests);
variable_set('perfmon_last_run', time());
// Store results and return.
return perfmon_store_results($results);
}
/**
* Store test results.
*/
function perfmon_store_results($results) {
$saved = $to_save = 0;
foreach ($results as $testname => $test) {
db_delete('perfmon')
->condition('testname', $testname)
->execute();
$to_save++;
$record = array(
'testname' => $testname,
'result' => $test['result'],
'lastrun' => $test['lastrun'] ? $test['lastrun'] : REQUEST_TIME,
);
if (drupal_write_record('perfmon', $record) == SAVED_NEW) {
$saved++;
}
else {
_perfmon_log($testname, 'Unable to store test !testname', array('!testname' => $testname), WATCHDOG_ERROR);
}
}
return ($to_save == $saved) ? TRUE : FALSE;
}
/**
* Run the perfmon tests and return the full results.
*/
function perfmon_run_tests($testlist, $log = NULL) {
module_load_include('inc', 'perfmon');
// Allow callers, like our drush command, to decide not to log.
if (is_null($log)) {
$log = variable_get('perfmon_log', TRUE);
}
// Call our private function to perform the actual run.
$results = _perfmon_run_tests($testlist, $log);
return $results;
}
/**
* Operation function called by Batch.
*/
function _perfmon_batch_op($testname, $test, $log, &$context) {
module_load_include('inc', 'perfmon');
$context['message'] = $test['title'];
// Run the check.
$test_result = _perfmon_run_test($testname, $test, $log);
if (!empty($test_result)) {
$context['results'][$testname] = $test_result;
}
}
/**
* Finished callback for Batch processing the checklist.
*/
function _perfmon_batch_finished($success, $results, $operations) {
variable_set('perfmon_last_run', time());
module_load_include('inc', 'perfmon');
if ($success) {
if (!empty($results)) {
// Store results in our present table.
perfmon_store_results($results);
}
drupal_set_message(t('Review complete'));
}
else {
$error_operation = reset($operations);
$message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
_perfmon_log('', '', $message, array(), WATCHDOG_ERROR);
drupal_set_message(t('The review did not store all results, please run again or check the logs for details.'));
}
}