public function connect() { $this->linkID = mysqli_connect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass'], $this->config['dbname']); if ($this->linkID === FALSE) { xhprof_error("Could not connect to db"); throw new Exception("Unable to connect to database"); return false; } $this->query("SET NAMES utf8"); }
public function connect() { $linkid = mssql_connect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass']); if ($linkid === FALSE) { xhprof_error("Could not connect to db"); throw new Exception("Unable to connect to database"); return false; } mssql_select_db($this->config['dbname'], $linkid); $this->linkID = $linkid; }
/** * Send an HTTP header with the response. You MUST use this function instead * of header() so that we can debug header issues because they're virtually * impossible to debug otherwise. If you try to commit header(), SVN will * reject your commit. * * @param string HTTP header name, like 'Location' * @param string HTTP header value, like 'http://www.example.com/' * */ function xhprof_http_header($name, $value) { if (!$name) { xhprof_error('http_header usage'); return null; } if (!is_string($value)) { xhprof_error('http_header value not a string'); } header($name . ': ' . $value, true); }
public function connect() { $connectionInfo = array("UID" => $this->config['dbuser'], "PWD" => $this->config['dbpass'], "Database" => $this->config['dbname'], "ReturnDatesAsStrings" => TRUE); $linkid = sqlsrv_connect($this->config['dbhost'], $connectionInfo); if ($linkid === FALSE) { xhprof_error("Could not connect to db"); throw new Exception("Unable to connect to database"); return false; } $this->linkID = $linkid; }
protected function db() { global $_xhprof; $linkid = mysqli_connect($_xhprof['dbhost'], $_xhprof['dbuser'], $_xhprof['dbpass']); if ($linkid === FALSE) { xhprof_error("Could not connect to db"); $run_desc = "could not connect to db"; throw new Exception("Unable to connect to database"); return false; } mysqli_select_db($linkid, $_xhprof['dbname']); $this->linkID = $linkid; }
public function connect() { $connectionString = $this->config['dbtype'] . ':host=' . $this->config['dbhost'] . ';dbname=' . $this->config['dbname']; $db = new PDO($connectionString, $this->config['dbuser'], $this->config['dbpass']); if ($db === FALSE) { xhprof_error("Could not connect to db"); $run_desc = "could not connect to db"; throw new Exception("Unable to connect to database"); return FALSE; } $this->db = $db; $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); }
protected function db() { global $_xhprof; $connectionInfo = array("UID" => $_xhprof['dbuser'], "PWD" => $_xhprof['dbpass'], "Database" => $_xhprof['dbname'], "ReturnDatesAsStrings" => TRUE); $linkid = sqlsrv_connect($_xhprof['dbhost'], $connectionInfo); if ($linkid === FALSE) { xhprof_error("Could not connect to db"); $run_desc = "could not connect to db"; throw new Exception("Unable to connect to database"); return false; } $this->linkID = $linkid; }
public function save_run($xhprof_data, $type, $run_id = null) { // Use PHP serialize function to store the XHProf's // raw profiler data. $xhprof_data = serialize($xhprof_data); if ($run_id === null) { $run_id = $this->gen_run_id($type); } $file_name = $this->file_name($run_id, $type); $file = fopen($file_name, 'w'); if ($file) { fwrite($file, $xhprof_data); fclose($file); } else { xhprof_error("Could not open {$file_name}\n"); } if (defined("ECAE_MODE") && constant("ECAE_MODE")) { // 如果是存放在ecae中返回的是ECAE S3存放id return ecae_file_save(constant("ECAE_SITE_NAME") . "-private", $file_name, array("path" => "xphrof", "name" => $run_id . ".txt")); } // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n"; return $run_id; }
xhprof_param_init($params); if (!empty($run)) { // single run mode $raw_data = $xhprof_runs_impl->get_run($run, $source, $desc_unused); $functions = xhprof_get_matching_functions($q, $raw_data); } else { if (!empty($run1) && !empty($run2)) { // diff mode $raw_data = $xhprof_runs_impl->get_run($run1, $source, $desc_unused); $functions1 = xhprof_get_matching_functions($q, $raw_data); $raw_data = $xhprof_runs_impl->get_run($run2, $source, $desc_unused); $functions2 = xhprof_get_matching_functions($q, $raw_data); $functions = array_unique(array_merge($functions1, $functions2)); asort($functions); } else { xhprof_error("no valid runs specified to typeahead endpoint"); $functions = array(); } } // If exact match is present move it to the front if (in_array($q, $functions)) { $old_functions = $functions; $functions = array($q); foreach ($old_functions as $f) { // exact match case has already been added to the front if ($f != $q) { $functions[] = $f; } } } foreach ($functions as $f) {
public function __construct($dir = null) { // if user hasn't passed a directory location, // we use the xhprof.output_dir ini setting // if specified, else we default to the directory // in which the error_log file resides. if (empty($dir)) { $dir = ini_get("xhprof.output_dir"); if (empty($dir)) { // some default that at least works on unix... $dir = "/tmp"; xhprof_error("Warning: Must specify directory location for XHProf runs. " . "Trying {$dir} as default. You can either pass the " . "directory location as an argument to the constructor " . "for XHProfRuns_Default() or set xhprof.output_dir " . "ini param."); } } $this->dir = $dir; }
/** * Takes raw XHProf data that was aggregated over "$num_runs" number * of runs averages/nomalizes the data. Essentially the various metrics * collected are divided by $num_runs. */ protected function normalize_metrics($raw_data, $num_runs) { if (empty($raw_data) || $num_runs == 0) { return $raw_data; } $raw_data_total = array(); if (isset($raw_data["==>main()"]) && isset($raw_data["main()"])) { xhprof_error("XHProf Error: both ==>main() and main() set in raw data..."); } foreach ($raw_data as $parent_child => $info) { foreach ($info as $metric => $value) { $raw_data_total[$parent_child][$metric] = $value / $num_runs; } } return $raw_data_total; }
/** * Initialize params from URL query string. The function * creates globals variables for each of the params * and if the URL query string doesn't specify a particular * param initializes them with the corresponding default * value specified in the input. * * @params array $params An array whose keys are the names * of URL params who value needs to * be retrieved from the URL query * string. PHP globals are created * with these names. The value is * itself an array with 2-elems (the * param type, and its default value). * If a param is not specified in the * query string the default value is * used. * @author Kannan */ function xhprof_param_init($params) { /* Create variables specified in $params keys, init defaults */ foreach ($params as $k => $v) { switch ($v[0]) { case XHPROF_STRING_PARAM: $p = xhprof_get_string_param($k, $v[1]); break; case XHPROF_UINT_PARAM: $p = xhprof_get_uint_param($k, $v[1]); break; case XHPROF_FLOAT_PARAM: $p = xhprof_get_float_param($k, $v[1]); break; case XHPROF_BOOL_PARAM: $p = xhprof_get_bool_param($k, $v[1]); break; default: xhprof_error("Invalid param type passed to xhprof_param_init: " . $v[0]); // gg: let the script go on anyway //exit(); throw new Exception("Invalid param type passed to xhprof_param_init: " . $v[0]); } // create a global variable using the parameter name. $GLOBALS[$k] = $p; } }
/** * Initialize params from URL query string. The function * creates globals variables for each of the params * and if the URL query string doesn't specify a particular * param initializes them with the corresponding default * value specified in the input. * * @params array $params An array whose keys are the names * of URL params who value needs to * be retrieved from the URL query * string. PHP globals are created * with these names. The value is * itself an array with 2-elems (the * param type, and its default value). * If a param is not specified in the * query string the default value is * used. * @author Kannan */ function xhprof_param_init($params) { /* Create variables specified in $params keys, init defaults */ foreach ($params as $k => $v) { switch ($v[0]) { case XHPROF_STRING_PARAM: $p = xhprof_get_string_param($k, $v[1]); break; case XHPROF_UINT_PARAM: $p = xhprof_get_uint_param($k, $v[1]); break; case XHPROF_FLOAT_PARAM: $p = xhprof_get_float_param($k, $v[1]); break; case XHPROF_BOOL_PARAM: $p = xhprof_get_bool_param($k, $v[1]); break; default: xhprof_error("Invalid param type passed to xhprof_param_init: " . $v[0]); exit; } if ($k === 'run') { $p = implode(',', array_filter(explode(',', $p), 'ctype_xdigit')); } // create a global variable using the parameter name. $GLOBALS[$k] = $p; } }
/** * Given some run data, one type and, optionally, one runid * store the information in DB * * Note that $type is completely ignored */ public function save_run($xhprof_data, $type, $run_id = null) { global $DB, $CFG; if (is_null($this->url)) { xhprof_error("Warning: You must use the prepare_run() method before saving it"); } // Calculate runid if needed $this->runid = is_null($run_id) ? md5($this->url . '-' . uniqid()) : $run_id; // Calculate totals $this->totalexecutiontime = $xhprof_data['main()']['wt']; $this->totalcputime = $xhprof_data['main()']['cpu']; $this->totalcalls = array_reduce($xhprof_data, array($this, 'sum_calls')); $this->totalmemory = $xhprof_data['main()']['mu']; // Prepare data $rec = new stdClass(); $rec->runid = $this->runid; $rec->url = $this->url; $rec->data = base64_encode(gzcompress(serialize($xhprof_data), 9)); $rec->totalexecutiontime = $this->totalexecutiontime; $rec->totalcputime = $this->totalcputime; $rec->totalcalls = $this->totalcalls; $rec->totalmemory = $this->totalmemory; $rec->timecreated = $this->timecreated; $DB->insert_record('profiling', $rec); if (PHPUNIT_TEST) { // Calculate export variables. $tempdir = 'profiling'; make_temp_directory($tempdir); $runids = array($this->runid); $filename = $this->runid . '.mpr'; $filepath = $CFG->tempdir . '/' . $tempdir . '/' . $filename; // Generate the mpr file and send it. if (profiling_export_runs($runids, $filepath)) { fprintf(STDERR, "Profiling data saved to: " . $filepath . "\n"); } } return $this->runid; }
public function save_run_fqpn($xhprof_data, $type, $run_id = null, $run_path) { // Use PHP serialize function to store the XHProf's // raw profiler data. if (function_exists(igbinary_serialize)) { $xhprof_data = igbinary_serialize($xhprof_data); } else { $xhprof_data = serialize($xhprof_data); } if ($run_id === null) { $run_id = $this->gen_run_id($type); } $file_name = "{$run_path}/{$run_id}.{$type}"; $file = fopen($file_name, 'w'); if ($file) { fwrite($file, $xhprof_data); fclose($file); } else { xhprof_error("Could not open {$file_name}\n"); } // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n"; return $run_id; }
/** * Generate image content from phprof run id. * * @param \Sugarcrm\XHProf\Viewer\Storage\StorageInterface $storage An object that implements * the iXHProfRuns interface * @param run_id, integer, the unique id for the phprof run, this is the * primary key for phprof database table. * @param type, string, one of the supported image types. See also * $xhprof_legal_image_types. * @param threshold, float, the threshold value [0,1). The functions in the * raw_data whose exclusive wall times ratio are below the * threshold will be filtered out and won't apprear in the * generated image. * @param func, string, the focus function. * @returns, string, the DOT script to generate image. * * @author cjiang */ function xhprof_get_content_by_run(\Sugarcrm\XHProf\Viewer\Storage\StorageInterface $storage, $run_id, $type, $threshold, $func, $source, $critical_path) { if (!$run_id) { return ""; } $raw_data = $storage->getRunXHProfData($run_id); if (!$raw_data) { xhprof_error("Raw data is empty"); return ""; } $script = xhprof_generate_dot_script($raw_data, $threshold, $source, $run_id, $func, $critical_path); $content = xhprof_generate_image_by_dot($script, $type); return $content; }
function xhprof_prune_run($raw_data, $prune_percent) { $main_info = $raw_data["main()"]; if (empty($main_info)) { xhprof_error("XHProf: main() missing in raw data"); return false; } // raw data should contain either wall time or samples information... if (isset($main_info["wt"])) { $prune_metric = "wt"; } else { if (isset($main_info["samples"])) { $prune_metric = "samples"; } else { xhprof_error("XHProf: for main() we must have either wt " . "or samples attribute set"); return false; } } // determine the metrics present in the raw data.. $metrics = array(); foreach ($main_info as $metric => $val) { if (isset($val)) { $metrics[] = $metric; } } $prune_threshold = $main_info[$prune_metric] * $prune_percent / 100.0; init_metrics($raw_data, null, null, false); $flat_info = xhprof_compute_inclusive_times($raw_data); foreach ($raw_data as $parent_child => $info) { list($parent, $child) = xhprof_parse_parent_child($parent_child); // is this child's overall total from all parents less than threshold? if ($flat_info[$child][$prune_metric] < $prune_threshold) { unset($raw_data[$parent_child]); // prune the edge } else { if ($parent && $parent != "__pruned__()" && $flat_info[$parent][$prune_metric] < $prune_threshold) { // Parent's overall inclusive metric is less than a threshold. // All edges to the parent node will get nuked, and this child will // be a dangling child. // So instead change its parent to be a special function __pruned__(). $pruned_edge = xhprof_build_parent_child_key("__pruned__()", $child); if (isset($raw_data[$pruned_edge])) { foreach ($metrics as $metric) { $raw_data[$pruned_edge][$metric] += $raw_data[$parent_child][$metric]; } } else { $raw_data[$pruned_edge] = $raw_data[$parent_child]; } unset($raw_data[$parent_child]); // prune the edge } } } return $raw_data; }
public function save_run($xhprof_data, $type, $run_id = null) { // Use PHP serialize function to store the XHProf's // raw profiler data. $xhprof_data = serialize($xhprof_data); if ($run_id === null) { $run_id = $this->gen_run_id($type); } $file_name = $this->file_name($run_id, $type); $file = fopen($file_name, 'w'); if ($file) { fwrite($file, $xhprof_data); fclose($file); } else { xhprof_error("Could not open {$file_name}\n"); } // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n"; return $run_id; }
/** * Given some run data, one type and, optionally, one runid * store the information in DB * * Note that $type is completely ignored */ public function save_run($xhprof_data, $type, $run_id = null) { global $DB; if (is_null($this->url)) { xhprof_error("Warning: You must use the prepare_run() method before saving it"); } // Calculate runid if needed $this->runid = is_null($run_id) ? md5($this->url . '-' . uniqid()) : $run_id; // Calculate totals $this->totalexecutiontime = $xhprof_data['main()']['wt']; $this->totalcputime = $xhprof_data['main()']['cpu']; $this->totalcalls = array_reduce($xhprof_data, array($this, 'sum_calls')); $this->totalmemory = $xhprof_data['main()']['mu']; // Prepare data $rec = new stdClass(); $rec->runid = $this->runid; $rec->url = $this->url; $rec->data = base64_encode(serialize($xhprof_data)); $rec->totalexecutiontime = $this->totalexecutiontime; $rec->totalcputime = $this->totalcputime; $rec->totalcalls = $this->totalcalls; $rec->totalmemory = $this->totalmemory; $rec->timecreated = $this->timecreated; $DB->insert_record('profiling', $rec); return $this->runid; }
/** * Generate image content from phprof run id. * * @param object $xhprof_runs_impl An object that implements * the iXHProfRuns interface * @param run_id, integer, the unique id for the phprof run, this is the * primary key for phprof database table. * @param type, string, one of the supported image types. See also * $xhprof_legal_image_types. * @param threshold, float, the threshold value [0,1). The functions in the * raw_data whose exclusive wall times ratio are below the * threshold will be filtered out and won't apprear in the * generated image. * @param func, string, the focus function. * @returns, string, the DOT script to generate image. * * @author cjiang */ function xhprof_get_content_by_run($xhprof_runs_impl, $run_id, $type, $threshold, $func, $source, $critical_path) { if (!$run_id) { return ""; } $raw_data = $xhprof_runs_impl->get_run($run_id, $source, $description); if (!$raw_data) { xhprof_error("Raw data is empty"); return ""; } $script = xhprof_generate_dot_script($raw_data, $threshold, $source, $description, $func, $critical_path); $content = xhprof_generate_image_by_dot($script, $type); return $content; }
protected function db() { $linkid = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass); if ($linkid === FALSE) { xhprof_error("Could not connect to db"); $run_desc = "could not connect to db"; return null; } mysql_select_db($this->dbName, $linkid); $this->linkID = $linkid; }