Passed
Branch 8.x-1.x (e6d5ba)
by Frédéric G.
03:03
created

instrument.drush.inc::instrument_drush_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @file
4
 * A simple Drush plugin to report on time spent and memory usage
5
 * without all the noise from the Drush "-d" option. Results look like:
6
7
$ drush cc all
8
'all' cache was cleared.                                                                                                                   [success]
9
Duration:  3.39 seconds
10
Memory:
11
Initial: malloc =  9.01M           real = 10.00M
12
Final:   malloc =  9.60M (+ 0.59M) real = 11.00M (+ 1.00M)
13
Peak:    malloc = 10.40M (+ 1.39M) real = 11.00M (+ 1.00M)
14
15
 */
16
17
/**
18
 * Implements hook_drush_init().
19
 */
20
function instrument_drush_init() {
21
  global $instrument;
22
  $instrument = [];
23
  $instrument['times'] = [microtime(true)];
24
  $instrument['memory'] = [[
25
    'current' => [
26
      'malloc' => memory_get_usage(),
27
      'true' => memory_get_usage(TRUE),
28
    ]
29
  ]];
30
}
31
32
/**
33
 * Implements hook_drush_exit().
34
 */
35
function instrument_drush_exit() {
36
  global $instrument;
37
  $instrument['times'][] = microtime(true);
38
  $instrument['memory'][] = [
39
    'current' => [
40
      'malloc' => memory_get_usage(),
41
      'true' => memory_get_usage(TRUE),
42
    ],
43
    'peak' => [
44
      'malloc' => memory_get_peak_usage(),
45
      'true' => memory_get_peak_usage(TRUE),
46
    ]
47
  ];
48
49
  $memory = $instrument['memory'];
50
  printf("Duration: %5.2f seconds\n", $instrument['times'][1] - $instrument['times'][0]);
51
  $format = <<<FORMAT
52
Memory:
53
  Initial: malloc = %5.2fM           real = %5.2fM
54
  Final:   malloc = %5.2fM (+%5.2fM) real = %5.2fM (+%5.2fM)
55
  Peak:    malloc = %5.2fM (+%5.2fM) real = %5.2fM (+%5.2fM)
56
57
FORMAT;
58
  $mega = 2 << 20;
59
  printf($format,
60
    $memory[0]['current']['malloc'] / $mega,
61
    $memory[0]['current']['true'] / $mega,
62
    $memory[1]['current']['malloc'] / $mega,
63
    ($memory[1]['current']['malloc'] - $memory[0]['current']['malloc']) / $mega,
64
    $memory[1]['current']['true'] / $mega,
65
    ($memory[1]['current']['true'] - $memory[0]['current']['true']) / $mega,
66
    $memory[1]['peak']['malloc'] / $mega,
67
    ($memory[1]['peak']['malloc'] - $memory[0]['current']['malloc']) / $mega,
68
    $memory[1]['peak']['true'] / $mega,
69
    ($memory[1]['peak']['true'] - $memory[0]['current']['true']) / $mega
70
  );
71
}
72