Skip to content

Commit 06ad9a8

Browse files
authored
Merge pull request #1650 from algolia/feat/MAGE-1105-perf-bottlenecks
MAGE-1105: Profiling fixes and output formatting
2 parents d61d854 + cf9c017 commit 06ad9a8

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

Exception/DiagnosticsException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Exception;
4+
5+
use Magento\Framework\Exception\LocalizedException;
6+
7+
class DiagnosticsException extends LocalizedException
8+
{
9+
10+
}

Helper/Entity/ProductHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,13 +511,14 @@ public function getObject(Product $product)
511511
*/
512512
protected function getSubProducts(Product $product): array
513513
{
514-
$this->logger->startProfiling(__METHOD__);
515514
$type = $product->getTypeId();
516515

517516
if (!in_array($type, ['bundle', 'grouped', 'configurable'], true)) {
518517
return [];
519518
}
520519

520+
$this->logger->startProfiling(__METHOD__);
521+
521522
$storeId = $product->getStoreId();
522523
$typeInstance = $product->getTypeInstance();
523524

Logger/DiagnosticsLogger.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Logger;
44

5-
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
5+
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
66
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
77
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
88
use Magento\Framework\Exception\NoSuchEntityException;
@@ -17,14 +17,20 @@ class DiagnosticsLogger
1717
/** @var array */
1818
protected const ALGOLIA_TAGS = ['group' => 'algolia'];
1919
protected const PROFILE_LOG_MESSAGES_DEFAULT = false;
20+
protected const DEFAULT_NAMESPACE_DEPTH = 2;
21+
2022
protected bool $isLoggerEnabled = false;
2123
protected bool $isProfilerEnabled = false;
2224

25+
/** @var string[] */
26+
private array $timerStack = [];
27+
2328
public function __construct(
2429
protected ConfigHelper $config,
2530
protected TimedLogger $logger,
2631
protected StoreNameFetcher $storeNameFetcher
27-
) {
32+
)
33+
{
2834
$this->isLoggerEnabled = $this->config->isLoggingEnabled();
2935
$this->isProfilerEnabled = $this->config->isProfilerEnabled();
3036
}
@@ -63,7 +69,7 @@ public function start(string $action, bool $profileMethod = self::PROFILE_LOG_ME
6369

6470

6571
/**
66-
* @throws AlgoliaException
72+
* @throws DiagnosticsException
6773
*/
6874
public function stop(string $action, bool $profileMethod = self::PROFILE_LOG_MESSAGES_DEFAULT): void
6975
{
@@ -83,13 +89,26 @@ public function startProfiling(string $timerName): void
8389
{
8490
if (!$this->isProfilerEnabled) return;
8591

92+
$timerName = $this->simplifyMethodName($timerName);
93+
$this->timerStack[] = $timerName;
94+
8695
Profiler::start($timerName, self::ALGOLIA_TAGS);
8796
}
8897

98+
/**
99+
* @throws DiagnosticsException
100+
*/
89101
public function stopProfiling(string $timerName): void
90102
{
91103
if (!$this->isProfilerEnabled) return;
92104

105+
$lastTimerName = array_pop($this->timerStack);
106+
$timerName = $this->simplifyMethodName($timerName);
107+
108+
if ($lastTimerName !== $timerName) {
109+
throw new DiagnosticsException(__("Profiling on %1 was stopped before nested operation %2 completed.", $timerName, $lastTimerName));
110+
}
111+
93112
Profiler::setDefaultTags(self::ALGOLIA_TAGS);
94113
Profiler::stop($timerName);
95114
Profiler::setDefaultTags([]);
@@ -102,7 +121,8 @@ public function log(string $message, int $logLevel = Logger::INFO): void
102121
}
103122
}
104123

105-
public function error(string $message): void {
124+
public function error(string $message): void
125+
{
106126
if ($this->isLoggerEnabled) {
107127
$this->logger->log($message, Logger::ERROR);
108128
}
@@ -111,6 +131,7 @@ public function error(string $message): void {
111131
/**
112132
* Gets the name of the method that called the diagnostics
113133
*
134+
* @param int $level
114135
* @return string|null
115136
*/
116137
protected function getCallingMethodName(int $level = 2): ?string
@@ -120,4 +141,21 @@ protected function getCallingMethodName(int $level = 2): ?string
120141
? $backtrace[$level]['class'] . "::" . $backtrace[$level]['function']
121142
: null;
122143
}
144+
145+
protected function simplifyMethodName(string $methodName, int $namespaceDepth = self::DEFAULT_NAMESPACE_DEPTH): string
146+
{
147+
$separator = '\\';
148+
$parts = explode($separator, $methodName);
149+
150+
if (count($parts) <= abs($namespaceDepth)) {
151+
return $methodName;
152+
}
153+
154+
$simplified = implode($separator, array_slice($parts, $namespaceDepth));
155+
if (!count($this->timerStack)) {
156+
$simplified = "ALGOLIA:" . $simplified;
157+
}
158+
return $simplified;
159+
}
160+
123161
}

Logger/TimedLogger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Logger;
44

5-
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
5+
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
66
use Monolog\Logger;
77
use Psr\Log\LoggerInterface;
88

@@ -25,12 +25,12 @@ public function start($action): void
2525
}
2626

2727
/**
28-
* @throws AlgoliaException
28+
* @throws DiagnosticsException
2929
*/
3030
public function stop($action): void
3131
{
3232
if (false === isset($this->timers[$action])) {
33-
throw new AlgoliaException('Algolia Logger => non existing action');
33+
throw new DiagnosticsException(__('Algolia Logger => non existing action'));
3434
}
3535

3636
$this->log('<<<<< END ' . $action . ' (' . $this->formatTime($this->timers[$action], microtime(true)) . ')');

Setup/.DS_Store

-6 KB
Binary file not shown.

0 commit comments

Comments
 (0)