-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateCsv.php
125 lines (104 loc) · 4.82 KB
/
CreateCsv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/
declare(strict_types=1);
namespace JBZoo\PHPUnit\Benchmarks\Commands;
use Faker\Factory;
use JBZoo\Cli\CliCommand;
use JBZoo\CsvBlueprint\Utils;
use League\Csv\Writer;
use Symfony\Component\Console\Input\InputOption;
/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class CreateCsv extends CliCommand
{
protected function configure(): void
{
$this
->setName('create:csv')
->setDescription('Create CSV file with random data based on PHP Faker')
->addOption('rows', 'R', InputOption::VALUE_REQUIRED, 'Number of rows', 1_000)
->addOption('add-header', 'H', InputOption::VALUE_NONE, 'Add header row')
->addOption('columns', 'C', InputOption::VALUE_REQUIRED, 'Columns', 0);
parent::configure();
}
protected function executeAction(): int
{
$addHeader = $this->getOptBool('add-header');
$rows = $this->getOptInt('rows');
$columns = $this->getOptInt('columns');
$outputFile = $this->getFilename();
$writer = Writer::createFromPath($outputFile, 'w+');
if ($addHeader) {
$writer->insertOne(\array_keys($this->getDatasetRow($columns)));
if ($rows === 0) {
$this->_('Only header created: ' . Utils::printFile($outputFile));
return self::SUCCESS;
}
}
foreach (\range(0, $rows - 1) as $index) {
$writer->insertOne($this->getDatasetRow($columns, $index + 1));
}
$this->_('File created: ' . Utils::printFile($outputFile));
return self::SUCCESS;
}
private function getDatasetRow(int $dataset, int $i = 0): array
{
$faker = Factory::create();
$data = [
// Tear 1: Small
'id' => static fn () => $i, // 1
'bool_int' => static fn () => \random_int(0, 1), // 2
'bool_str' => static fn () => \random_int(0, 1) === 1 ? 'true' : 'false', // 3
'number' => static fn () => \random_int(0, 1_000_000), // 4
'float' => static fn () => \random_int(0, 10_000_000) / 7, // 5
// Tear 2: Medium
'date' => static fn () => $faker->date(), // 6
'datetime' => static fn () => $faker->date('Y-m-d H:i:s'), // 7
'domain' => static fn () => $faker->domainName(), // 8
'email' => static fn () => $faker->email(), // 9
'ip4' => static fn () => $faker->ipv4(), // 10
// Tear 3: Large
'uuid' => static fn () => $faker->uuid(), // 11
'address' => static fn () => \str_replace("\n", '; ', $faker->address()), // 12
'postcode' => static fn () => $faker->postcode(), // 13
'latitude' => static fn () => $faker->latitude(), // 14
'longitude' => static fn () => $faker->longitude(), // 15
// Tear 4: Huge
'ip6' => static fn () => $faker->ipv6(), // 16
'sentence_tiny' => static fn () => $faker->sentence(3), // 17
'sentence_small' => static fn () => $faker->sentence(6), // 18
'sentence_medium' => static fn () => $faker->sentence(10), // 19
'sentence_huge' => static fn () => $faker->sentence(30), // 20
];
$firstN = $data;
if ($dataset > 0) {
$firstN = \array_slice($data, 0, $dataset, true);
}
return \array_map(static fn ($fn) => $fn(), $firstN);
}
private function getFilename(): string
{
$addHeader = $this->getOptBool('add-header');
$rows = $this->getOptInt('rows');
$columns = $this->getOptInt('columns');
if ($rows === 0) {
return $addHeader
? PATH_ROOT . "/build/bench/{$columns}_header.csv"
: PATH_ROOT . "/build/bench/{$columns}.csv";
}
return $addHeader
? PATH_ROOT . "/build/bench/{$columns}_{$rows}_header.csv"
: PATH_ROOT . "/build/bench/{$columns}_{$rows}.csv";
}
}