Skip to content

Instantly share code, notes, and snippets.

@michael-grunder
Created June 9, 2014 15:55
Show Gist options
  • Save michael-grunder/4e315422d276f6701aaa to your computer and use it in GitHub Desktop.
Save michael-grunder/4e315422d276f6701aaa to your computer and use it in GitHub Desktop.
<?php
ini_set('memory_limit','4G');
$arr_args = getopt('', Array('file:', 'port:', 'step:', 'runs', 'name:'));
if(!isset($arr_args['file'])) {
die("Please pass an input filename of sets!\n");
}
if(!isset($arr_args['port'])) {
die("Pass a port where redis-server is running!\n");
}
$str_file = $arr_args['file'];
$i_port = (int)$arr_args['port'];
$i_step = isset($arr_args['step']) ? $arr_args['step'] : 10000;
$i_runs = isset($arr_args['runs']) ? $arr_args['runs'] : 5;
$str_name = isset($arr_args['name']) ? $arr_args['name'] : NULL;
$obj_r = new Redis();
if(!$obj_r->connect('127.0.0.1', $i_port)) {
echo "Can't communicate with Redis on port $i_port!\n";
exit(1);
}
if(!isset($arr_args['name'])) {
echo "Give this run a name!\n";
exit(1);
}
if(!file_exists($str_file)) {
echo "Sets file '$str_file' not found!\n";
exit(1);
}
// Headers
fputcsv(STDOUT,Array('sets','incard','outcard',$str_name));
// The sets in question
$arr_sets = explode("\n", file_get_contents($str_file));
// Start with stepping, increase the set count each time
for($i=$i_step; $i<count($arr_sets);$i+=$i_step) {
// Grab a slice of N sets
$arr_run_sets = array_slice($arr_sets, 0, $i);
// Determine input cardinality
$obj_r->pipeline();
foreach($arr_run_sets as $str_set) {
$obj_r->zcard($str_set);
}
$i_in_card = array_sum($obj_r->exec());
// Total timing for this run
$i_tot = 0;
// Run "runs" time
for($j=0;$j<$i_runs;$j++) {
$obj_r->del('dest_set');
$st = microtime(true);
$i_out_card = $obj_r->zunionstore('dest_set', $arr_run_sets);
$et = microtime(true);
$i_tot += $et-$st;
}
// Average the runs
$f_timing = round($i_tot/$i_runs, 4);
fputcsv(STDOUT, Array($i, $i_in_card, $i_out_card, $f_timing));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment