Navigation Menu

Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
simple performance recording for page loads
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbender committed Nov 2, 2011
1 parent cdf1d2c commit b5d6322
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ gitstatus.log
refreshCDN
*.swp
.gitignore
tests/speed/stats/stats.db
21 changes: 11 additions & 10 deletions tests/speed/lists-ul.html → tests/speed/lists-ul.php 100755 → 100644
@@ -1,23 +1,24 @@
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile Docs - Lists</title>
<link rel="stylesheet" href="../../css/themes/default/" />
<script src="../../js/jquery.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile Docs - Lists</title>
<link rel="stylesheet" href="../../css/themes/default/" />
<script src="../../js/jquery.js"></script>
<script type="text/javascript" src="stats/perf.js"></script>
<script src="../../js/"></script>
</head>
<body>
</head>
<body>
<div data-role="page">
<div data-role="page" id="list-page">
<div data-role="header">
<h1>400 item list</h1>
</div><!-- /header -->
<div data-role="content" data-filter="true">
<ul data-role="listview" data-theme="d">
<li><a href="index.html">Acura</a></li>
<li><a href="index.html">Audi</a></li>
Expand Down
34 changes: 34 additions & 0 deletions tests/speed/stats/index.php
@@ -0,0 +1,34 @@
<?php
$db = new SQLiteDatabase('stats.db');

// hand to God I tried CREATE TABLE IF NOT EXISTS and it persisted with
// a syntax error. The IDENTICAL query sans IF NOT EXISTS works perfectly
// http://www.sqlite.org/lang_createtable.html
@$db->query('CREATE TABLE stats (id INTEGER, agent TEXT, point TEXT, value REAL, PRIMARY KEY (id))');

// making a sad attempt here to provide a clean REST-respecting url scheme.
// stats with a GET returns - wait for it - the stats, and a post with the
// the right params will create a new entry
if ( $_SERVER['REQUEST_METHOD'] == "GET" ) {
$json = Array();
$results = $db->query( 'SELECT point, value FROM stats' );

// TODO not sure if there's a better way to convert db results into
// a json encodable data structure
while($row = $results->fetch(SQLITE3_ASSOC)){
array_push($json, $row);
}

header("Content-Type: application/json");
echo json_encode($json);
} elseif ( $_POST['datapoint'] && $_POST['value'] && $_POST['agent'] ){
// TODO it is not clear from the docs if there's an easier way to do the
// escaped query interpolation here. Suggestions welcome :(
$data_point = sqlite_escape_string( $_POST['datapoint'] );
$value = sqlite_escape_string( $_POST['value'] );
$agent = sqlite_escape_string( $_POST['agent'] );
$db->query('INSERT INTO stats (agent, point, value) VALUES ("' . $agent . '", "' . $data_point . '",' . $value . ')');

echo "success";
}
?>
70 changes: 70 additions & 0 deletions tests/speed/stats/perf.js
@@ -0,0 +1,70 @@
window.Perf = (function($, Perf) {
$.extend(Perf, {
reportUrl: 'stats/',
revUrl: 'stats/rev.php',

// should be defined before report or poll are run
currentRev: undefined,

report: function( data, after ) {
var self = this;

$.post( self.reportUrl, data, after );
},

poll: function() {
var self = this;

setInterval(function() {
$.get( self.revUrl, function( data ) {

// if there's a new revision refresh or currentRev isn't being set
if( self.currentRev && self.currentRev !== data ){
location.href = location.href;
}
});
}, 60000);
},

setCurrentRev: function() {
var self = this;

$.get( self.revUrl, function( data ) {
self.currentRev = data;
});
}
});

var $listPage = $( "#list-page" );

Perf.setCurrentRev();
Perf.pageLoadStart = Date.now();

$listPage.live( "pagebeforecreate", function() {
Perf.pageCreateStart = Date.now();
});

$listPage.live( "pageinit", function(){
Perf.pageLoadEnd = Date.now();

// report the time taken for a full app boot
Perf.report({
agent: window.navigator.userAgent,
datapoint: "fullboot",
value: Perf.pageLoadEnd - Perf.pageLoadStart
});

// record the time taken to load and enhance the page
// start polling for a new revision
Perf.report({
agent: window.navigator.userAgent,
datapoint: "pageload",
value: Perf.pageCreateStart - Perf.pageLoadStart,
after: function() {
Perf.poll();
}
});
});

return Perf;
})(jQuery, window.Perf || {});
1 change: 1 addition & 0 deletions tests/speed/stats/rev.php
@@ -0,0 +1 @@
<?php echo exec('git rev-parse HEAD') ?>

0 comments on commit b5d6322

Please sign in to comment.