Created
October 11, 2015 15:05
-
-
Save joegreen88/be9b34f02b799937c6ce to your computer and use it in GitHub Desktop.
Profiling the script to try and find out where the memory is going
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$columnHeadings = $columnHeadings + ['mem_row_start','delta_mem_row_fetched','delta_mem_row_composed','ref_bucket']; | |
$outStream = fopen("php://output", "r+"); | |
fputcsv($outStream, $columnHeadings); | |
// Create a loop so that we can do paginated couchbase view queries | |
$finished = false; | |
$startKeyDocId = null; | |
$perPage = 5; | |
while (!$finished) { | |
// Set up the couchbase view query | |
$query = \CouchbaseViewQuery::from('webforms', $cbView); | |
$query | |
->order($query::ORDER_ASCENDING) | |
->stale((null === $startKeyDocId ? $query::UPDATE_BEFORE : $query::UPDATE_NONE)) | |
->reduce(true) | |
->group(3) | |
->range( | |
[$webForm->getType(), $ID, $startKeyDocId], | |
[$webForm->getType(), $ID+1, null], | |
false | |
) | |
->limit($perPage + 1) | |
; | |
// Fetch query results | |
$result = $bucket->query($query); | |
$countRows = count($result['rows']); | |
// No results means we are done here | |
if (0 === $countRows) { | |
break; | |
} | |
// We can try to calculate if this is the last page based on the number of results | |
if ($countRows <= $perPage) { | |
$finished = true; | |
} | |
// Loop through rows in this paginated result set | |
$numLoops = min($countRows, $perPage); | |
for ($i = 0; $i < $numLoops; $i++) { | |
$memory['mem_row_start'] = memory_get_usage(); | |
$entryResult = $bucket->get($result['rows'][$i]['key'][2]); | |
$doc = $entryResult->value; | |
$memory['mem_row_fetched'] = memory_get_usage(); | |
$memory['delta_mem_row_fetched'] = $memory['mem_row_fetched'] - $memory['mem_row_start']; | |
// ... | |
// here I process the data from $doc into an array $data for csv export | |
// ... | |
$memory['mem_row_composed'] = memory_get_usage(); | |
$memory['delta_mem_row_composed'] = $memory['mem_row_composed'] - $memory['mem_row_fetched']; | |
foreach ($memory as $k => $v) { | |
if (in_array($k, $columnHeadings)) { | |
$data[$k] = $v; | |
} | |
} | |
ob_start(); | |
debug_zval_dump($bucket); | |
$out = ob_get_clean(); | |
preg_match('/refcount\\((\\d+)\\)/', $out, $matches); | |
$data['ref_bucket'] = $matches[1]; | |
fputcsv($outStream, $data); | |
unset($doc); | |
unset($entryResult); | |
unset($row); | |
unset($key); | |
unset($data); | |
unset($out); | |
unset($memory); | |
} | |
// Limit is $perPage + 1 so on 0-indexed array a key of $perPage means we fulfilled that limit | |
if (array_key_exists($perPage, $result['rows'])) { | |
$startKeyDocId = $result['rows'][$perPage]['key'][2]; | |
} | |
flush(); | |
unset($query); | |
unset($result); | |
sleep(1); | |
} | |
fclose($outStream); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment