/** * Resources needed for most tests. * * @return void */ protected function setUp() { parent::setUp(); $this->_partition = Hashmark::getModule('Partition', '', $this->_db); $this->_core = Hashmark::getModule('Core', '', $this->_db); $this->_mergeTablePrefix = Hashmark::getConfig('Partition', '', 'mergetable_prefix'); }
/** * @test * @group Cron * @group runsAgents * @group runAgents */ public function runsAgents() { $core = Hashmark::getModule('Core', '', $this->_db); $scheduledScalarIds = array(); $expected = array(); $expected['name'] = self::randomString(); $expected['type'] = 'decimal'; $expected['value'] = 1234; $scalarId = $core->createScalar($expected); $agent = $core->getAgentByName('ScalarValue'); if ($agent) { $agentId = $agent['id']; } else { $agentId = $core->createAgent('ScalarValue'); } // Agent is scheduled to run immediately (0 frequency). $scalarAgentId = $core->createScalarAgent($scalarId, $agentId, 0, 'Scheduled'); ob_start(); require HASHMARK_ROOT_DIR . '/Cron/runAgents.php'; ob_end_clean(); // Assert scalar value changed in last 60 seconds. $actual = $core->getScalarById($scalarId); $this->assertEquals($expected['value'] + 5, (int) $actual['value']); // Assert scalar agent is ready for future run. $scalarAgent = $core->getScalarAgentById($scalarAgentId); $this->assertEquals('Scheduled', $scalarAgent['status']); $this->assertEquals('', $scalarAgent['error']); $this->assertLessThan(5, abs(strtotime($scalarAgent['lastrun'] . ' UTC') - time())); }
/** * @test * @group DbHelper * @group reusesDbConnection * @group reuseDb */ public function reusesDbConnection() { $config = Hashmark::getConfig('DbHelper'); $link = new mysqli($config['profile']['unittest']['params']['host'], $config['profile']['unittest']['params']['username'], $config['profile']['unittest']['params']['password'], $config['profile']['unittest']['params']['dbname'], $config['profile']['unittest']['params']['port']); $db = Hashmark::getModule('DbHelper')->reuseDb($link, 'Mysqli'); $this->assertEquals('mysqli', get_class($db->getConnection())); }
/** * @test * @group Hashmark * @group loadsExternalModuleType * @group getModule * @group getTypeConfig */ public function loadsExternalModuleType() { $inst = Hashmark::getModule('Test', 'FakeExternalType'); $this->assertTrue(is_subclass_of($inst, 'Hashmark_Test')); $typeConfig = $inst->getTypeConfig(); $this->assertEquals(4400, $typeConfig['test_key']); }
/** * Resources needed for most tests. * * @return void */ protected function setUp() { parent::setUp(); $partition = Hashmark::getModule('Partition', '', $this->_db); $this->_analyst = Hashmark::getModule('Analyst', 'BasicDecimal', $this->_db, $partition); $this->_partition = Hashmark::getModule('Partition', '', $this->_db); $this->_core = Hashmark::getModule('Core', '', $this->_db); }
/** * Called by Hashmark::getModule() to inject dependencies. * * @param mixed $db Connection object/resource. * @param string $dbName Database selection, unquoted. [optional] * @param Hashmark_Partition $partition Initialized instance. * @return boolean False if module could not be initialized and is unusable. * Hashmark::getModule() will also then return false. */ public function initModule($db, $partition = '') { parent::initModule($db); $this->_partition = $partition; $dbHelperConfig = Hashmark::getConfig('DbHelper'); $this->_divPrecisionIncr = $dbHelperConfig['div_precision_increment']; return true; }
/** * @test * @group Agent * @group Test * @group runsAgent * @group run */ public function runsAgent() { $expectedFields = array(); $expectedFields['name'] = self::randomString(); $expectedFields['value'] = 'test_value'; $expectedFields['type'] = 'string'; $expectedId = Hashmark::getModule('Core', '', $this->_db)->createScalar($expectedFields); $sample = Hashmark::getModule('Agent', 'ScalarValue')->run(array('scalarId' => $expectedId)); $this->assertEquals($expectedFields['value'], $sample); }
/** * @test * @group Module * @group initsBaseAndTypeProps * @group getModule * @group getBase * @group getBaseConfig * @group getType * @group getTypeConfig */ public function initsBaseAndTypeProps() { $expectedBase = 'Test'; $expectedType = 'FakeModuleType'; $inst = Hashmark::getModule($expectedBase, $expectedType); $this->assertEquals($expectedBase, $inst->getBase()); $baseConfig = $inst->getBaseConfig(); $this->assertEquals($baseConfig['base'], $expectedBase); $this->assertEquals($expectedType, $inst->getType()); $typeConfig = $inst->getTypeConfig(); $this->assertEquals($typeConfig['type'], $expectedType); }
/** * @see Parent/interface signature docs. */ public static function run(&$agent) { if (empty($agent['scalar_id'])) { return; } $db = Hashmark::getModule('DbHelper')->openDb('cron'); $client = Hashmark::getModule('Client', '', $db); $partition = Hashmark::getModule('Partition', '', $db); if (!$client) { return; } $oldValue = $client->get((int) $agent['scalar_id']); $newValue = $oldValue + 5; $time = time(); if (!$partition->createSample($agent['scalar_id'], $newValue, $time)) { $agent['error'] = sprintf('Could not save sample: time=%s value=%s', $time, $value); } }
/** * @test * @group Cron * @group collectsGarbageMergeTables * @group getAllMergeTables */ public function collectsGarbageMergeTables() { $partition = Hashmark::getModule('Partition', '', $this->_db); $mergeTablePrefix = Hashmark::getConfig('Partition', '', 'mergetable_prefix'); // Drop all merge tables to clear the slate. $priorMergeTables = $partition->getTablesLike($mergeTablePrefix . '%'); if ($priorMergeTables) { $partition->dropTable($priorMergeTables); } $scalar = array(); $scalar['name'] = self::randomString(); $scalar['type'] = 'decimal'; $scalarId = Hashmark::getModule('Core', '', $this->_db)->createScalar($scalar); $regTableName = 'test_samples_' . self::randomString(); $partition->createTable($scalarId, $regTableName, $scalar['type']); $now = time(); $start = '2008-04-01 01:45:59'; $mergeTableNames = array(); // Create several merge tables from the same single normal table. // Space them 1 day apart. // Vary $end to make the merge table names unique. for ($t = 0; $t < 5; $t++) { $end = "2009-06-1{$t} 01:45:59"; $comment = gmdate(HASHMARK_DATETIME_FORMAT, $now - $t * 86400); $mergeTableNames[$t] = $mergeTablePrefix . "{$scalarId}_20080401_2009061{$t}"; $actualTable = $partition->createMergeTable($scalarId, $start, $end, array($regTableName), $comment); $this->assertEquals($mergeTableNames[$t], $actualTable); } $maxDays = 3; $maxCount = 2; ob_start(); require HASHMARK_ROOT_DIR . '/Cron/gcMergeTables.php'; ob_end_clean(); for ($t = 0; $t < 5; $t++) { if ($t < 2) { $this->assertTrue($partition->tableExists($mergeTableNames[$t]), "Expected {$mergeTableNames[$t]} to exist"); } else { $this->assertFalse($partition->tableExists($mergeTableNames[$t]), "Expected {$mergeTableNames[$t]} to be missing"); } } }
/** * Return minimum based on bccomp(). * * @param Array $values * @return string Minimum value. * @throws Exception if $values is not a populated Array. */ public static function min($values) { if (!is_array($values) || empty($values)) { throw new Exception('min() requires a populated Array.', HASHMARK_EXCEPTION_VALIDATION); } bcscale(Hashmark::getConfig('DbHelper', '', 'decimal_right_width')); usort($values, 'bccomp'); return $values[0]; }
$core = Hashmark::getModule('Core', '', $db); $partition = Hashmark::getModule('Partition', '', $db); $scheduledAgents = $core->getScheduledAgents(); if (empty($scheduledAgents)) { exit; } // Reuse previously loaded agent objects since they have no properties. $cache = array(); foreach ($scheduledAgents as $scalarAgent) { if ('Running' == $scalarAgent['status']) { $core->setScalarAgentStatus($scalarAgent['id'], 'Unscheduled', 'Last run did not finish.'); continue; } if (!isset($cache[$scalarAgent['name']])) { try { $cache[$scalarAgent['name']] = Hashmark::getModule('Agent', $scalarAgent['name']); } catch (Exception $e) { $error = sprintf('Agent "%s" module missing: %s', $scalarAgent['name'], $e->getMessage()); $core->setScalarAgentStatus($scalarAgent['id'], 'Unscheduled', $error); continue; } } if (!$cache[$scalarAgent['name']]) { $error = "Agent '{$scalarAgent['name']}' was missing"; $core->setScalarAgentStatus($scalarAgent['id'], 'Unscheduled', $error); continue; } $core->setScalarAgentStatus($scalarAgent['id'], 'Running'); $value = $cache[$scalarAgent['name']]->run($scalarAgent); // run() received $scalarAgent by-ref and can apply indepedent logic // to set a new status, error message, etc.
/** * @test * @group Cache * @group removesWholeGroup * @group save * @group load * @group removeGroup */ public function removesWholeGroup() { $cache = Hashmark::getModule('Cache'); // Assert these are unaffected by the operations on subject keys/values/groups. $unaffectedKey = self::randomString(); $unaffectedValue = self::randomString(); $unaffectedGroup = self::randomString(); $this->assertTrue($cache->save($unaffectedValue, $unaffectedKey, $unaffectedGroup)); $this->assertEquals($unaffectedValue, $cache->load($unaffectedKey, $unaffectedGroup)); // Two keys will share group. $subjectKeys = array(self::randomString(), self::randomString()); $subjectGroup = self::randomString(); // Write to subject key/group. foreach ($subjectKeys as $subjectKey) { $subjectValue = self::randomString(); $this->assertTrue($cache->save($subjectValue, $subjectKey, $subjectGroup)); $this->assertEquals($subjectValue, $cache->load($subjectKey, $subjectGroup)); } // Remove subject group. $this->assertTrue($cache->removeGroup($subjectGroup)); foreach ($subjectKeys as $subjectKey) { $this->assertFalse($cache->load($subjectKey, $subjectGroup)); } // Verify other group not touched. $this->assertEquals($unaffectedValue, $cache->load($unaffectedKey, $unaffectedGroup)); }
/** * Increment a scalar identified by name. * * @param string $name * @param string $amount * @param boolean $newSample If true, a new sample partition row is inserted * `scalars` is updated. * @return boolean True on success. * @throws Exception On query error or non-string $scalarName. */ public function incr($scalarName, $amount = '1', $newSample = false) { if (!is_string($scalarName)) { throw new Exception('Cannot look up scalar with a non-string name.', HASHMARK_EXCEPTION_VALIDATION); } if (!preg_match('/[0-9.-]+/', $amount)) { throw new Exception('Cannot increment/decrement a scalar an invalid amount.', HASHMARK_EXCEPTION_VALIDATION); } if ($this->_createScalarIfNotExists) { $core = $this->getModule('Core'); $scalarId = $core->getScalarIdByName($scalarName); if (!$scalarId) { $fields = array('name' => $scalarName, 'type' => 'decimal', 'value' => $amount, 'description' => 'Auto-created by client'); $scalarId = $core->createScalar($fields); if (!$scalarId) { throw new Exception("Scalar '{$scalarName}' was not auto-created", HASHMARK_EXCEPTION_SQL); } if ($newSample) { $sql = 'INSERT INTO ~samples ' . '(`value`, `end`) ' . "VALUES ({$amount}, UTC_TIMESTAMP())"; $partition = $this->getModule('Partition'); $partition->queryCurrent($scalarId, $sql); } return true; } } $dbHelperConfig = Hashmark::getConfig('DbHelper'); $sum = 'CONVERT(`value`, DECIMAL' . "({$dbHelperConfig['decimal_total_width']},{$dbHelperConfig['decimal_right_width']})) + " . $this->escape($amount); if ($newSample) { $sql = "UPDATE {$this->_dbName}`scalars` " . "SET `value` = {$sum}, " . '`last_inline_change` = UTC_TIMESTAMP() ' . 'WHERE `name` = ? ' . 'AND `type` = "decimal"'; $stmt = $this->_db->query($sql, array($scalarName)); if (!$stmt->rowCount()) { return false; } unset($stmt); $currentScalarValue = "SELECT `value` FROM {$this->_dbName}`scalars` WHERE `name` = ? LIMIT 1"; $sql = 'INSERT INTO ~samples ' . '(`value`, `end`) ' . "VALUES (({$currentScalarValue}), UTC_TIMESTAMP())"; $scalarId = $this->getModule('Core')->getScalarIdByName($scalarName); $partition = $this->getModule('Partition'); $stmt = $partition->queryCurrent($scalarId, $sql, array($scalarName)); } else { $sql = "UPDATE {$this->_dbName}`scalars` " . "SET `value` = {$sum}, " . '`last_inline_change` = UTC_TIMESTAMP() ' . 'WHERE `name` = ? ' . 'AND `type` = "decimal"'; $stmt = $this->_db->query($sql, array($scalarName)); } return 1 == $stmt->rowCount(); }
*/ $likeExpr = ''; $alterExprs = array(); if (!$likeExpr) { die("\nNo LIKE expression.\n"); } if (!$alterExprs) { die("\nNo ALTER expression(s).\n"); } /** * For Hashmark::getModule(). */ require_once dirname(__FILE__) . '/../../Hashmark.php'; $db = Hashmark::getModule('DbHelper')->openDb('unittest'); if (!$db) { die("\nNo DB link.\n"); } $p = Hashmark::getModule('Partition', '', $db); $targets = $p->getTablesLike($likeExpr); if (!$targets) { die("\nNo matching tables.\n"); } foreach ($targets as $table) { foreach ($alterExprs as $alter) { $stmt = $db->query("ALTER TABLE `{$table}` {$alter}"); if ($stmt->rowCount()) { echo "`{$table}`: {$alter}\n"; } unset($stmt); } }
/** * Testable logic for assertDecimalEquals(). * * @param string $expected * @param string $actual * @return boolean True if equal. */ public static function checkDecimalEquals($expected, $actual) { if (!is_string($expected) || !is_string($actual)) { return false; } bcscale(Hashmark::getConfig('DbHelper', '', 'decimal_right_width')); return 0 === bccomp($expected, $actual); }
*/ /** * For Hashmark::getModule(). */ require_once dirname(__FILE__) . '/../../../bootstrap.php'; /** * For hashmark_random_samples(). */ require_once HASHMARK_ROOT_DIR . '/Test/Analyst/BasicDecimal/Tool/randomSamples.php'; define('HASHMARK_CREATESAMPLES_TYPE', 'decimal'); define('HASHMARK_CREATESAMPLES_SCALARS', 1); define('HASHMARK_CREATESAMPLES_COUNT', 10000); 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);
*/ $modList = array('BcMath' => '', 'Cache' => '', 'Client' => '', 'Core' => '', 'DbHelper' => '', 'Partition' => '', 'Agent' => 'YahooWeather', 'Test' => 'FakeModuleType'); foreach ($modList as $baseName => $typeName) { // Cache modules don't use the DB argument, // but it should have no effect. $inst = Hashmark::getModule($baseName, $typeName, $mockDb); if ($typeName) { $className = "Hashmark_{$baseName}_{$typeName}"; } else { $className = "Hashmark_{$baseName}"; } $testDetail = "{$className} module.\n"; if ($inst instanceof $className) { echo "pass: Loaded {$testDetail}"; } else { echo "====> FAIL: Could not load {$testDetail}"; } } /** * * Misc. configuration value checks. * */ $mockScalarId = 1234; $partitionTableName = Hashmark::getModule('Partition', '', $mockDb)->getIntervalTableName($mockScalarId); $testDetail = "partition name with '" . Hashmark::getConfig('Partition', '', 'interval') . "' setting in Config/Partition.php.\n"; if ($partitionTableName) { echo "pass: Built {$partitionTableName} {$testDetail}"; } else { echo "====> FAIL: Could not build {$testDetail}"; }
/** * @test * @group Client * @group scalarCreatedIfNotExistsWithNewSample * @group incr */ public function scalarCreatedIfNotExistsWithNewSample() { $this->_client->createScalarIfNotExists(true); $expectedName = self::randomString(); $expectedValue = self::randomDecimal(); $this->_client->incr($expectedName, $expectedValue, true); $scalar = $this->_core->getScalarByName($expectedName); $sample = Hashmark::getModule('Partition', '', $this->_db)->getLatestSample($scalar['id']); $this->assertDecimalEquals($expectedValue, $sample['value']); }
/** * Resources needed for most tests. * * @return void */ protected function setUp() { parent::setUp(); $this->_core = Hashmark::getModule('Core', '', $this->_db); }
* - @name macros will not be escaped nor quoted, ex. SQL functions. * - ~name table name macros will be backtick-quoted. * - To set the destination columns for the INSERT INTO ... SELECT * that populates a temp. table with macro named ~exampleTemp~, * define macro @exampleTempCols, ex. w/ value: `x`, `y` * - ROUND() used to avoid truncation warnings. * * @filesource * @copyright Copyright (c) 2008-2011 David Smith * @license http://www.opensource.org/licenses/mit-license.php MIT License * @package Hashmark * @subpackage Sql * @version $Id$ */ $decimalTotalWidth = Hashmark::getConfig('DbHelper', '', 'decimal_total_width'); $decimalRightWidth = Hashmark::getConfig('DbHelper', '', 'decimal_right_width'); $sql = array(); $sql['values'] = 'SELECT `end` AS `x`, ' . '`value` AS `y` ' . 'FROM ~samples ' . 'WHERE `end` >= ? ' . 'AND `end` <= ? '; /** * - Self-join allows us to pull the most recent row from inside each * interval to reprsent the whole. * - Duplicate the start/end conditions in the self-join * in order to narrow `s2` scan. */ $sql['valuesAtInterval'] = 'SELECT `s1`.`end` AS `x`, ' . '`s1`.`value` AS `y` ' . 'FROM ~samples AS `s1` ' . 'LEFT JOIN ~samples AS `s2` ' . 'ON DATE_FORMAT(`s1`.`end`, ?) = DATE_FORMAT(`s2`.`end`, ?) ' . 'AND `s1`.`end` < `s2`.`end` ' . 'AND `s2`.`end` >= ? ' . 'AND `s2`.`end` <= ? ' . 'WHERE `s1`.`end` >= ? ' . 'AND `s1`.`end` <= ? ' . 'AND `s2`.`end` IS NULL '; $sql['valuesAgg'] = 'SELECT ROUND(@aggFunc(@distinct`value`), ' . $decimalRightWidth . ') AS `y` ' . 'FROM ~samples ' . 'WHERE `end` >= ? ' . 'AND `end` <= ? '; $sql['valuesAggAtInterval'] = 'SELECT DATE_FORMAT(`end`, ?) AS `x`, ' . 'ROUND(@aggFunc(@distinct`value`), ' . $decimalRightWidth . ') AS `y` ' . 'FROM ~samples ' . 'WHERE `end` >= ? ' . 'AND `end` <= ? ' . 'GROUP BY `x` '; $sql['valuesNestedAggAtInterval'] = 'SELECT ROUND(@aggFunc(@distinct`y2`), ' . $decimalRightWidth . ') AS `y2` ' . 'FROM ~valuesAggAtInterval '; $sql['valuesAggAtRecurrence'] = 'SELECT @recurFunc(`end`) AS `x`, ' . 'ROUND(@aggFunc(@distinct`value`), ' . $decimalRightWidth . ') AS `y` ' . 'FROM ~samples ' . 'WHERE `end` >= ? ' . 'AND `end` <= ? ' . 'GROUP BY @recurFunc(`end`) '; /** * - Duplicate the start/end conditions in the self-join
/** * @test * @group DbDependent * @group expandsNamedMacros * @group expandSql */ public function expandsNamedMacros() { $mod = Hashmark::getModule('Core', '', $this->_db); // Verify that ':' prefixed macros get escaped/quoted; // that '@' prefixed get only escaped. $sql = 'SELECT * FROM `@table` WHERE `id` = :id OR `name` = :name'; $values = array('@table' => 'sca"lars', ':id' => 2, ':name' => 'two\'s'); $expectedSql = 'SELECT * FROM `sca\\"lars` WHERE `id` = 2 OR `name` = \'two\\\'s\''; $actualSql = $mod->expandSql($sql, $values); $this->assertEquals($expectedSql, $actualSql); }