Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simple performance recording for page loads

  • Loading branch information...
commit b5d6322e03ae5e6928cae8b24876718a6516eef8 1 parent cdf1d2c
@johnbender johnbender authored
View
1  .gitignore
@@ -11,3 +11,4 @@ gitstatus.log
refreshCDN
*.swp
.gitignore
+tests/speed/stats/stats.db
View
21 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>
View
34 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";
+ }
+?>
View
70 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 || {});
View
1  tests/speed/stats/rev.php
@@ -0,0 +1 @@
+<?php echo exec('git rev-parse HEAD') ?>
Please sign in to comment.
Something went wrong with that request. Please try again.