define('HASHMARK_CREATESAMPLES_STARTDATE', '2009-01-01 00:00:00 UTC'); define('HASHMARK_CREATESAMPLES_ENDDATE', '2009-01-01 23:59:59 UTC'); $db = Hashmark::getModule('DbHelper')->openDb('unittest'); $core = Hashmark::getModule('Core', '', $db); $partition = $core->getModule('Partition'); $rndSampleTime = 0; $createScalarTime = 0; $createSampleTime = 0; $totalSampleCnt = 0; $startDatetime = gmdate(HASHMARK_DATETIME_FORMAT); for ($scalars = 0; $scalars < HASHMARK_CREATESAMPLES_SCALARS; $scalars++) { $start = microtime(true); $samples = hashmark_random_samples(HASHMARK_CREATESAMPLES_TYPE, HASHMARK_CREATESAMPLES_STARTDATE, HASHMARK_CREATESAMPLES_ENDDATE, HASHMARK_CREATESAMPLES_COUNT); $end = microtime(true); $rndSampleTime += $end - $start; $scalarFields = array('type' => HASHMARK_CREATESAMPLES_TYPE, 'name' => Hashmark_Util::randomSha1()); $start = microtime(true); $scalarId = $core->createScalar($scalarFields); $end = microtime(true); $createScalarTime += $end - $start; $start = microtime(true); $end = microtime(true); $sampleCnt = count($samples); $start = microtime(true); foreach ($samples as $timeData => $value) { list($time) = explode('=', $timeData); $partition->createSample($scalarId, $value, $time); } $end = microtime(true); $createSampleTime += $end - $start; $totalSampleCnt += $sampleCnt;
/** * Return a random string. * * @param int $minLength 1 to 40. * @param int $maxLength 1 to 40. * @return string * @throws Exception If $minLength or $maxLength is greater than 40 or negative. */ public static function randomString($minLength = 30, $maxLength = 30) { $str = Hashmark_Util::randomSha1(); if ($maxLength > 0 && $maxLength < 41 && $minLength > 0 && $minLength <= $maxLength) { return substr($str, 0, mt_rand($minLength, $maxLength)); } throw new Exception('Random string limits are invalid.', HASHMARK_EXCEPTION_VALIDATION); }
/** * Return a value key key based on a group name. * * @param string $group Group name. * @param boolean $new If true, a new key is created. * @return string Active group key; false on error. */ protected function _getGroupKey($group, $new = false) { $groupKeyKey = 'Hashmark' . $group; if ($new) { $groupKeyValue = Hashmark_Util::randomSha1(); if ($this->save($groupKeyValue, $groupKeyKey)) { return $groupKeyValue; } else { return false; } } return $this->load($groupKeyKey); }
define('HASHMARK_DUMP_RANDOMSAMPLES_SCALARS', 1); define('HASHMARK_DUMP_RANDOMSAMPLES_COUNT', 100000); define('HASHMARK_DUMP_RANDOMSAMPLES_RANDOM_SET_MAX', 50000); define('HASHMARK_DUMP_RANDOMSAMPLES_SQL_BUFFER_MAX', 10000); define('HASHMARK_DUMP_RANDOMSAMPLES_STARTDATE', '2008-01-01 00:00:00 UTC'); define('HASHMARK_DUMP_RANDOMSAMPLES_ENDDATE', '2009-01-01 23:59:59 UTC'); $db = Hashmark::getModule('DbHelper')->openDb('unittest'); $partition = Hashmark::getModule('Partition', '', $db); // $tableDef is used for all sample CREATE TABLE statements. $baseTableDef = $partition->getPartitionDefinition(HASHMARK_DUMP_RANDOMSAMPLES_TYPE); $tableDef = preg_replace('/\\n|CREATE TABLE `samples_' . HASHMARK_DUMP_RANDOMSAMPLES_TYPE . '`/', '', $baseTableDef); // We will be incrementing manually. $info = $partition->getTableInfo('scalars'); $scalarId = $info['AUTO_INCREMENT']; for ($scalars = 0; $scalars < HASHMARK_DUMP_RANDOMSAMPLES_SCALARS; $scalars++) { $scalarFields = array('type' => HASHMARK_DUMP_RANDOMSAMPLES_TYPE, 'name' => Hashmark_Util::randomSha1()); $sql = 'INSERT IGNORE INTO `scalars` ' . '(`id`, `type`) ' . 'VALUES (' . $scalarId++ . ', \'' . HASHMARK_DUMP_RANDOMSAMPLES_TYPE . "');\n"; file_put_contents(HASHMARK_DUMP_RANDOMSAMPLES_FILE, $sql); $scalarSampleCnt = array(); $createdTables = array(); // Chunk random samples into sets to avoid memory limit. $scalarSampleCnt = 0; while ($scalarSampleCnt < HASHMARK_DUMP_RANDOMSAMPLES_COUNT) { // Last parameter will sort $samples by date ascending. $samples = hashmark_random_samples(HASHMARK_DUMP_RANDOMSAMPLES_TYPE, HASHMARK_DUMP_RANDOMSAMPLES_STARTDATE, HASHMARK_DUMP_RANDOMSAMPLES_ENDDATE, min(HASHMARK_DUMP_RANDOMSAMPLES_RANDOM_SET_MAX, HASHMARK_DUMP_RANDOMSAMPLES_COUNT - $scalarSampleCnt), false, false, null, null, true); $scalarSampleCnt += count($samples); $buffer = ''; $bufferSize = 0; foreach ($samples as $timeData => $value) { list($time) = explode('=', $timeData); $sampleDate = Hashmark_Util::toDatetime($time);
/** * Create a temporary table from a SELECT statement and existing definition. * * @param string $src Name of table providing a definition. * @param string $columns Columns populated by $sql, ex.: `x`, `y` * @param string $selectSql Pre-escaped statement. * @param Array $values Hashmark_Module_DbDependent::expandSql() compatible macros. * * If $selectSql uses the ~samples macro, these * values are required: :scalarId, :start, :end * @return string Destination temporary table name. */ public function createTempFromQuery($src, $columns, $selectSql, $values = array(), $scalarId = '', $start = '', $end = '') { $name = 'samples_tmp_' . Hashmark_Util::randomSha1(); $createSql = "CREATE TEMPORARY TABLE IF NOT EXISTS {$this->_dbName}`{$name}` LIKE `{$src}`"; $this->_db->query($createSql); $insertSql = "INSERT INTO {$this->_dbName}`{$name}` ({$columns}) {$selectSql}"; if (false === strpos($insertSql, '~samples')) { $this->_db->query($insertSql, $values); } else { $this->queryInRange($scalarId, $start, $end, $insertSql, $values); } return $name; }