1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Includes/Generate.php. |
4
|
|
|
* |
5
|
|
|
* @author Adam "Saibamen" Stachowicz <[email protected]> |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/Saibamen/Generate-Sort-Numbers |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Includes; |
12
|
|
|
|
13
|
|
|
require_once 'Text.php'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Functions for 'Generate' script. |
17
|
|
|
*/ |
18
|
|
|
class Generate |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var bool If true - print less information for eg. Travis or CircleCI to avoid big log file. |
22
|
|
|
*/ |
23
|
|
|
private static $testingMode = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generate string with randomized numbers. |
27
|
|
|
* |
28
|
|
|
* @param int|float $min Minimum allowed number to generate |
29
|
|
|
* @param int|float $max Maximum allowed number to generate |
30
|
|
|
* @param int|float $decimalPlaces Number of decimal places |
31
|
|
|
* @param int $maxFileSize Maximum file size in bytes |
32
|
|
|
* @param string $filename Output filename without extension |
33
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
34
|
|
|
* @param string|mixed $delimiter Delimiter. Default is ' ' |
35
|
|
|
* |
36
|
|
|
* @see Generate::getMinMaxNumberSize() |
37
|
|
|
* @see File::createMissingDirectory() |
38
|
|
|
* @see File::checkIfFileExists() |
39
|
|
|
*/ |
40
|
|
|
public static function generateAndSaveRandomNumbers($min, $max, $decimalPlaces, $maxFileSize, $filename, $fileExtension = '.dat', $delimiter = ' ') |
41
|
|
|
{ |
42
|
|
|
$range = $max - $min; |
43
|
|
|
|
44
|
|
|
$minMaxNumberSize = self::getMinMaxNumberSize($min, $max, $decimalPlaces); |
45
|
|
|
|
46
|
|
|
if ($minMaxNumberSize['max'] > $maxFileSize) { |
47
|
|
|
die('Error: Cannot generate any number due to too low file size.'); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
File::createMissingDirectory($filename); |
51
|
|
|
File::checkIfFileExistsAndDeleteContent($filename, $fileExtension); |
52
|
|
|
|
53
|
|
|
// Maximum iteration without delimiter |
54
|
|
|
$maxIteration = (int) ($maxFileSize / $minMaxNumberSize['max']); |
55
|
|
|
Text::debug('First maximum iteration: '.$maxIteration); |
56
|
|
|
|
57
|
|
|
$findingStart = microtime(true); |
58
|
|
|
Text::message('Finding maximum iteration for loop...'); |
59
|
|
|
|
60
|
|
|
for ($i = 0; ; $i++) { |
61
|
|
|
$tempMaxIteration = $maxIteration - $i; |
62
|
|
|
$maximumBytes = ($minMaxNumberSize['max'] * $tempMaxIteration) + $tempMaxIteration - strlen($delimiter); |
63
|
|
|
|
64
|
|
|
if ($maxFileSize >= $maximumBytes) { |
65
|
|
|
$maxIteration = $tempMaxIteration; |
66
|
|
|
unset($tempMaxIteration); |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
Text::printTimeDuration($findingStart); |
72
|
|
|
unset($findingStart); |
73
|
|
|
|
74
|
|
|
Text::message('GENERATING...'); |
75
|
|
|
Text::debug('Min: '.$min.' Max: '.$max.' Decimal places: '.$decimalPlaces.' Size: '.$maxFileSize."\nMaximum iteration: ".$maxIteration."\n"); |
76
|
|
|
|
77
|
|
|
$generateStart = microtime(true); |
78
|
|
|
|
79
|
|
|
$file = fopen($filename.$fileExtension, 'w'); |
80
|
|
|
$chunkSize = 20; |
81
|
|
|
$maxMainIteration = ceil((int) ($maxIteration / $chunkSize)); |
82
|
|
|
$outputString = null; |
83
|
|
|
$progress = 1; |
84
|
|
|
|
85
|
|
|
for ($i = 0; $i <= $maxMainIteration; $i++) { |
86
|
|
|
for ($j = 1; $j <= $chunkSize; $j++, $progress++) { |
87
|
|
|
if ($progress > $maxIteration) { |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!self::getTesting()) { |
92
|
|
|
// Print progress and move cursor back to position 0 |
93
|
|
|
echo 'Progress: '.$progress.'/'.$maxIteration."\r"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Random number |
97
|
|
|
$number = $min + $range * (mt_rand() / mt_getrandmax()); |
98
|
|
|
// Format with trailing zeros ie. 8.00 |
99
|
|
|
$number = number_format((float) $number, (int) $decimalPlaces, '.', ''); |
100
|
|
|
|
101
|
|
|
$outputString .= $number.$delimiter; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Remove last delimiter |
105
|
|
|
if ($progress > $maxIteration) { |
106
|
|
|
$outputString = rtrim($outputString, $delimiter); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
fwrite($file, $outputString); |
|
|
|
|
110
|
|
|
$outputString = null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
fclose($file); |
114
|
|
|
|
115
|
|
|
Text::printTimeDuration($generateStart); |
116
|
|
|
Text::message('Output file '.$filename.$fileExtension.' generated with '.filesize($filename.$fileExtension).' bytes.'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Switch min and max if min > max. |
121
|
|
|
* |
122
|
|
|
* @param int|float $min Minimum allowed number to generate |
123
|
|
|
* @param int|float $max Maximum allowed number to generate |
124
|
|
|
* @param int|float $decimalPlaces Number of decimal places |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
private static function getMinMaxNumberSize($min, $max, $decimalPlaces) |
129
|
|
|
{ |
130
|
|
|
$additionalBytes = null; |
131
|
|
|
|
132
|
|
|
if ((int) $decimalPlaces > 0) { |
133
|
|
|
// + 1 for decimal point |
134
|
|
|
$additionalBytes = (int) $decimalPlaces + 1; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$minimumNumberSize = min(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
138
|
|
|
$maximumNumberSize = max(strlen((int) $min), strlen((int) $max)) + $additionalBytes; |
139
|
|
|
|
140
|
|
|
Text::debug('Minimum number size: '.$minimumNumberSize); |
141
|
|
|
Text::debug('Maximum number size: '.$maximumNumberSize); |
142
|
|
|
|
143
|
|
|
return array('min' => $minimumNumberSize, 'max' => $maximumNumberSize); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Switch min and max if min > max. |
148
|
|
|
* |
149
|
|
|
* @param float $min Minimum |
150
|
|
|
* @param float $max Maximum |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public static function checkSwitchMinMax($min, $max) |
155
|
|
|
{ |
156
|
|
|
if ($min > $max) { |
157
|
|
|
Text::debug('!! Switching min and max !!'); |
158
|
|
|
|
159
|
|
|
$tempMin = $min; |
160
|
|
|
|
161
|
|
|
$min = $max; |
162
|
|
|
$max = $tempMin; |
163
|
|
|
|
164
|
|
|
Text::debug('Min: '.$min); |
165
|
|
|
Text::debug('Max: '.$max); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return array('min' => $min, 'max' => $max); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set $testingMode property value. |
173
|
|
|
* |
174
|
|
|
* @param bool $value |
175
|
|
|
* |
176
|
|
|
* @see Generate::$testingMode |
177
|
|
|
*/ |
178
|
|
|
public static function setTesting($value) |
179
|
|
|
{ |
180
|
|
|
self::$testingMode = $value; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get $testingMode property value. |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
* |
188
|
|
|
* @see Generate::$testingMode |
189
|
|
|
*/ |
190
|
|
|
public static function getTesting() |
191
|
|
|
{ |
192
|
|
|
return self::$testingMode; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.