|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Mail\SetupChecks; |
| 11 | + |
| 12 | +use OCA\Mail\Account; |
| 13 | +use OCA\Mail\Db\MailAccountMapper; |
| 14 | +use OCA\Mail\Db\ProvisioningMapper; |
| 15 | +use OCA\Mail\IMAP\FolderMapper; |
| 16 | +use OCA\Mail\IMAP\IMAPClientFactory; |
| 17 | +use OCP\IL10N; |
| 18 | +use OCP\SetupCheck\ISetupCheck; |
| 19 | +use OCP\SetupCheck\SetupResult; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | +use Throwable; |
| 22 | + |
| 23 | +class MailConnectionPerformance implements ISetupCheck { |
| 24 | + public function __construct( |
| 25 | + private IL10N $l10n, |
| 26 | + private LoggerInterface $logger, |
| 27 | + private ProvisioningMapper $provisioningMapper, |
| 28 | + private MailAccountMapper $accountMapper, |
| 29 | + private IMAPClientFactory $clientFactory, |
| 30 | + private FolderMapper $folderMapper, |
| 31 | + private MicroTime $microtime, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function getName(): string { |
| 36 | + return $this->l10n->t('Mail connection performance'); |
| 37 | + } |
| 38 | + |
| 39 | + public function getCategory(): string { |
| 40 | + return 'mail'; |
| 41 | + } |
| 42 | + |
| 43 | + public function run(): SetupResult { |
| 44 | + // retrieve unique imap hosts for provisionings and abort if none exists |
| 45 | + $hosts = $this->provisioningMapper->findUniqueImapHosts(); |
| 46 | + if (empty($hosts)) { |
| 47 | + return SetupResult::success(); |
| 48 | + } |
| 49 | + // retrieve random account ids for each host |
| 50 | + $accounts = []; |
| 51 | + foreach ($hosts as $host) { |
| 52 | + $accounts[$host] = $this->accountMapper->getRandomAccountIdsByImapHost($host); |
| 53 | + } |
| 54 | + // test accounts |
| 55 | + $tests = []; |
| 56 | + foreach ($accounts as $host => $collection) { |
| 57 | + foreach ($collection as $accountId) { |
| 58 | + $account = new Account($this->accountMapper->findById((int)$accountId)); |
| 59 | + $client = $this->clientFactory->getClient($account); |
| 60 | + try { |
| 61 | + $tStart = $this->microtime->get(true); |
| 62 | + // time login |
| 63 | + $client->login(); |
| 64 | + $tLogin = $this->microtime->get(true); |
| 65 | + // time operation |
| 66 | + $list = $client->listMailboxes('*'); |
| 67 | + $status = $client->status(key($list)); |
| 68 | + $tOperation = $this->microtime->get(true); |
| 69 | + |
| 70 | + $tests[$host][$accountId] = ['start' => $tStart, 'login' => $tLogin, 'operation' => $tOperation]; |
| 71 | + } catch (Throwable $e) { |
| 72 | + $this->logger->warning('Error occurred while performing system check on mail account: ' . $account->getId()); |
| 73 | + } finally { |
| 74 | + $client->close(); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + // calculate performance |
| 79 | + $performance = []; |
| 80 | + foreach ($tests as $host => $test) { |
| 81 | + $tLogin = 0; |
| 82 | + $tOperation = 0; |
| 83 | + foreach ($test as $entry) { |
| 84 | + [$start, $login, $operation] = array_values($entry); |
| 85 | + $tLogin += ($login - $start); |
| 86 | + $tOperation += ($operation - $login); |
| 87 | + } |
| 88 | + $performance[$host]['login'] = $tLogin / count($tests[$host]); |
| 89 | + $performance[$host]['operation'] = $tOperation / count($tests[$host]); |
| 90 | + } |
| 91 | + // display performance test outcome |
| 92 | + foreach ($performance as $host => $entry) { |
| 93 | + [$login, $operation] = array_values($entry); |
| 94 | + if ($login > 1) { |
| 95 | + return SetupResult::warning( |
| 96 | + $this->l10n->t('Slow mail service detected (%1$s) an attempt to connect to several accounts took an average of %2$s seconds per account', [$host, round($login, 3)]) |
| 97 | + ); |
| 98 | + } |
| 99 | + if ($operation > 1) { |
| 100 | + return SetupResult::warning( |
| 101 | + $this->l10n->t('Slow mail service detected (%1$s) an attempt to perform a mail box list operation on several accounts took an average of %2$s seconds per account', [$host, round($operation, 3)]) |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + return SetupResult::success(); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments