/** * Save the run in the database. * * @param string $xhprof_data * @param mixed $type * @param string $run_id * @param mixed $xhprof_details * @return string */ public function save_run($xhprof_data, $type, $run_id = NULL, $xhprof_details = NULL) { global $_xhprof; $sql = array(); if ($run_id === NULL) { $run_id = $this->gen_run_id($type); } /* Session data is ommitted purposefully, mostly because it's not likely that the data that resides in $_SESSION at this point is the same as the data that the application started off with (for most apps, it's likely that session data is manipulated on most pageloads). The goal of storing get, post and cookie is to help explain why an application chose a particular code execution path, pehaps it was a poorly filled out form, or a cookie that overwrote some default parameters. So having them helps. Most applications don't push data back into those super globals, so we're safe(ish) storing them now. We can't just clone the session data in header.php to be sneaky either, starting the session is an application decision, and we don't want to go starting sessions where none are needed (not good performance wise). We could be extra sneaky and do something like: if(isset($_COOKIE['phpsessid'])) { session_start(); $_xhprof['session_data'] = $_SESSION; } but starting session support really feels like an application level decision, not one that a supposedly unobtrusive profiler makes for you. */ if (!isset($GLOBALS['_xhprof']['serializer']) || strtolower($GLOBALS['_xhprof']['serializer'] == 'php')) { $sql['get'] = $this->db->escape(serialize($_GET)); $sql['cookie'] = $this->db->escape(serialize($_COOKIE)); //This code has not been tested if (isset($_xhprof['savepost']) && $_xhprof['savepost']) { $sql['post'] = $this->db->escape(serialize($_POST)); } else { $sql['post'] = $this->db->escape(serialize(array("Skipped" => "Post data omitted by rule"))); } } else { $sql['get'] = $this->db->escape(json_encode($_GET)); $sql['cookie'] = $this->db->escape(json_encode($_COOKIE)); //This code has not been tested if (isset($_xhprof['savepost']) && $_xhprof['savepost']) { $sql['post'] = $this->db->escape(json_encode($_POST)); } else { $sql['post'] = $this->db->escape(json_encode(array("Skipped" => "Post data omitted by rule"))); } } $sql['pmu'] = isset($xhprof_data['main()']['pmu']) ? $xhprof_data['main()']['pmu'] : ''; $sql['wt'] = isset($xhprof_data['main()']['wt']) ? $xhprof_data['main()']['wt'] : ''; $sql['cpu'] = isset($xhprof_data['main()']['cpu']) ? $xhprof_data['main()']['cpu'] : ''; // The value of 2 seems to be light enugh that we're not killing the server, but still gives us lots of breathing room on // full production code. if (!isset($GLOBALS['_xhprof']['serializer']) || strtolower($GLOBALS['_xhprof']['serializer'] == 'php')) { $sql['data'] = $this->db->escape(gzcompress(serialize($xhprof_data), 2)); } else { $sql['data'] = $this->db->escape(gzcompress(json_encode($xhprof_data), 2)); } $url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']; $sname = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : ''; $sql['url'] = $this->db->escape($url); $sql['c_url'] = $this->db->escape(_urlSimilartor($_SERVER['REQUEST_URI'])); $sql['servername'] = $this->db->escape($sname); $sql['type'] = (int) (isset($xhprof_details['type']) ? $xhprof_details['type'] : 0); $sql['timestamp'] = $this->db->escape($_SERVER['REQUEST_TIME']); $sql['server_id'] = $this->db->escape($_xhprof['servername']); $sql['aggregateCalls_include'] = getenv('xhprof_aggregateCalls_include') ? getenv('xhprof_aggregateCalls_include') : ''; $query = "INSERT INTO `details` (`id`, `url`, `c_url`, `timestamp`, `server_name`, `perfdata`, `type`, `cookie`, `post`, `get`, `pmu`, `wt`, `cpu`, `server_id`, `aggregateCalls_include`) VALUES('{$run_id}', '{$sql['url']}', '{$sql['c_url']}', FROM_UNIXTIME('{$sql['timestamp']}'), '{$sql['servername']}', '{$sql['data']}', '{$sql['type']}', '{$sql['cookie']}', '{$sql['post']}', '{$sql['get']}', '{$sql['pmu']}', '{$sql['wt']}', '{$sql['cpu']}', '{$sql['server_id']}', '{$sql['aggregateCalls_include']}')"; $this->db->query($query); if ($this->db->affectedRows($this->db->linkID) == 1) { return $run_id; } else { global $_xhprof; if ($_xhprof['display'] === TRUE) { echo "Failed to insert: {$query} <br>\n"; } return -1; } }
/** * Save the run in the database. * * @param string $xhprof_data * @param mixed $type * @param string $run_id * @param mixed $xhprof_details * @return string */ public function save_run_sample($xhprof_data, $type, $request_hash, $request_params, $end_at, $run_id = null, $xhprof_details = null, $time = null) { global $_xhprof; //$_xhprof = $GLOBALS['xhprof']; $sql = array(); if ($run_id === null) { $run_id = $this->gen_run_id($type); } if (!isset($GLOBALS['_xhprof']['serializer']) || strtolower($GLOBALS['_xhprof']['serializer'] == 'php')) { //$sql['get'] = $this->db->escape(serialize($_GET)); $sql['get'] = pg_escape_string(serialize($_GET)); $sql['cookie'] = pg_escape_string(serialize($_COOKIE)); //This code has not been tested if (isset($_xhprof['savepost']) && $_xhprof['savepost']) { $sql['post'] = pg_escape_string(serialize($_POST)); } else { $sql['post'] = pg_escape_string(serialize(array("Skipped" => "Post data omitted by rule"))); } } else { $sql['get'] = pg_escape_string(json_encode($_GET)); $sql['cookie'] = pg_escape_string(json_encode($_COOKIE)); //This code has not been tested if (isset($_xhprof['savepost']) && $_xhprof['savepost']) { $sql['post'] = pg_escape_string(json_encode($_POST)); } else { $sql['post'] = pg_escape_string(json_encode(array("Skipped" => "Post data omitted by rule"))); } } $sql['pmu'] = isset($xhprof_data['main()']['pmu']) ? $xhprof_data['main()']['pmu'] : ''; $sql['wt'] = isset($xhprof_data['main()']['wt']) ? $xhprof_data['main()']['wt'] : ''; $sql['cpu'] = isset($xhprof_data['main()']['cpu']) ? $xhprof_data['main()']['cpu'] : ''; // The value of 2 seems to be light enugh that we're not killing the server, but still gives us lots of breathing room on // full production code. if (!isset($GLOBALS['_xhprof']['serializer']) || strtolower($GLOBALS['_xhprof']['serializer'] == 'php')) { $sql['data'] = pg_escape_string(base64_encode(gzcompress(serialize($xhprof_data), 9))); } else { $sql['data'] = pg_escape_string(base64_encode(gzcompress(json_encode($xhprof_data), 9))); } $url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']; $sname = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : ''; $sql['url'] = pg_escape_string($url); $sql['c_url'] = pg_escape_string($this->_urlSimilartor($_SERVER['REQUEST_URI'])); $sql['servername'] = pg_escape_string($sname); $sql['type'] = (int) (isset($xhprof_details['type']) ? $xhprof_details['type'] : 0); $sql['timestamp'] = pg_escape_string($_SERVER['REQUEST_TIME']); $sql['server_id'] = pg_escape_string(php_uname("n")); $sql['aggregateCalls_include'] = getenv('xhprof_aggregateCalls_include') ? getenv('xhprof_aggregateCalls_include') : ''; if (empty($sql['pmu'])) $sql['pmu'] = 0; if (empty($sql['wt'])) $sql['wt'] = 0; if (empty($sql['cpu'])) $sql['cpu'] = 0; $query = "INSERT INTO xhprof_sample (run_id,request_hash, start_at,end_at,params) VALUES('$run_id', '$request_hash',to_timestamp('{$sql['timestamp']}'), to_timestamp('{$end_at}'),'$request_params')"; $this->db->query($query); $query = "INSERT INTO xhprof_sample_details (run_id, url, c_url, \"timestamp\", \"server name\", perfdata, type, cookie, post, get, pmu, wt, cpu, server_id, \"aggregateCalls_include\",request_hash,execute_time) VALUES('$run_id', '{$sql['url']}', '{$sql['c_url']}', to_timestamp('{$sql['timestamp']}'), '{$sql['servername']}', '{$sql['data']}', '{$sql['type']}', '{$sql['cookie']}', '{$sql['post']}', '{$sql['get']}', '{$sql['pmu']}', '{$sql['wt']}', '{$sql['cpu']}', '{$sql['server_id']}', '{$sql['aggregateCalls_include']}', '$request_hash','$time')"; $this->db->query($query); return -1; }