File: D:/HostingSpaces/SBogers10/shop.komma.nl/tests/Benchmark/ProductsBenchmark.php
<?php
namespace Tests\Benchmark;
use Komma\KMS\Transfer\ColumnMap;
use Komma\KMS\QualityAssurance\BaseBenchMark;
use App\Categories\Models\Category;
use App\Products\Product\Product;
use App\Products\Product\Transfer\ProductColumnMapsTrait;
use App\Products\Product\Transfer\ProductCsvImportService;
use App\Tests\Traits\WithFaker;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ProductsBenchmark extends BaseBenchMark
{
use WithFaker,
DatabaseTransactions,
ProductColumnMapsTrait;
/*
|--------------------------------------------------------------------------
| Setup and teardown
|--------------------------------------------------------------------------
| We use setup and teardown methods to run respectively before and after
| each benchmark. The time they take is not taken into account in the benchmark.
|
*/
/**
* Setup the test environment before each benchmark
*
* @return void
*/
public function setUp()
{
parent::setUp();
}
/*
|--------------------------------------------------------------------------
| Benchmark subjects
|--------------------------------------------------------------------------
| The next methods are benchmark subjects. They test the performance of
| a part of the application. Various measures will be done while they run.
| The results of these measures are visible in console, or if configured in
| XML and HTML files for example. Their names must start with the subject
| pattern, defined in phpbench_config.json. Usually this means they must start with
| the word "bench"
|
*/
/**
* Benchmark how long it takes to create a product in the database in its simplest form, with the least amount of
* framework overhead as possible.
*
* @Revs({1, 10, 100})
* @Iterations(1)
* @Groups({"products"})
* @OutputTimeUnit("seconds")
*/
public function benchFastestProductCreation()
{
factory(Product::class)->create();
}
/**
* Benchmark how long it takes to import products
*
* @Revs({1, 10, 100})
* @Iterations(1)
* @Groups({"products"})
* @OutputTimeUnit("seconds")
* @throws \Exception
*/
public function benchProductImport()
{
$productData = $this->provideProductData();
/** @var ProductCsvImportService $productImportService */
$productImportService = app(ProductCsvImportService::class);
$columnMaps = $this->getColumnMaps();
$data = collect();
foreach ($columnMaps['productData'] as $columnMap) {
/** @var ColumnMap $columnMap */
$data->push($productData[$columnMap->getModelAttributeName()]);
}
$data->push($productData['quantity'].'/'.$productData['quantity_price']);
foreach ($columnMaps['translationData'] as $iso_2 => $columnMaps) {
foreach($columnMaps as $columnMap) {
$data->push($productData[$columnMap->getModelAttributeName().'-'.$iso_2]);
}
}
$productImportService->import($data->toArray(), 1);
}
/*
|--------------------------------------------------------------------------
| Helper methods
|--------------------------------------------------------------------------
| The next methods provide useful services to other methods. They can,
| for example provide the data for a benchmark subject.
|
*/
public function provideProductData()
{
$category = Category::inRandomOrder()->first();
return [
"category_id" => $category->id,
"active" => $this->faker->boolean,
"price" => $this->faker->randomNumber(4),
"stock_keeping_unit" => $this->faker->ean13,
"product_documents" => "",
"quantity_price" => $this->faker->randomNumber(2),
"quantity" => $this->faker->randomNumber(2),
"name-en" => $this->faker->productName(),
"name-de" => $this->faker->productName(),
"name-nl" => $this->faker->productName(),
"meta_description-en" => $this->faker->paragraph(1),
"meta_description-de" => $this->faker->paragraph(1),
"meta_description-nl" => $this->faker->paragraph(1),
"description-en" => $this->faker->paragraph(3),
"description-de" => $this->faker->paragraph(3),
"description-nl" => $this->faker->paragraph(3),
];
}
}