Skip to content

Instantly share code, notes, and snippets.

@lancegliser
Created February 24, 2017 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancegliser/9646364169ed1393151e043015dfb02e to your computer and use it in GitHub Desktop.
Save lancegliser/9646364169ed1393151e043015dfb02e to your computer and use it in GitHub Desktop.
Runs a given batch operation as many times as possible in a given time span adjusting for execution rate dynamically
<?php
/**
* This controller will run up to the alloted seconds fitting as many operations as possible
* @param int $seconds
* @return array
*/
public function bulk_operation($seconds = NULL, $identifier)
{
$execution_time_limit = $seconds != NULL? $seconds : ini_get('max_execution_time');
if( !empty($identifier) ){
$ids = array($identifier);
$this->_process_batch();
return $ids;
} else {
$ids = array();
// Run batches against the average execution time that day
// The system sometimes tends to speed up after the initial batch
// Timings as of script creation
// Initial average response time: x
// Hot average response time: y
$execution_time_total = 0;
$batch_size = 5;
$estimated_execution_average = 1.5;
while($batch_size > 0){
$time_start = microtime(true);
$batch = $this->_get_ids($batch_size);
$ids = array_merge($ids, $batch);
$this->_process_batch($batch);
$time_end = microtime(true);
// Determine the optimal size of the next batch
$execution_time = $time_end - $time_start;
$estimated_execution_average = $execution_time / $batch_size;
$execution_time_total += $execution_time;
// Calculate a new batch size
// If we allow a batch to be too large, we might get inaccurate average execution times deviations
// Don't race too close to our limit, the last thing we want is failed executions.
$batch_size = min(20, floor( ($execution_time_limit - $execution_time_total ) / $estimated_execution_average * .8 ));
}
}
return $ids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment