Skip to content

Commit 3024a5e

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-1181: Set STATIC_CONTENT_THREADS = 1 for Separate Deployment Strategies (#66)
1 parent dd59274 commit 3024a5e

File tree

6 files changed

+160
-8
lines changed

6 files changed

+160
-8
lines changed

src/StaticContent/Build/Option.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\MagentoCloud\Filesystem\DirectoryList;
1010
use Magento\MagentoCloud\Package\MagentoVersion;
1111
use Magento\MagentoCloud\StaticContent\OptionInterface;
12+
use Magento\MagentoCloud\StaticContent\ThreadCountOptimizer;
1213
use Magento\MagentoCloud\Util\ArrayManager;
1314
use Magento\MagentoCloud\Config\Build as BuildConfig;
1415

@@ -42,33 +43,44 @@ class Option implements OptionInterface
4243
*/
4344
private $buildConfig;
4445

46+
/**
47+
* @var ThreadCountOptimizer
48+
*/
49+
private $threadCountOptimizer;
50+
4551
/**
4652
* @param Environment $environment
4753
* @param ArrayManager $arrayManager
4854
* @param MagentoVersion $magentoVersion
4955
* @param DirectoryList $directoryList
5056
* @param BuildConfig $buildConfig
57+
* @param ThreadCountOptimizer $threadCountOptimizer
5158
*/
5259
public function __construct(
5360
Environment $environment,
5461
ArrayManager $arrayManager,
5562
MagentoVersion $magentoVersion,
5663
DirectoryList $directoryList,
57-
BuildConfig $buildConfig
64+
BuildConfig $buildConfig,
65+
ThreadCountOptimizer $threadCountOptimizer
5866
) {
5967
$this->environment = $environment;
6068
$this->magentoVersion = $magentoVersion;
6169
$this->arrayManager = $arrayManager;
6270
$this->directoryList = $directoryList;
6371
$this->buildConfig = $buildConfig;
72+
$this->threadCountOptimizer = $threadCountOptimizer;
6473
}
6574

6675
/**
6776
* @inheritdoc
6877
*/
6978
public function getTreadCount(): int
7079
{
71-
return (int)$this->buildConfig->get(BuildConfig::OPT_SCD_THREADS, 1);
80+
return $this->threadCountOptimizer->optimize(
81+
(int)$this->buildConfig->get(BuildConfig::OPT_SCD_THREADS, 1),
82+
$this->getStrategy()
83+
);
7284
}
7385

7486
/**

src/StaticContent/Deploy/Option.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\MagentoCloud\DB\ConnectionInterface;
1010
use Magento\MagentoCloud\Package\MagentoVersion;
1111
use Magento\MagentoCloud\StaticContent\OptionInterface;
12+
use Magento\MagentoCloud\StaticContent\ThreadCountOptimizer;
1213

1314
/**
1415
* Options for static deploy command in build process
@@ -30,27 +31,38 @@ class Option implements OptionInterface
3031
*/
3132
private $magentoVersion;
3233

34+
/**
35+
* @var ThreadCountOptimizer
36+
*/
37+
private $threadCountOptimizer;
38+
3339
/**
3440
* @param Environment $environment
3541
* @param ConnectionInterface $connection
3642
* @param MagentoVersion $magentoVersion
43+
* @param ThreadCountOptimizer $threadCountOptimizer
3744
*/
3845
public function __construct(
3946
Environment $environment,
4047
ConnectionInterface $connection,
41-
MagentoVersion $magentoVersion
48+
MagentoVersion $magentoVersion,
49+
ThreadCountOptimizer $threadCountOptimizer
4250
) {
4351
$this->environment = $environment;
4452
$this->connection = $connection;
4553
$this->magentoVersion = $magentoVersion;
54+
$this->threadCountOptimizer = $threadCountOptimizer;
4655
}
4756

4857
/**
4958
* @inheritdoc
5059
*/
5160
public function getTreadCount(): int
5261
{
53-
return $this->environment->getStaticDeployThreadsCount();
62+
return $this->threadCountOptimizer->optimize(
63+
$this->environment->getStaticDeployThreadsCount(),
64+
$this->getStrategy()
65+
);
5466
}
5567

5668
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\StaticContent;
7+
8+
class ThreadCountOptimizer
9+
{
10+
/**
11+
* @var string
12+
*/
13+
const STRATEGY_COMPACT = 'compact';
14+
15+
/**
16+
* Recommended tread count value for compact strategy
17+
*
18+
* @var int
19+
*/
20+
const THREAD_COUNT_COMPACT_STRATEGY = 1;
21+
22+
/**
23+
* Defines best thread count value based on deploy strategy name
24+
*
25+
* @param int $threads
26+
* @param string $strategy
27+
* @return int
28+
*/
29+
public function optimize(int $threads, string $strategy): int
30+
{
31+
if ($strategy === self::STRATEGY_COMPACT) {
32+
return self::THREAD_COUNT_COMPACT_STRATEGY;
33+
}
34+
35+
return $threads;
36+
}
37+
}

src/Test/Unit/StaticContent/Build/OptionTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\MagentoCloud\Filesystem\DirectoryList;
1111
use Magento\MagentoCloud\Package\MagentoVersion;
1212
use Magento\MagentoCloud\StaticContent\Build\Option;
13+
use Magento\MagentoCloud\StaticContent\ThreadCountOptimizer;
1314
use Magento\MagentoCloud\Util\ArrayManager;
1415
use PHPUnit\Framework\TestCase;
1516
use PHPUnit_Framework_MockObject_MockObject as Mock;
@@ -46,28 +47,42 @@ class OptionTest extends TestCase
4647
*/
4748
private $directoryListMock;
4849

50+
/**
51+
* @var ThreadCountOptimizer|Mock
52+
*/
53+
private $threadCountOptimizerMock;
54+
4955
protected function setUp()
5056
{
5157
$this->magentoVersionMock = $this->createMock(MagentoVersion::class);
5258
$this->directoryListMock = $this->createMock(DirectoryList::class);
5359
$this->buildConfigMock = $this->createMock(BuildConfig::class);
5460
$this->environmentMock = $this->createMock(Environment::class);
5561
$this->arrayManagerMock = $this->createMock(ArrayManager::class);
62+
$this->threadCountOptimizerMock = $this->createMock(ThreadCountOptimizer::class);
5663

5764
$this->option = new Option(
5865
$this->environmentMock,
5966
$this->arrayManagerMock,
6067
$this->magentoVersionMock,
6168
$this->directoryListMock,
62-
$this->buildConfigMock
69+
$this->buildConfigMock,
70+
$this->threadCountOptimizerMock
6371
);
6472
}
6573

6674
public function testGetThreadCount()
6775
{
68-
$this->buildConfigMock->expects($this->once())
76+
$this->buildConfigMock->expects($this->exactly(2))
6977
->method('get')
70-
->with(BuildConfig::OPT_SCD_THREADS)
78+
->withConsecutive(
79+
[BuildConfig::OPT_SCD_THREADS],
80+
[BuildConfig::OPT_SCD_STRATEGY]
81+
)
82+
->willReturnOnConsecutiveCalls(3, 'strategyName');
83+
$this->threadCountOptimizerMock->expects($this->once())
84+
->method('optimize')
85+
->with(3, 'strategyName')
7186
->willReturn(3);
7287

7388
$this->assertEquals(3, $this->option->getTreadCount());

src/Test/Unit/StaticContent/Deploy/OptionTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\MagentoCloud\DB\Connection;
1010
use Magento\MagentoCloud\Package\MagentoVersion;
1111
use Magento\MagentoCloud\StaticContent\Deploy\Option;
12+
use Magento\MagentoCloud\StaticContent\ThreadCountOptimizer;
1213
use PHPUnit\Framework\TestCase;
1314
use PHPUnit_Framework_MockObject_MockObject as Mock;
1415

@@ -34,16 +35,23 @@ class OptionTest extends TestCase
3435
*/
3536
private $connectionMock;
3637

38+
/**
39+
* @var ThreadCountOptimizer|Mock
40+
*/
41+
private $threadCountOptimizerMock;
42+
3743
protected function setUp()
3844
{
3945
$this->magentoVersionMock = $this->createMock(MagentoVersion::class);
4046
$this->connectionMock = $this->createMock(Connection::class);
4147
$this->environmentMock = $this->createMock(Environment::class);
48+
$this->threadCountOptimizerMock = $this->createMock(ThreadCountOptimizer::class);
4249

4350
$this->option = new Option(
4451
$this->environmentMock,
4552
$this->connectionMock,
46-
$this->magentoVersionMock
53+
$this->magentoVersionMock,
54+
$this->threadCountOptimizerMock
4755
);
4856
}
4957

@@ -52,6 +60,14 @@ public function testGetThreadCount()
5260
$this->environmentMock->expects($this->once())
5361
->method('getStaticDeployThreadsCount')
5462
->willReturn(3);
63+
$this->environmentMock->expects($this->once())
64+
->method('getVariable')
65+
->with(Environment::VAR_SCD_STRATEGY)
66+
->willReturn('strategyName');
67+
$this->threadCountOptimizerMock->expects($this->once())
68+
->method('optimize')
69+
->with(3, 'strategyName')
70+
->willReturn(3);
5571

5672
$this->assertEquals(3, $this->option->getTreadCount());
5773
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Test\Unit\StaticContent;
7+
8+
use Magento\MagentoCloud\StaticContent\ThreadCountOptimizer;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class ThreadCountOptimizerTest extends TestCase
12+
{
13+
/**
14+
* @var ThreadCountOptimizer
15+
*/
16+
private $optimizer;
17+
18+
protected function setUp()
19+
{
20+
$this->optimizer = new ThreadCountOptimizer();
21+
}
22+
23+
/**
24+
* @param int $threadCount
25+
* @param string $strategy
26+
* @param int $expectedThreadCount
27+
* @dataProvider optimizeDataProvider
28+
*/
29+
public function testOptimize(int $threadCount, string $strategy, int $expectedThreadCount)
30+
{
31+
$this->assertEquals(
32+
$expectedThreadCount,
33+
$this->optimizer->optimize($threadCount, $strategy)
34+
);
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function optimizeDataProvider(): array
41+
{
42+
return [
43+
[
44+
3,
45+
ThreadCountOptimizer::STRATEGY_COMPACT,
46+
1
47+
],
48+
[
49+
5,
50+
'SomeStrategy',
51+
5
52+
],
53+
[
54+
1,
55+
'SomeStrategy',
56+
1
57+
]
58+
];
59+
}
60+
}

0 commit comments

Comments
 (0)