PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
TimingsCommand.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\command\defaults;
23 
27 
29 
30  public static $timingStart = 0;
31 
32  public function __construct($name){
33  parent::__construct(
34  $name,
35  "Records timings to see performance of the server.",
36  "/timings <reset|report|on|off|paste>"
37  );
38  $this->setPermission("pocketmine.command.timings");
39  }
40 
41  public function execute(CommandSender $sender, $currentAlias, array $args){
42  if(!$this->testPermission($sender)){
43  return true;
44  }
45 
46  if(count($args) !== 1){
47  $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
48 
49  return true;
50  }
51 
52  $mode = strtolower($args[0]);
53 
54  if($mode === "on"){
55  $sender->getServer()->getPluginManager()->setUseTimings(true);
56  TimingsHandler::reload();
57  $sender->sendMessage("Enabled Timings & Reset");
58 
59  return true;
60  }elseif($mode === "off"){
61  $sender->getServer()->getPluginManager()->setUseTimings(false);
62  $sender->sendMessage("Disabled Timings");
63  }
64 
65  if(!$sender->getServer()->getPluginManager()->useTimings()){
66  $sender->sendMessage("Please enable timings by typing /timings on");
67 
68  return true;
69  }
70 
71  $paste = $mode === "paste";
72 
73  if($mode === "reset"){
74  TimingsHandler::reload();
75  $sender->sendMessage("Timings reset");
76  }elseif($mode === "merged" or $mode === "report" or $paste){
77 
78  $sampleTime = microtime(true) - self::$timingStart;
79  $index = 0;
80  $timingFolder = $sender->getServer()->getDataPath() . "timings/";
81  @mkdir($timingFolder, 0777);
82  $timings = $timingFolder . "timings.txt";
83  while(file_exists($timings)){
84  $timings = $timingFolder . "timings" . (++$index) . ".txt";
85  }
86 
87  $fileTimings = $paste ? fopen("php://temp", "r+b") : fopen($timings, "a+b");
88 
89  TimingsHandler::printTimings($fileTimings);
90 
91  fwrite($fileTimings, "Sample time " . round($sampleTime * 1000000000) . " (" . $sampleTime . "s)" . PHP_EOL);
92 
93  if($paste){
94  fseek($fileTimings, 0);
95  $data = [
96  "syntax" => "text",
97  "poster" => $sender->getServer()->getName(),
98  "content" => stream_get_contents($fileTimings)
99  ];
100 
101  $ch = curl_init("http://paste.ubuntu.com/");
102  curl_setopt($ch, CURLOPT_POST, 1);
103  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
104  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
105  curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
106  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
107  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
108  curl_setopt($ch, CURLOPT_AUTOREFERER, false);
109  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
110  curl_setopt($ch, CURLOPT_HEADER, true);
111  curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: " . $this->getName() . " " . $sender->getServer()->getPocketMineVersion()]);
112  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
113  $data = curl_exec($ch);
114  curl_close($ch);
115  if(preg_match('#^Location: http://paste\\.ubuntu\\.com/([0-9]{1,})/#m', $data, $matches) == 0){
116  $sender->sendMessage("An error happened while pasting the report");
117 
118  return true;
119  }
120 
121 
122  $sender->sendMessage("Timings uploaded to http://paste.ubuntu.com/" . $matches[1] . "/");
123  $sender->sendMessage("You can read the results at http://timings.aikar.co/?url=" . $matches[1]);
124  fclose($fileTimings);
125  }else{
126  fclose($fileTimings);
127  $sender->sendMessage("Timings written to " . $timings);
128  }
129  }
130 
131  return true;
132  }
133 }
testPermission(CommandSender $target)
Definition: Command.php:121
setPermission($permission)
Definition: Command.php:112