Skip to content

Commit

Permalink
Merge pull request #1042 from magento-performance/MAGETWO-66727
Browse files Browse the repository at this point in the history
[Performance] MAGETWO-66727: Update performance profiles with swatches
  • Loading branch information
kandy authored Apr 21, 2017
2 parents 404a1a0 + c6acc08 commit 33261e7
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 48 deletions.
11 changes: 9 additions & 2 deletions setup/performance-toolkit/profiles/ce/extra_large.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
<simple_products>600000</simple_products> <!-- Simple products count -->
<configurable_products> <!-- Configurable product -->
<config>
<attributes>1</attributes>
<options>24</options>
<attributes>
<attribute>
<options>3</options>
<swatches>image</swatches>
</attribute>
<attribute>
<options>8</options>
</attribute>
</attributes>
<sku>Configurable Product %s</sku>
<products>16000</products>
</config>
Expand Down
11 changes: 9 additions & 2 deletions setup/performance-toolkit/profiles/ce/large.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
<simple_products>300000</simple_products> <!-- Simple products count -->
<configurable_products> <!-- Configurable product -->
<config>
<attributes>1</attributes>
<options>24</options>
<attributes>
<attribute>
<options>3</options>
<swatches>image</swatches>
</attribute>
<attribute>
<options>8</options>
</attribute>
</attributes>
<sku>Configurable Product %s</sku>
<products>8000</products>
</config>
Expand Down
11 changes: 9 additions & 2 deletions setup/performance-toolkit/profiles/ce/medium.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
<simple_products>24000</simple_products> <!-- Simple products count -->
<configurable_products> <!-- Configurable product -->
<config>
<attributes>1</attributes>
<options>24</options>
<attributes>
<attribute>
<options>3</options>
<swatches>image</swatches>
</attribute>
<attribute>
<options>8</options>
</attribute>
</attributes>
<sku>Configurable Product %s</sku>
<products>640</products>
</config>
Expand Down
11 changes: 9 additions & 2 deletions setup/performance-toolkit/profiles/ce/medium_msite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
<simple_products>4000</simple_products> <!-- Simple products count -->
<configurable_products> <!-- Configurable product -->
<config>
<attributes>1</attributes>
<options>24</options>
<attributes>
<attribute>
<options>3</options>
<swatches>image</swatches>
</attribute>
<attribute>
<options>8</options>
</attribute>
</attributes>
<sku>Configurable Product %s</sku>
<products>800</products>
</config>
Expand Down
11 changes: 9 additions & 2 deletions setup/performance-toolkit/profiles/ce/small.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
<simple_products>800</simple_products> <!-- Simple products count -->
<configurable_products> <!-- Configurable product -->
<config>
<attributes>1</attributes>
<options>24</options>
<attributes>
<attribute>
<options>3</options>
<swatches>image</swatches>
</attribute>
<attribute>
<options>8</options>
</attribute>
</attributes>
<sku>Configurable Product %s</sku>
<products>16</products>
</config>
Expand Down
2 changes: 1 addition & 1 deletion setup/src/Magento/Setup/Fixtures/AttributeSet/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function generateAttributeSet(
for ($index = 1; $index <= $attributesPerSet; $index++) {
$attributeData = $this->generateAttribute(
$index,
$optionsPerAttribute
is_array($optionsPerAttribute) ? $optionsPerAttribute[$index-1] : $optionsPerAttribute
);
if (is_callable($attributePattern)) {
$attributeData = $attributePattern($index, $attributeData);
Expand Down
143 changes: 112 additions & 31 deletions setup/src/Magento/Setup/Fixtures/ConfigurableProductsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@
* </config>
* </configurable_products>
*
* 2.3 Generate products based on dynamically created attribute set with specified configuration per each attribute
* <configurable_products> <!-- Configurable product -->
* <config>
* <attributes>
* <!-- Configuration for a first attribute -->
* <attribute>
* <options>{Amount of options per attribute}</options>
* <swatches>color|image</swatches> Type of Swatch attribute
* </attribute>
* <!-- Configuration for a second attribute -->
* <attribute>
* <options>{Amount of options per attribute}</options>
* </attribute>
* </attributes>
* <sku>{Configurable sku pattern with %s}</sku>
* <products>{Amount of configurable products}</products>
* </config>
* </configurable_products>
*
* Products will be uniformly distributed per categories and websites
* If node "assign_entities_to_all_websites" from profile is set to "1" then products will be assigned to all websites
*
Expand Down Expand Up @@ -392,31 +411,16 @@ public function introduceParamLabels()
*/
public function printInfo(OutputInterface $output)
{
$config = $this->fixtureModel->getValue('configurable_products', []);
if (!$config) {
if (!$this->fixtureModel->getValue('configurable_products', [])) {
return;
}

$generalAmount = is_numeric($config) ? $config : array_sum(array_column($config, 'products'));
$output->writeln(sprintf('<info> |- Configurable products: %s</info>', $generalAmount));

$configurableProductConfig = $this->prepareConfigurableConfig(
$this->getDefaultAttributeSetsWithAttributes()
);
$generalAmount = array_sum(array_column($configurableProductConfig, 'products'));

foreach ($configurableProductConfig as $config) {
$attributeSetName = isset($config['attributeSet'])
? $config['attributeSet']
: 'Dynamic Attribute Set ' . $config['attributes'] . '-' . $config['options'];

$output->writeln(
sprintf(
'<info> |--- %s products for attribute set "%s"</info>',
$config['products'],
$attributeSetName
)
);
}
$output->writeln(sprintf('<info> |- Configurable products: %s</info>', $generalAmount));
}

/**
Expand Down Expand Up @@ -477,6 +481,7 @@ function (&$item, $key) {
*
* @return array
* @throws ValidatorException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function getConfigurableProductConfig()
{
Expand All @@ -500,14 +505,19 @@ private function getConfigurableProductConfig()
$skuPull = [];
foreach ($configurableProductConfig as $i => &$config) {
$attributeSet = $config['attributeSet'];
$attributes = (int)$config['attributes'];
$attributes = $config['attributes'];
$options = (int)$config['options'];
if ($attributeSet && isset($defaultAttributeSets[$attributeSet])) {
// process default attribute sets
$attributeSet = $defaultAttributeSets[$attributeSet];
$attributes = count($attributeSet['attributes']);
$options = count($attributeSet['attributes'][0]['values']);
} elseif (is_array($attributes)) {
$attributeSet = $this->getCustomAttributeSet($attributes);
$options = array_column($attributes, 'options');
$attributes = count($attributes);
} elseif ($attributes && $options) {
$attributes = (int)$attributes;
// process dynamic attribute sets
$attributeSet = $this->getAttributeSet($attributes, $options, $config['swatches']);
}
Expand All @@ -522,7 +532,7 @@ private function getConfigurableProductConfig()
$config['attributeSet'] = $this->convertAttributesToDBFormat($attributeSet);
$config['attributes'] = $attributes;
$config['options'] = $options;
$config['variationCount'] = pow($options, $attributes);
$config['variationCount'] = is_array($options) ? array_product($options) : pow($options, $attributes);
$skuPull[] = $config['sku'];
}

Expand Down Expand Up @@ -556,6 +566,16 @@ private function prepareConfigurableConfig($defaultAttributeSets)
} else {
$configurableConfigs = $configurableConfigs['config'];
}

foreach ($configurableConfigs as &$config) {
if (isset($config['attributes']) && is_array($config['attributes'])) {
if (!isset($config['attributes']['attribute'][0])) {
$config['attributes'] = [$config['attributes']['attribute']];
} else {
$config['attributes'] = $config['attributes']['attribute'];
}
}
}
} else {
throw new ValidatorException(__('Configurable product config is invalid'));
}
Expand Down Expand Up @@ -613,11 +633,10 @@ private function getConfigurableSkuPattern($config, $attributeSetName)
private function getAttributeSet($attributes, $options, $swatches)
{
$attributeCode = 'configurable_attribute' . $attributes . '_' . $options . '_';
$setName = 'Dynamic Attribute Set ' . $attributes . '-' . $options;

return $this->attributeSetsFixture->createAttributeSet(
$this->attributePattern->generateAttributeSet(
$setName,
$this->getAttributeSetName($attributes, $options),
$attributes,
$options,
function ($index, $attribute) use ($attributeCode, $options, $swatches) {
Expand All @@ -635,6 +654,68 @@ function ($index, $attribute) use ($attributeCode, $options, $swatches) {
);
}

/**
* Provide attribute set based on attributes configuration
*
* @param array $attributes
* @return array
*/
private function getCustomAttributeSet(array $attributes)
{
$attributeSetName = $this->getAttributeSetName(
count($attributes),
implode(',', array_column($attributes, 'options'))
);

$pattern = $this->attributePattern->generateAttributeSet(
$attributeSetName,
count($attributes),
array_column($attributes, 'options'),
function ($index, $attribute) use ($attributeSetName, $attributes) {
$swatch = [];
$timeStamp = time();
$data = [
'attribute_code' => sprintf('custom_attribute_%s_%s', $index, $timeStamp),
'frontend_label' => 'Dynamic Attribute ' . sprintf('custom_attribute_%s_%s', $index, $timeStamp),
];

if (isset($attributes[$index-1]['swatches'])) {
$data['is_visible_in_advanced_search'] = 1;
$data['is_searchable'] = 1;
$data['is_filterable'] = 1;
$data['is_filterable_in_search'] = 1;
$data['used_in_product_listing'] = 1;

$swatch = $this->swatchesGenerator->generateSwatchData(
(int) $attributes[$index-1]['options'],
$attributeSetName . $index,
$attributes[$index-1]['swatches']
);
}

return array_replace_recursive(
$attribute,
$data,
$swatch
);
}
);

return $this->attributeSetsFixture->createAttributeSet($pattern);
}

/**
* Provide attribute set name based on amount of attributes and options per attribute set
*
* @param int $attributesCount
* @param int $optionsCount
* @return string
*/
private function getAttributeSetName($attributesCount, $optionsCount)
{
return sprintf('Dynamic Attribute Set %s-%s', $attributesCount, $optionsCount);
}

/**
* @return array
*/
Expand Down Expand Up @@ -689,13 +770,13 @@ private function getConfigurableProductsVariationsValue()
*/
private function getAdditionalAttributesClosure(array $attributes, $variationCount)
{
return function ($attributeSetId, $index, $entityNumber) use ($attributes, $variationCount) {
$variationIndex = $this->getConfigurableVariationIndex($entityNumber, $variationCount) - 1;
$attributeValues = [];
$optionsPerAttribute = count($attributes[0]['values']);
$variationIndex = $variationIndex % $variationCount;
$optionsPerAttribute = array_map(function ($attr) {
return count($attr['values']);
}, $attributes);
$variationsMatrix = $this->generateVariationsMatrix(count($attributes), $optionsPerAttribute);

$variationsMatrix = $this->generateVariationsMatrix(count($attributes), $optionsPerAttribute);
return function ($attributeSetId, $index, $entityNumber) use ($attributes, $variationCount, $variationsMatrix) {
$variationIndex = $this->getConfigurableVariationIndex($entityNumber, $variationCount) - 1;
if (isset($variationsMatrix[$variationIndex])) {
$tempProductData = [];
foreach ($variationsMatrix[$variationIndex] as $attributeIndex => $optionIndex) {
Expand All @@ -706,7 +787,7 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
return $tempProductData;
}

return $attributeValues;
return [];
};
}

Expand All @@ -719,8 +800,8 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
private function generateVariationsMatrix($attributesPerSet, $optionsPerAttribute)
{
$variationsMatrix = null;
for ($i = 1; $i <= $attributesPerSet; $i++) {
$variationsMatrix[] = range(0, $optionsPerAttribute - 1);
for ($i = 0; $i < $attributesPerSet; $i++) {
$variationsMatrix[] = range(0, $optionsPerAttribute[$i] - 1);
}
return $this->generateVariations($variationsMatrix);
}
Expand Down
35 changes: 29 additions & 6 deletions setup/src/Magento/Setup/Model/FixtureGenerator/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Setup\Model\FixtureGenerator;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\ValidatorException;

/**
Expand Down Expand Up @@ -138,14 +139,25 @@ public function __construct(
* @param TemplateEntityGeneratorInterface $entityGenerator
* @param int $entitiesAmount
* @param callable $fixture
* @throws LocalizedException
* @return void
*/
public function generate(TemplateEntityGeneratorInterface $entityGenerator, $entitiesAmount, callable $fixture)
{
$this->sqlCollector->enable();
$entity = $entityGenerator->generateEntity();
$this->sqlCollector->disable();
$entity->delete();
$this->getConnection()->beginTransaction();
try {
$this->sqlCollector->enable();
$entity = $entityGenerator->generateEntity();
$this->sqlCollector->disable();
$entity->delete();
$this->getConnection()->commit();
} catch (\Exception $e) {
$this->getConnection()->rollBack();
throw new LocalizedException(
__('Cannot generate entities - error occurred during template creation: %1', $e->getMessage()),
$e
);
}

$map = [];
$processed = 0;
Expand Down Expand Up @@ -239,11 +251,22 @@ private function bindWithCustomHandler($table, $entityId, $entityNumber, $fixtur
*
* @param array $map
* @return void
* @throws LocalizedException
*/
private function saveEntities(array &$map)
{
foreach ($map as $table => $data) {
$this->getConnection()->insertMultiple($table, $data);
$this->getConnection()->beginTransaction();
try {
foreach ($map as $table => $data) {
$this->getConnection()->insertMultiple($table, $data);
}
$this->getConnection()->commit();
} catch (\Exception $e) {
$this->getConnection()->rollBack();
throw new LocalizedException(
__('Cannot save entity. Error occurred: %1', $e->getMessage()),
$e
);
}

$map = [];
Expand Down

0 comments on commit 33261e7

Please sign in to comment.