Skip to content

Commit e2de133

Browse files
ACPT-1584
Fixing static tests and making requested changes
1 parent 8d8ba85 commit e2de133

File tree

10 files changed

+77
-61
lines changed

10 files changed

+77
-61
lines changed

app/code/Magento/ApplicationPerformanceMonitor/Profiler/Input/GeneralInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\AppInterface;
1212

1313
/**
14-
* Outputs the performance metrics and other information to Logger
14+
* Adds applicationClass based on the current application
1515
*/
1616
class GeneralInput implements InputInterface
1717
{

app/code/Magento/ApplicationPerformanceMonitor/Profiler/Metric.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@
1313
*/
1414
class Metric
1515
{
16-
// TODO: move these consts to enum once Magento's SVC is fixed to support enums.
17-
public const TYPE_OTHER = 1;
18-
public const TYPE_SECONDS_ELAPSED_FLOAT = 1;
19-
public const TYPE_UNIX_TIMESTAMP_FLOAT = 2;
20-
public const TYPE_MEMORY_SIZE_INT = 3;
21-
2216
/**
2317
* @param int $type
2418
* @param string $name
2519
* @param mixed $value
2620
* @param bool $verbose
2721
*/
28-
public function __construct(private int $type, private string $name, private mixed $value, private bool $verbose)
29-
{
22+
public function __construct(
23+
private readonly MetricType $type,
24+
private readonly string $name,
25+
private readonly mixed $value,
26+
private readonly bool $verbose,
27+
) {
3028
}
3129

3230
/**
33-
* @return int
31+
* @return MetricType
3432
*/
3533
public function getType()
3634
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ApplicationPerformanceMonitor\Profiler;
9+
10+
/**
11+
* Enum for which type of metric
12+
*/
13+
enum MetricType
14+
{
15+
case Other;
16+
case SecondsElapsedFloat;
17+
case UnixTimestampFloat;
18+
case MemorySizeInt;
19+
}

app/code/Magento/ApplicationPerformanceMonitor/Profiler/Metrics.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
class Metrics
1414
{
1515
public function __construct(
16-
private int $peakMemoryUsage,
17-
private int $memoryUsage,
18-
private array $rusage,
19-
private float $microtime
16+
private readonly int $peakMemoryUsage,
17+
private readonly int $memoryUsage,
18+
private readonly array $rusage,
19+
private readonly float $microtime
2020
) {
2121
}
2222

app/code/Magento/ApplicationPerformanceMonitor/Profiler/MetricsComparator.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MetricsComparator
1515
/**
1616
* @param MetricFactory $metricFactory
1717
*/
18-
public function __construct(private MetricFactory $metricFactory)
18+
public function __construct(private readonly MetricFactory $metricFactory)
1919
{
2020
}
2121

@@ -32,103 +32,103 @@ public function compareMetrics(Metrics $beforeMetrics, Metrics $afterMetrics, ?M
3232
{
3333
$metrics = [];
3434
$metrics['memoryUsageBefore'] = $this->metricFactory->create([
35-
'type' => Metric::TYPE_MEMORY_SIZE_INT,
35+
'type' => MetricType::MemorySizeInt,
3636
'name' => 'memoryUsageBefore',
3737
'value' => $beforeMetrics->getMemoryUsage(),
3838
'verbose' => true,
3939
]);
4040
$metrics['memoryUsageAfter'] = $this->metricFactory->create([
41-
'type' => Metric::TYPE_MEMORY_SIZE_INT,
41+
'type' => MetricType::MemorySizeInt,
4242
'name' => 'memoryUsageAfter',
4343
'value' => $afterMetrics->getMemoryUsage(),
4444
'verbose' => false,
4545
]);
4646
if ($previousAfterMetrics) {
4747
$metrics['memoryUsageAfterComparedToPrevious'] = $this->metricFactory->create([
48-
'type' => Metric::TYPE_MEMORY_SIZE_INT,
48+
'type' => MetricType::MemorySizeInt,
4949
'name' => 'memoryUsageAfterComparedToPrevious',
5050
'value' => $afterMetrics->getMemoryUsage() - $previousAfterMetrics->getMemoryUsage(),
5151
'verbose' => false,
5252
]);
5353
}
5454
$metrics['memoryUsageDelta'] = $this->metricFactory->create([
55-
'type' => Metric::TYPE_MEMORY_SIZE_INT,
55+
'type' => MetricType::MemorySizeInt,
5656
'name' => 'memoryUsageDelta',
5757
'value' => $afterMetrics->getMemoryUsage() - $beforeMetrics->getMemoryUsage(),
5858
'verbose' => false,
5959
]);
6060
$metrics['peakMemoryUsageBefore'] = $this->metricFactory->create([
61-
'type' => Metric::TYPE_MEMORY_SIZE_INT,
61+
'type' => MetricType::MemorySizeInt,
6262
'name' => 'peakMemoryUsageBefore',
6363
'value' => $beforeMetrics->getPeakMemoryUsage(),
6464
'verbose' => true,
6565
]);
6666
$metrics['peakMemoryUsageAfter'] = $this->metricFactory->create([
67-
'type' => Metric::TYPE_MEMORY_SIZE_INT,
67+
'type' => MetricType::MemorySizeInt,
6868
'name' => 'peakMemoryUsageAfter',
6969
'value' => $afterMetrics->getPeakMemoryUsage(),
7070
'verbose' => false,
7171
]);
7272
$metrics['peakMemoryUsageDelta'] = $this->metricFactory->create([
73-
'type' => Metric::TYPE_MEMORY_SIZE_INT,
73+
'type' => MetricType::MemorySizeInt,
7474
'name' => 'peakMemoryUsageDelta',
7575
'value' => $afterMetrics->getPeakMemoryUsage() - $beforeMetrics->getPeakMemoryUsage(),
7676
'verbose' => false,
7777
]);
7878
$metrics['wallTimeBefore'] = $this->metricFactory->create([
79-
'type' => Metric::TYPE_UNIX_TIMESTAMP_FLOAT,
79+
'type' => MetricType::UnixTimestampFloat,
8080
'name' => 'wallTimeBefore',
8181
'value' => $beforeMetrics->getMicrotime(),
8282
'verbose' => true,
8383
]);
8484
$metrics['wallTimeAfter'] = $this->metricFactory->create([
85-
'type' => Metric::TYPE_UNIX_TIMESTAMP_FLOAT,
85+
'type' => MetricType::UnixTimestampFloat,
8686
'name' => 'wallTimeAfter',
8787
'value' => $afterMetrics->getMicrotime(),
8888
'verbose' => true,
8989
]);
9090
$metrics['wallTimeElapsed'] = $this->metricFactory->create([
91-
'type' => Metric::TYPE_SECONDS_ELAPSED_FLOAT,
91+
'type' => MetricType::SecondsElapsedFloat,
9292
'name' => 'wallTimeElapsed',
9393
'value' => $afterMetrics->getMicrotime() - $beforeMetrics->getMicrotime(),
9494
'verbose' => false,
9595
]);
9696
$metrics['userTimeBefore'] = $this->metricFactory->create([
97-
'type' => Metric::TYPE_SECONDS_ELAPSED_FLOAT,
97+
'type' => MetricType::SecondsElapsedFloat,
9898
'name' => 'userTimeBefore',
9999
'value' => $beforeMetrics->getRusage()['ru_utime.tv_sec']
100100
+ 0.000001 * $beforeMetrics->getRusage()['ru_utime.tv_usec'],
101101
'verbose' => true,
102102
]);
103103
$metrics['userTimeAfter'] = $this->metricFactory->create([
104-
'type' => Metric::TYPE_SECONDS_ELAPSED_FLOAT,
104+
'type' => MetricType::SecondsElapsedFloat,
105105
'name' => 'userTimeAfter',
106106
'value' => $afterMetrics->getRusage()['ru_utime.tv_sec']
107107
+ 0.000001 * $afterMetrics->getRusage()['ru_utime.tv_usec'],
108108
'verbose' => true,
109109
]);
110110
$metrics['userTimeElapsed'] = $this->metricFactory->create([
111-
'type' => Metric::TYPE_SECONDS_ELAPSED_FLOAT,
111+
'type' => MetricType::SecondsElapsedFloat,
112112
'name' => 'userTimeElapsed',
113113
'value' => $metrics['userTimeAfter']->getValue() - $metrics['userTimeBefore']->getValue(),
114114
'verbose' => true,
115115
]);
116116
$metrics['systemTimeBefore'] = $this->metricFactory->create([
117-
'type' => Metric::TYPE_SECONDS_ELAPSED_FLOAT,
117+
'type' => MetricType::SecondsElapsedFloat,
118118
'name' => 'systemTimeBefore',
119119
'value' => $beforeMetrics->getRusage()['ru_stime.tv_sec']
120120
+ 0.000001 * $beforeMetrics->getRusage()['ru_stime.tv_usec'],
121121
'verbose' => true,
122122
]);
123123
$metrics['systemTimeAfter'] = $this->metricFactory->create([
124-
'type' => Metric::TYPE_SECONDS_ELAPSED_FLOAT,
124+
'type' => MetricType::SecondsElapsedFloat,
125125
'name' => 'systemTimeAfter',
126126
'value' => $afterMetrics->getRusage()['ru_stime.tv_sec']
127127
+ 0.000001 * $afterMetrics->getRusage()['ru_stime.tv_usec'],
128128
'verbose' => true,
129129
]);
130130
$metrics['systemTimeElapsed'] = $this->metricFactory->create([
131-
'type' => Metric::TYPE_SECONDS_ELAPSED_FLOAT,
131+
'type' => MetricType::SecondsElapsedFloat,
132132
'name' => 'systemTimeElapsed',
133133
'value' => $metrics['systemTimeAfter']->getValue() - $metrics['systemTimeBefore']->getValue(),
134134
'verbose' => true,

app/code/Magento/ApplicationPerformanceMonitor/Profiler/MetricsGatherer.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MetricsGatherer
1515
/**
1616
* @param MetricsFactory $metricsFactory
1717
*/
18-
public function __construct(private MetricsFactory $metricsFactory)
18+
public function __construct(private readonly MetricsFactory $metricsFactory)
1919
{
2020
}
2121

@@ -26,15 +26,11 @@ public function __construct(private MetricsFactory $metricsFactory)
2626
*/
2727
public function gatherMetrics()
2828
{
29-
$memoryUsage = \memory_get_usage();
30-
$peakMemoryUsage = \memory_get_peak_usage();
31-
$rusage = \getrusage();
32-
$microtime = \microtime(true);
3329
return $this->metricsFactory->create([
34-
'memoryUsage' => $memoryUsage,
35-
'peakMemoryUsage' => $peakMemoryUsage,
36-
'rusage' => $rusage,
37-
'microtime' => $microtime,
30+
'memoryUsage' => \memory_get_usage(),
31+
'peakMemoryUsage' => \memory_get_peak_usage(),
32+
'rusage' => \getrusage(),
33+
'microtime' => \microtime(true),
3834
]);
3935
}
4036
}

app/code/Magento/ApplicationPerformanceMonitor/Profiler/Output/LoggerOutput.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\ApplicationPerformanceMonitor\Profiler\Output;
99

1010
use Magento\ApplicationPerformanceMonitor\Profiler\Metric;
11+
use Magento\ApplicationPerformanceMonitor\Profiler\MetricType;
1112
use Magento\ApplicationPerformanceMonitor\Profiler\OutputInterface;
1213
use Magento\Framework\App\DeploymentConfig;
1314
use Psr\Log\LoggerInterface;
@@ -24,8 +25,10 @@ class LoggerOutput implements OutputInterface
2425
* @param LoggerInterface $logger
2526
* @param DeploymentConfig $deploymentConfig
2627
*/
27-
public function __construct(private LoggerInterface $logger, private DeploymentConfig $deploymentConfig)
28-
{
28+
public function __construct(
29+
private readonly LoggerInterface $logger,
30+
private readonly DeploymentConfig $deploymentConfig,
31+
) {
2932
}
3033

3134
/**
@@ -42,7 +45,7 @@ public function isEnabled(): bool
4245
/**
4346
* @inheritDoc
4447
*/
45-
public function doOutput(array $metrics, array $information)
48+
public function doOutput(array $metrics, array $information) : void
4649
{
4750
if (!$this->isEnabled()) {
4851
return;
@@ -86,13 +89,13 @@ private function doOutputMetrics(array $metrics, bool $verbose)
8689
continue;
8790
}
8891
switch ($metric->getType()) {
89-
case Metric::TYPE_SECONDS_ELAPSED_FLOAT:
92+
case MetricType::SecondsElapsedFloat:
9093
$prettyMetrics[$metric->getName()] = $this->prettyElapsedTime($metric->getValue());
9194
break;
92-
case Metric::TYPE_UNIX_TIMESTAMP_FLOAT:
95+
case MetricType::UnixTimestampFloat:
9396
$prettyMetrics[$metric->getName()] = $this->prettyUnixTime($metric->getValue());
9497
break;
95-
case Metric::TYPE_MEMORY_SIZE_INT:
98+
case MetricType::MemorySizeInt:
9699
$prettyMetrics[$metric->getName()] = $this->prettyMemorySize($metric->getValue());
97100
break;
98101
default:

app/code/Magento/ApplicationPerformanceMonitor/Profiler/OutputInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ public function isEnabled() : bool;
2727
* @param array $information
2828
* @return void
2929
*/
30-
public function doOutput(array $metrics, array $information);
30+
public function doOutput(array $metrics, array $information) : void;
3131
}

app/code/Magento/ApplicationPerformanceMonitor/Profiler/Profiler.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
*/
1515
class Profiler
1616
{
17-
/** @var OutputInterface[] */
18-
private array $outputs;
19-
20-
/** @var InputInterface[] */
21-
private array $inputs;
22-
17+
/**
18+
* @var Metrics|null used for comparing against previous metrics
19+
*/
2320
private ?Metrics $previousAfterMetrics = null;
21+
22+
/**
23+
* @var int used for keeping track of how many requests were already processed by this thread
24+
*/
2425
private int $previousRequestCount = 0;
2526

2627
/**
@@ -30,13 +31,11 @@ class Profiler
3031
* @param MetricsGatherer $metricsGatherer
3132
*/
3233
public function __construct(
33-
array $outputs,
34-
array $inputs,
35-
private MetricsComparator $metricsComparator,
36-
private MetricsGatherer $metricsGatherer
34+
private readonly array $outputs,
35+
private readonly array $inputs,
36+
private readonly MetricsComparator $metricsComparator,
37+
private readonly MetricsGatherer $metricsGatherer,
3738
) {
38-
$this->outputs = $outputs;
39-
$this->inputs = $inputs;
4039
}
4140

4241
/**

app/code/Magento/ApplicationPerformanceMonitorNewRelic/Profiler/Output/NewRelicOutput.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class NewRelicOutput implements OutputInterface
2020
public const CONFIG_ENABLE_KEY = 'application/performance_monitor/newrelic_output_enable';
2121
public const CONFIG_VERBOSE_KEY = 'application/performance_monitor/newrelic_output_verbose';
2222

23-
public function __construct(private DeploymentConfig $deploymentConfig, private NewRelicWrapper $newRelicWrapper)
23+
public function __construct(
24+
private readonly DeploymentConfig $deploymentConfig, private readonly NewRelicWrapper $newRelicWrapper)
2425
{
2526
}
2627

@@ -41,7 +42,7 @@ public function isEnabled(): bool
4142
/**
4243
* @inheritDoc
4344
*/
44-
public function doOutput(array $metrics, array $information)
45+
public function doOutput(array $metrics, array $information) : void
4546
{
4647
if (!$this->isEnabled()) {
4748
return;

0 commit comments

Comments
 (0)