/**
  *
  */
 public function run()
 {
     $nRecords = mysqli_query($this->conn, 'SELECT COUNT(*) AS `qty` FROM `data`;')->fetch_object()->qty;
     $xml = new DOMDocument("1.0");
     $xml->formatOutput = true;
     $xml->encoding = 'UTF-8';
     $root = $xml->appendChild(new DOMElement('records'));
     $pBar = new ProgressBar($nRecords);
     $totalCost = 0;
     $offset = 0;
     while ($offset < $nRecords) {
         $record = mysqli_query($this->conn, 'SELECT * FROM `data` LIMIT 1 OFFSET ' . $offset)->fetch_assoc();
         $pid = $record['product_id'];
         $xmlRecord = $root->appendChild(new DOMElement('record'));
         $xmlRecord->setAttribute('id', $record['id']);
         $xmlRecord->appendChild(new DOMElement('product_id', $pid));
         $xmlRecord->appendChild(new DOMElement('cost', $record['cost']));
         $product = $this->getRecord($pid);
         $productRatio = isset($product['product_ratio']) ? $product['product_ratio'] : 0;
         $familyRatio = isset($product['family_ratio']) ? $product['family_ratio'] : 0;
         $realCost = $record['cost'] * $productRatio * $familyRatio;
         $xmlRecord->appendChild(new DOMElement('relative_cost', $realCost));
         $xmlRecord->appendChild(new DOMElement('product_name', $product['product_name']));
         $xmlRecord->appendChild(new DOMElement('family_id', $product['family_id']));
         $xmlRecord->appendChild(new DOMElement('family_name', $product['family_name']));
         $totalCost += $realCost;
         $offset++;
         $pBar->updateValue($offset);
     }
     print "\n";
     $xml->appendChild(new DOMElement('total_cost', $totalCost));
     print $xml->saveXML();
     print sprintf("this script lasted %d seconds !\n", intval(date("U") - $this->startTime));
 }
$analysis = array();
$options = getopt("h::u::p::d::");
$host = isset($options['h']) ? $options['h'] : 'localhost';
$user = isset($options['u']) ? $options['u'] : 'root';
$password = isset($options['p']) ? $options['p'] : '';
$database = isset($options['d']) ? $options['d'] : 'test';
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {
    die(mysqli_error($conn));
}
$nRecords = mysqli_query($conn, 'SELECT COUNT(*) AS `qty` FROM `data`;')->fetch_object()->qty;
$xml = new DOMDocument("1.0");
$xml->formatOutput = true;
$xml->encoding = 'UTF-8';
$root = $xml->appendChild(new DOMElement('records'));
$pBar = new ProgressBar($nRecords);
$totalCost = 0;
$offset = 0;
while ($offset < $nRecords) {
    $record = mysqli_query($conn, 'SELECT * FROM `data` LIMIT 1 OFFSET ' . $offset)->fetch_assoc();
    $pid = $record['product_id'];
    $xmlRecord = $root->appendChild(new DOMElement('record'));
    $xmlRecord->setAttribute('id', $record['id']);
    $xmlRecord->appendChild(new DOMElement('product_id', $pid));
    $xmlRecord->appendChild(new DOMElement('cost', $record['cost']));
    $product = mysqli_query($conn, sprintf('SELECT `p`.`name` AS `product_name`, `p`.`ratio` AS `product_ratio`, ' . '`p`.`family_id` AS `family_id`, `f`.`name` AS `family_name`, `f`.`ratio` AS `family_ratio` ' . 'FROM `product` as `p` ' . 'LEFT JOIN `family` AS `f` ON `p`.`family_id`=`f`.`id` ' . 'WHERE `p`.`id`= %d', $pid))->fetch_assoc();
    $analysis[$pid] = isset($analysis[$pid]) ? $analysis[$pid] + 1 : 1;
    $productRatio = isset($product['product_ratio']) ? $product['product_ratio'] : 0;
    $familyRatio = isset($product['family_ratio']) ? $product['family_ratio'] : 0;
    $realCost = $record['cost'] * $productRatio * $familyRatio;
    $xmlRecord->appendChild(new DOMElement('relative_cost', $realCost));
$host = isset($options['h']) ? $options['h'] : 'localhost';
$user = isset($options['u']) ? $options['u'] : 'root';
$password = isset($options['p']) ? $options['p'] : '';
$database = isset($options['d']) ? $options['d'] : 'test';
$nFamilies = isset($options['n']) ? intval($options['n']) : 1000;
$nRecords = isset($options['m']) ? intval($options['m']) : 100000;
$nProducts = isset($options['o']) ? intval($options['o']) : 10000;
$conn = mysqli_connect($host, $user, $password, $database);
if (!$conn) {
    die(mysqli_error($conn));
}
print "Table: family\n";
$pBar = new ProgressBar($nFamilies);
for ($i = 0; $i < $nFamilies; $i++) {
    mysqli_query($conn, sprintf("INSERT IGNORE INTO `family` (`id`, `name`, `ratio`) VALUES ('%d', '%s', '%f')", $i, 'family #' . $i, rand(0, 1000) / 1000));
    $pBar->updateValue($i);
}
$productNameGenerator = new RandomTokenizer();
print "Table: product\n";
$pBar = new ProgressBar($nProducts);
for ($i = 0; $i < $nProducts; $i++) {
    mysqli_query($conn, sprintf("INSERT IGNORE INTO `product` (`id`, `name`, `family_id`, `ratio`) VALUES ('%d', '%s', '%d', '%f')", $i, $productNameGenerator->getPhrase(), rand(0, $nFamilies), rand(0, 1000) / 1000));
    $pBar->updateValue($i);
}
print "\nTable: data\n";
$pBar = new ProgressBar($nRecords);
for ($i = 0; $i < $nRecords; $i++) {
    mysqli_query($conn, sprintf("INSERT IGNORE INTO `data` (`id`, `product_id`, `cost`) VALUES ('%d', '%d', '%f')", $i, rand(0, $nProducts), rand(0, 100) / 1000));
    $pBar->updateValue($i);
}
print "\nDone!\n";