/**
* Display the charts for the given benchmark
* 
* @param mixed $benchmark
*/
function DisplayBenchmarkData(&$benchmark, $loc = null, $title = null)
{
    global $raw_data;
    $raw_data = null;
    // figure out the time of the most recent test
    $test_time = 0;
    if (is_dir("./results/benchmarks/{$benchmark['name']}/data")) {
        $files = scandir("./results/benchmarks/{$benchmark['name']}/data");
        foreach ($files as $file) {
            if (preg_match('/([0-9]+_[0-9]+)\\..*/', $file, $matches)) {
                $UTC = new DateTimeZone('UTC');
                $date = DateTime::createFromFormat('Ymd_Hi', $matches[1], $UTC);
                $time = $date->getTimestamp();
                if ($time > $test_time) {
                    $test_time = $time;
                }
            }
        }
    }
    $out_data = array();
    if (LoadTestData($data, $configurations, $benchmark[name], 0, 'SpeedIndex', $test_time, $meta, $loc)) {
        foreach ($data as $urlid => &$row) {
            $url = $meta[$urlid]['url'];
            $data_points = 0;
            $url_data = array();
            // figure out the maximum number of data points we have
            foreach ($configurations as &$configuration) {
                foreach ($configuration['locations'] as &$location) {
                    if (array_key_exists($configuration['name'], $row) && array_key_exists($location['location'], $row[$configuration['name']]) && is_array($row[$configuration['name']][$location['location']])) {
                        $raw_values = $row[$configuration['name']][$location['location']];
                        $values = array();
                        foreach ($raw_values as $raw_value) {
                            $values[] = $raw_value['value'];
                        }
                        sort($values);
                        $count = count($values);
                        $out_str = null;
                        if ($count > 1) {
                            $median = $values[(int) ($count / 2)];
                            if ($median > 0) {
                                $good_count = 0;
                                $bad_count = 0;
                                foreach ($values as $value) {
                                    $delta = abs(($value - $median) / $median);
                                    if ($delta <= 0.15) {
                                        $good_count++;
                                    } else {
                                        $bad_count++;
                                    }
                                }
                                if ($good_count < 5) {
                                    $out_str = "{$good_count} results within 15% of median, {$bad_count} results not.";
                                }
                            } else {
                                $out_str = "Invalid median: {$median}";
                            }
                        } else {
                            $out_str = "Only {$count} successful runs";
                        }
                        if (isset($out_str)) {
                            echo "{$url} {$configuration['name']} {$location['location']}: {$out_str}<br>\n";
                        }
                    }
                }
            }
        }
    }
}
                        $test['started'] = true;
                    } else {
                        $test['started'] = false;
                    }
                }
                $tests[] = $test;
            }
        }
    }
}
$count = count($tests);
if ($count) {
    setcookie('fs', urlencode($_REQUEST['tests']));
    setcookie('tid', $tests[0]['id']);
    $id = $tests[0]['id'];
    LoadTestData();
} else {
    $error = "No valid tests selected.";
}
if (array_key_exists('thumbSize', $_REQUEST) && is_numeric($_REQUEST['thumbSize'])) {
    $thumbSize = intval($_REQUEST['thumbSize']);
}
if (!isset($thumbSize) || $thumbSize < 50 || $thumbSize > 500) {
    if ($count > 6) {
        $thumbSize = 100;
    } elseif ($count > 4) {
        $thumbSize = 150;
    } else {
        $thumbSize = 200;
    }
}
function LoadTestComparisonTSV($configs, $cached, $metric, &$meta)
{
    $ok = false;
    $tsv = '';
    if (stripos($metric, 'bytes') !== false) {
        $isbytes = true;
    } elseif (stripos($metric, 'time') !== false || stripos($metric, 'render') !== false || stripos($metric, 'fullyloaded') !== false || stripos($metric, 'visualcomplete') !== false || stripos($metric, 'eventstart') !== false || stripos($metric, 'lastVisualChange') !== false || stripos($metric, 'ttfb') !== false) {
        $istime = true;
    }
    $row = "URL";
    foreach ($configs as $config) {
        $row .= "\t{$config['label']}";
    }
    $row .= "\n";
    $tsv .= $row;
    $rows = array();
    $maxValues = 0;
    foreach ($configs as $column => $config) {
        if (LoadTestData($data, $bmConfigs, $config['benchmark'], $cached, $metric, $config['time'], $meta, $config['location'])) {
            foreach ($data as $url => &$configData) {
                $ok = true;
                if (!array_key_exists($url, $rows)) {
                    $rows[$url] = array();
                }
                if (array_key_exists($config['config'], $configData) && array_key_exists($config['location'], $configData[$config['config']])) {
                    foreach ($configData[$config['config']][$config['location']] as &$result) {
                        if (!array_key_exists($column, $rows[$url])) {
                            $rows[$url][$column] = array('test' => $result['test'], 'values' => array());
                            if (!array_key_exists($url, $meta)) {
                                $meta[$url] = array();
                            }
                            if (!array_key_exists('tests', $meta[$url])) {
                                $meta[$url]['tests'] = array();
                            }
                            $meta[$url]['tests'][$column] = $result['test'];
                        }
                        $value = $result['value'];
                        if ($isbytes) {
                            $value = number_format($value / 1024.0, 3, '.', '');
                        } elseif ($istime) {
                            $value = number_format($value / 1000.0, 3, '.', '');
                        }
                        $rows[$url][$column]['values'][] = $value;
                        $maxValues = max($maxValues, count($rows[$url][$column]['values']));
                    }
                }
            }
        }
    }
    foreach ($rows as $url => $rowData) {
        for ($index = 0; $index < $maxValues; $index++) {
            $row = $url;
            foreach ($configs as $column => $config) {
                $row .= "\t";
                if (array_key_exists($column, $rowData) && array_key_exists($index, $rowData[$column]['values'])) {
                    $row .= $rowData[$column]['values'][$index];
                }
            }
            $row .= "\n";
            $tsv .= $row;
        }
    }
    if (!$ok) {
        unset($tsv);
    }
    return $tsv;
}