Exemplo n.º 1
0
 /**
  * @test
  * @group Util
  * @group convertsDataTypesToDatetimes
  * @group toDateTime
  */
 public function convertsDataTypesToDatetimes()
 {
     $expectedDatetime = '2009-01-31 15:03:56';
     $this->assertEquals($expectedDatetime, Hashmark_Util::toDateTime(1233414236));
     $this->assertEquals($expectedDatetime, Hashmark_Util::toDateTime('20090131150356'));
     $this->assertEquals($expectedDatetime, Hashmark_Util::toDateTime($expectedDatetime));
     $this->assertFalse(Hashmark_Util::toDateTime(-1));
     $this->assertFalse(Hashmark_Util::toDateTime('120090131150356'));
     $this->assertFalse(Hashmark_Util::toDateTime(''));
     $this->assertFalse(Hashmark_Util::toDateTime(false));
 }
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;
Exemplo n.º 3
0
 /**
  * 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);
 }
Exemplo n.º 4
0
 $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);
         if ('string' == HASHMARK_DUMP_RANDOMSAMPLES_TYPE) {
             $value = $partition->escape($value);
         }
         // Create partitions as needed based on sample date.
         $tableName = $partition->getIntervalTableName($scalarId, $sampleDate);
         if (!isset($createdTables[$tableName])) {
             $createdTables[$tableName] = 1;
             $buffer .= "CREATE TABLE IF NOT EXISTS `{$tableName}` {$tableDef} AUTO_INCREMENT=1;\n";
             $bufferSize++;
         }
         $buffer .= "INSERT INTO `{$tableName}` " . '(`value`, `end`) ' . "VALUES ('{$value}', '{$sampleDate}');\n";
         $bufferSize++;
         if ($bufferSize > HASHMARK_DUMP_RANDOMSAMPLES_SQL_BUFFER_MAX) {
             file_put_contents(HASHMARK_DUMP_RANDOMSAMPLES_FILE, $buffer, FILE_APPEND);
             $buffer = '';
Exemplo n.º 5
0
 /**
  * Update a milestone's fields.
  *
  * @param int       $id
  * @param string    $name
  * @param mixed     $when   UNIX timestamp or DATETIME string.
  * @return boolean  True on success.
  * @throws Exception On query error.
  */
 public function updateMilestone($id, $name, $when)
 {
     $when = Hashmark_Util::toDatetime($when);
     $sql = "UPDATE {$this->_dbName}`milestones` " . 'SET `name` = ?, ' . '`when` = ? ' . 'WHERE `id` = ?';
     $stmt = $this->_db->query($sql, array($name, $when, $id));
     return 1 == $stmt->rowCount();
 }
Exemplo n.º 6
0
 /**
  * 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);
 }
Exemplo n.º 7
0
 /**
  * Add new row to `samples` and update `scalars` statistics.
  *
  * @param int       $scalarId
  * @param string    $value
  * @param mixed     $end    UNIX timestamp or DATETIME string.
  * @return boolean  True on success.
  * @throws Exception On query error.
  */
 public function createSample($scalarId, $value, $end)
 {
     $end = Hashmark_Util::toDatetime($end);
     // `sample_count` seeds AUTO_INCREMENT `id` values in sample partitions
     $sql = "UPDATE {$this->_dbName}`scalars` " . 'SET `value` = ?, ' . '`last_agent_change` = ?, ' . '`sample_count` = `sample_count` + 1 ' . 'WHERE `id` = ?';
     $this->_db->query($sql, array($value, $end, $scalarId));
     $sql = 'INSERT INTO ~samples ' . '(`value`, `end`) ' . 'VALUES (?, ?)';
     // queryAtDate() instead of queryCurrent() so unit tests can
     // create backdated samples.
     $bind = array($value, $end);
     $stmt = $this->queryAtDate($scalarId, $sql, $end, $bind);
     return 1 == $stmt->rowCount();
 }