Skip to content

Instantly share code, notes, and snippets.

@AaronSchulz
Last active April 25, 2022 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 AaronSchulz/28a2cc7701a33adca1479b5ff6530b2c to your computer and use it in GitHub Desktop.
Save AaronSchulz/28a2cc7701a33adca1479b5ff6530b2c to your computer and use it in GitHub Desktop.
<?php
testAPCUMixedReadsAndWrites();
function testAPCUMixedReadsAndWrites() {
$xhprof_shutdown_fn = null;
if ( ( $_GET['profile'] ?? null ) === '1' ) {
if ( function_exists( 'tideways_xhprof_enable' ) ) {
tideways_xhprof_enable( TIDEWAYS_XHPROF_FLAGS_CPU );
$xhprof_shutdown_fn = 'tideways_xhprof_disable';
} elseif ( function_exists( 'tideways_enable' ) ) {
tideways_enable( TIDEWAYS_XHPROF_FLAGS_CPU );
$xhprof_shutdown_fn = 'tideways_disable';
} elseif ( function_exists( 'xhprof_enable' ) ) {
xhprof_enable( XHPROF_FLAGS_CPU );
$xhprof_shutdown_fn = 'xhprof_disable';
}
}
$overwrite = !empty( $_GET['overwrite'] );
$minBytes = intval( $_GET['minSize'] ?? 128 );
$maxBytes = intval( $_GET['maxSize'] ?? 1024 * 1024 );
$keysCount = intval( $_GET['keysCount'] ?? 1000 );
$keysPerRequest = intval( $_GET['keysPerRequest'] ?? $keysCount );
$iterations = intval( $_GET['iterations'] ?? 1 );
$ttl = intval( $_GET['ttl'] ?? 3600 );
$referenceValue = null;
$storeOK = 0;
$storeFail = 0;
foreach ( (array)array_rand( range( 1, $keysCount ), $keysPerRequest ) as $keyIndex ) {
for ( $i = 0; $i < $iterations; ++$i ) {
$copyKey = "global:testblobs:$keyIndex:$minBytes:$maxBytes";
$copyValue = apcu_fetch( $copyKey );
if ( $copyValue === false || $overwrite ) {
$referenceValue = $referenceValue
?? str_repeat( hash('sha256', microtime() ), (int)ceil( $maxBytes / 64 ) );
$copyValue = substr( $referenceValue, 0, mt_rand( $minBytes, $maxBytes ) );
$ok = apcu_store( $copyKey, $copyValue, $ttl );
if ( $ok ) {
++$storeOK;
} else {
++$storeFail;
}
}
}
}
header( 'Content-Type: text/javascript' );
$data = [ 'store_ok' => $storeOK, 'store_fail' => $storeFail ];
if ( $xhprof_shutdown_fn !== null ) {
$profile = $xhprof_shutdown_fn();
uasort(
$profile,
function ( $a, $b ) {
return $b['wt'] <=> $a['wt'];
}
);
$data += [ 'profile' => $profile ];
}
echo json_encode( $data, JSON_PRETTY_PRINT );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment