Development

/plugins/elXHProfPlugin/trunk/lib/XHProfRun.class.php

You must first sign up to be able to contribute.

root/plugins/elXHProfPlugin/trunk/lib/XHProfRun.class.php

Revision 27549, 2.8 kB (checked in by Eric.Lemoine, 3 years ago)

initial code

Line 
1 <?php
2 class XHProfRun
3 {
4  
5   /**
6    * Namespace du run
7    *
8    * @var string
9    */
10   protected $namespace;
11  
12   /**
13    * Id du run
14    *
15    * @var string
16    */
17   protected $id;
18  
19   /**
20    * Id du run
21    *
22    * @var string
23    */
24   protected $data;
25  
26   /**
27    * Etat du run
28    *
29    * @var string
30    */
31   protected $started = false;
32  
33   /**
34    * Date du run
35    *
36    * @var string
37    */
38   protected $date;
39  
40   /**
41    * Constructeur du run, le run est automatiquement démarré
42    *
43    * @param string $namespace
44    */
45   public function __construct($namespace, $autostart = true)
46   {
47     $this->namespace = $namespace;
48     
49     if ($autostart)
50     {
51       $this->start();
52     }
53   }
54  
55   /**
56    * Setter pour l'id
57    *
58    * @param string $id
59    */
60   public function setId($id)
61   {
62     $this->id = $id;
63   }
64  
65   /**
66    * Démarrage du profiling
67    *
68    */
69   public function start()
70   {
71     if (!extension_loaded('xhprof'))
72     {
73       throw new sfException("XHProf extension is not loaded.");
74     }
75     if (!$this->started)
76     {
77       xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
78       $this->started = true;
79     }
80   }
81  
82   /**
83    * Déclenche la fin du run, l'enregistrement des données et l'association du run au pool
84    *
85    */
86   public function end()
87   {
88     if (!extension_loaded('xhprof'))
89     {
90       throw new sfException("XHProf extension is not loaded.");
91     }
92     if ($this->started)
93     {
94       $this->data = xhprof_disable();
95       
96       require_once(dirname(__FILE__).'/vendor/facebook/xhprof/utils/xhprof_runs.php');
97       $xhprof_runs = new XHProfRuns_Default();
98       $this->id = $xhprof_runs->save_run($this->data, $this->namespace);
99       
100       $this->register();
101     }
102     else
103     {
104       throw new sfException("This run has not been started.");
105     }
106   }
107  
108   /**
109    * Getter sur l'id du run
110    *
111    * @return string
112    */
113   public function getId()
114   {
115     return $this->id;
116   }
117  
118   /**
119    * Getter sur le namespace
120    *
121    * @return string
122    */
123   public function getNamespace()
124   {
125     return $this->namespace;
126   }
127  
128   /**
129    * Génération de l'url de consultation du run
130    *
131    * @return string
132    */
133   public function getUrl()
134   {
135     return sprintf('%s?run=%s&source=%s', _compute_public_path('index.php', 'elXHProfPlugin', 'php'),$this->getId(), $this->getNamespace());
136   }
137  
138   /**
139    * Enregistrement du run au pool
140    *
141    */
142   protected function register()
143   {
144     $pool = XHProfRunPool::getInstance();
145     
146     $pool->register($this);
147   }
148  
149   /**
150    * Returns run timestamp
151    *
152    * @return int
153    */
154   public function getDate()
155   {
156     return filemtime($this->getFilename());
157   }
158  
159   /**
160    * Returns filepath of the data file
161    *
162    * @return string
163    */
164   public function getFilename()
165   {
166     return sprintf('%s/%s.%s', ini_get("xhprof.output_dir"), $this->getId(), $this->getNamespace());
167   }
168 }
169
Note: See TracBrowser for help on using the browser.