Refactor and optimizations
This commit is contained in:
@@ -2,27 +2,24 @@
|
||||
|
||||
namespace App\Services\IterationEventBuffer;
|
||||
|
||||
use App\Events\PerceptronTrainingIteration;
|
||||
|
||||
class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer
|
||||
{
|
||||
private $data;
|
||||
|
||||
private int $nextSizeIncreaseThreshold;
|
||||
|
||||
private int $underSizeIncreaseCount = 0;
|
||||
private array $data = [];
|
||||
|
||||
public function __construct(
|
||||
private string $sessionId,
|
||||
private string $trainingId,
|
||||
private int $sizeIncreaseStart = 10,
|
||||
private int $sizeIncreaseFactor = 2,
|
||||
) {
|
||||
$this->data = [];
|
||||
$this->nextSizeIncreaseThreshold = $sizeIncreaseStart;
|
||||
}
|
||||
) {}
|
||||
|
||||
public function flush(): void
|
||||
{
|
||||
event(new \App\Events\PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
|
||||
if ($this->data === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
event(new PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
@@ -35,27 +32,24 @@ class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer
|
||||
'weights' => $synaptic_weights,
|
||||
];
|
||||
|
||||
$payload = [
|
||||
'iterations' => [...$this->data, $iteration],
|
||||
'trainingId' => $this->trainingId,
|
||||
];
|
||||
|
||||
if ($this->data !== [] && strlen(json_encode($payload, JSON_THROW_ON_ERROR)) > config('broadcasting.broadcast_max_payload_size')) {
|
||||
$this->flush();
|
||||
}
|
||||
|
||||
$this->data[] = $iteration;
|
||||
|
||||
if ($this->underSizeIncreaseCount <= $this->sizeIncreaseStart) { // We can still send a single date because we are under the increase start threshold
|
||||
$this->underSizeIncreaseCount++;
|
||||
if ($this->data !== [] && $this->payloadExceedsLimit()) {
|
||||
$lastIteration = array_pop($this->data);
|
||||
$this->flush();
|
||||
} elseif (count($this->data) >= $this->nextSizeIncreaseThreshold) {
|
||||
$this->flush();
|
||||
$this->nextSizeIncreaseThreshold *= $this->sizeIncreaseFactor;
|
||||
$this->data[] = $lastIteration;
|
||||
}
|
||||
|
||||
if ($this->nextSizeIncreaseThreshold > config('perceptron.broadcast_iteration_size')) {
|
||||
$this->nextSizeIncreaseThreshold = config('perceptron.broadcast_iteration_size'); // Cap the threshold to the maximum size
|
||||
}
|
||||
if (count($this->data) >= config('perceptron.broadcast_iteration_size')) {
|
||||
$this->flush();
|
||||
}
|
||||
}
|
||||
|
||||
private function payloadExceedsLimit(): bool
|
||||
{
|
||||
return strlen(json_encode([
|
||||
'iterations' => $this->data,
|
||||
'trainingId' => $this->trainingId,
|
||||
], JSON_THROW_ON_ERROR)) > config('broadcasting.broadcast_max_payload_size');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user