PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
TimeTrackingSleeperHandler.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
22declare(strict_types=1);
23
24namespace pocketmine;
25
30use function hrtime;
31
37
38 private int $notificationProcessingTimeNs = 0;
39
44 private static array $handlerTimings = [];
45
46 public function __construct(
47 private TimingsHandler $timings
48 ){
49 parent::__construct();
50 }
51
52 public function addNotifier(\Closure $handler) : SleeperHandlerEntry{
53 $name = Utils::getNiceClosureName($handler);
54 $timings = self::$handlerTimings[$name] ??= new TimingsHandler("Snooze Handler: " . $name, $this->timings);
55
56 return parent::addNotifier(function() use ($timings, $handler) : void{
57 $timings->startTiming();
58 $handler();
59 $timings->stopTiming();
60 });
61 }
62
66 public function getNotificationProcessingTime() : int{ return $this->notificationProcessingTimeNs; }
67
71 public function resetNotificationProcessingTime() : void{ $this->notificationProcessingTimeNs = 0; }
72
73 public function processNotifications() : void{
74 $startTime = hrtime(true);
75 $this->timings->startTiming();
76 try{
77 parent::processNotifications();
78 }finally{
79 $this->notificationProcessingTimeNs += hrtime(true) - $startTime;
80 $this->timings->stopTiming();
81 }
82 }
83}