From 995a4228c9f7ee80a1d13f1ebfa250d8d513e839 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 29 Apr 2020 14:43:43 +0200 Subject: [PATCH] Report memory usage --- examples/downloadRenameBot.php | 16 ++++++++++++++++ .../MTProtoTools/AuthKeyHandler.php | 7 ++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/downloadRenameBot.php b/examples/downloadRenameBot.php index 5b225cfe..5c27c240 100755 --- a/examples/downloadRenameBot.php +++ b/examples/downloadRenameBot.php @@ -19,6 +19,11 @@ * @link https://docs.madelineproto.xyz MadelineProto documentation */ + +if (function_exists('memprof_enable')) { + memprof_enable(); +} + use Amp\Http\Server\HttpServer; use danog\MadelineProto\API; use danog\MadelineProto\APIWrapper; @@ -86,6 +91,10 @@ class MyEventHandler extends \danog\MadelineProto\EventHandler $this->UPLOAD = \class_exists(HttpServer::class); parent::__construct($API); } + public function onStart() + { + $this->adminId = yield $this->getInfo(self::ADMIN)['bot_api_id']; + } /** * Handle updates from channels and supergroups. * @@ -126,6 +135,13 @@ class MyEventHandler extends \danog\MadelineProto\EventHandler if ($update['message']['message'] === '/start') { return $this->messages->sendMessage(['peer' => $peerId, 'message' => self::START, 'parse_mode' => 'Markdown', 'reply_to_msg_id' => $messageId]); } + if ($update['message']['message'] === '/report' && $peerId === $this->adminId) { + memprof_dump_callgrind($stm = fopen("php://memory", "w")); + fseek($stm, 0); + yield $this->messages->sendMedia(['peer' => $peerId, 'media' => ['_' => 'inputMediaUploadedDocument', 'file' => $stm, 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'callgrind.out']]]]); + fclose($stm); + return; + } if (isset($update['message']['media']['_']) && $update['message']['media']['_'] !== 'messageMediaWebPage') { if ($this->UPLOAD && ($this->states[$peerId] ?? false) === $this->UPLOAD) { unset($this->states[$peerId]); diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 2ae02871..d8a3435b 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -110,7 +110,8 @@ trait AuthKeyHandler * *********************************************************************** * Compute p and q */ - $pq = (string) new BigInteger((string) $pq_bytes, 256); + $pq = new BigInteger((string) $pq_bytes, 256); + $pqStr = (string) $pq; foreach ([ 'auto_single', 'native_single_cpp', @@ -124,9 +125,9 @@ trait AuthKeyHandler $q = new BigInteger(0); try { if ($method === 'wolfram') { - $p = new BigInteger(yield from $this->wolframSingle($pq)); + $p = new BigInteger(yield from $this->wolframSingle($pqStr)); } else { - $p = new BigInteger(@PrimeModule::$method($pq)); + $p = new BigInteger(@PrimeModule::$method($pqStr)); } } catch (\Throwable $e) { $this->logger->logger("While factorizing with $method: $e");