Blame view

src/lib/profiler.php 3.51 KB
1ea3b987   Administrator   maby first commit
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
  <?php
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  namespace
  {
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
      /**
       * profiler Class
       *
       * @author          Roman Telychko
       * @version         1.0.20130926
       */
      class profiler extends \core
      {
          ///////////////////////////////////////////////////////////////////////
  
          protected       $stats      = array();
  
          ///////////////////////////////////////////////////////////////////////
  
          /**
           * profiler::setStatistics()
           *
           * @author          Roman Telychko
           * @version         1.0.20131112
           *
           * @param           array       $data
           * @return          bool
           */
          public function setStatistics( $data = [] )
          {
              if( !isset($data['type']) )
              {
                  return false;
              }
  
              $this->stats[ $data['type'] ][] = $data;
          }
  
          ///////////////////////////////////////////////////////////////////////
  
          /**
           * profiler::getStatistics()
           *
           * @author          Roman Telychko
           * @version         1.0.20131112
           *
           * @param           string          $type
           * @return          array
           */
          public function getStatistics( $type = 'sql' )
          {
              if( empty($this->stats) || !isset($this->stats[$type]) || empty($this->stats[$type]) )
              {
                  return false;
              }
  
              return $this->stats[ $type ];
          }
  
          ///////////////////////////////////////////////////////////////////////
  
          /**
           * profiler::getInfoStatistics()
           *
           * @author          Roman Telychko
           * @version         1.0.20131112
           *
           * @return          array
           */
          public function getInfoStatistics()
          {
              return
              [
                  'exec'      => ( defined('START_TIME') ? round( ( microtime(true) - START_TIME ) * 1000, 3 ) : 0 ),             // in ms
                  'memory'    => round( memory_get_peak_usage()/1024, 3 ),                                                        // in KB
                  'db'        =>
                  [
                      'count'     => $this->getStatistics('sql') ? count( $this->getStatistics('sql') ) : 0,
                      'time'      => round( array_sum( $this->getDi()->get('common')->array_column( $this->getStatistics('sql'), 'time' ) ) * 1000, 3 ),   // in ms
                  ]
              ];
          }
  
          ///////////////////////////////////////////////////////////////////////
  
          /**
           * profiler::getAllStatistics()
           *
           * @author          Roman Telychko
           * @version         1.0.20131112
           *
           * @return          array
           */
          public function getAllStatistics()
          {
              return $this->stats;
          }
  
          ///////////////////////////////////////////////////////////////////////
      }
  
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  }
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////