PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
TimingsHandler.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\event;
23 
28 
30 
32  private static $HANDLERS = [];
33 
34  private $name;
36  private $parent = null;
37 
38  private $count = 0;
39  private $start = 0;
40  private $timingDepth = 0;
41  private $totalTime = 0;
42  private $curTickTotal = 0;
43  private $violations = 0;
44 
49  public function __construct($name, TimingsHandler $parent = null){
50  $this->name = $name;
51  if($parent instanceof TimingsHandler){
52  $this->parent = $parent;
53  }
54 
55  self::$HANDLERS[spl_object_hash($this)] = $this;
56  }
57 
58  public static function printTimings($fp){
59  fwrite($fp, "Minecraft" . PHP_EOL);
60 
61  foreach(self::$HANDLERS as $timings){
62  $time = $timings->totalTime;
63  $count = $timings->count;
64  if($count === 0){
65  continue;
66  }
67 
68  $avg = $time / $count;
69 
70  fwrite($fp, " " . $timings->name . " Time: " . round($time * 1000000000) . " Count: " . $count . " Avg: " . round($avg * 1000000000) . " Violations: " . $timings->violations . PHP_EOL);
71  }
72 
73  fwrite($fp, "# Version " . Server::getInstance()->getVersion() . PHP_EOL);
74  fwrite($fp, "# " . Server::getInstance()->getName() . " " . Server::getInstance()->getPocketMineVersion() . PHP_EOL);
75 
76  $entities = 0;
77  $livingEntities = 0;
78  foreach(Server::getInstance()->getLevels() as $level){
79  $entities += count($level->getEntities());
80  foreach($level->getEntities() as $e){
81  if($e instanceof Living){
82  ++$livingEntities;
83  }
84  }
85  }
86 
87  fwrite($fp, "# Entities " . $entities . PHP_EOL);
88  fwrite($fp, "# LivingEntities " . $livingEntities . PHP_EOL);
89  }
90 
91  public static function reload(){
92  if(Server::getInstance()->getPluginManager()->useTimings()){
93  foreach(self::$HANDLERS as $timings){
94  $timings->reset();
95  }
96  TimingsCommand::$timingStart = microtime(true);
97  }
98  }
99 
100  public static function tick(){
101  if(PluginManager::$useTimings){
102  foreach(self::$HANDLERS as $timings){
103  if($timings->curTickTotal > 0.05){
104  $timings->violations += round($timings->curTickTotal / 0.05);
105  }
106  $timings->curTickTotal = 0;
107  $timings->timingDepth = 0;
108  }
109  }
110  }
111 
112  public function startTiming(){
113  if(PluginManager::$useTimings and ++$this->timingDepth === 1){
114  $this->start = microtime(true);
115  if($this->parent instanceof TimingsHandler and ++$this->parent->timingDepth === 1){
116  $this->parent->start = $this->start;
117  }
118  }
119  }
120 
121  public function stopTiming(){
122  if(PluginManager::$useTimings){
123  if(--$this->timingDepth !== 0 or $this->start === 0){
124  return;
125  }
126 
127  $diff = microtime(true) - $this->start;
128  $this->totalTime += $diff;
129  $this->curTickTotal += $diff;
130  $this->count++;
131  $this->start = 0;
132  if($this->parent instanceof TimingsHandler){
133  $this->parent->stopTiming();
134  }
135  }
136  }
137 
138  public function reset(){
139  $this->count = 0;
140  $this->violations = 0;
141  $this->curTickTotal = 0;
142  $this->totalTime = 0;
143  $this->start = 0;
144  $this->timingDepth = 0;
145  }
146 
147  public function remove(){
148  unset(self::$HANDLERS[spl_object_hash($this)]);
149  }
150 
151 }
__construct($name, TimingsHandler $parent=null)
static getInstance()
Definition: Server.php:1444