Skip to content

Instantly share code, notes, and snippets.

@kurtpayne
Created July 18, 2012 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtpayne/3138440 to your computer and use it in GitHub Desktop.
Save kurtpayne/3138440 to your computer and use it in GitHub Desktop.
Janky timer thingy
<?php
// Requests lib
// See https://github.com/rmccue/Requests
require_once 'Requests/library/Requests.php';
Requests::register_autoloader();
// Stopwatch
$timer = new Timer();
// Loop 10 times
set_time_limit(0);
$iterations = 10;
while ($iterations--) {
// Login page
$timer->start();
$url = 'http://example.com/wp-login.php?redirect_to=http%3A%2F%2Fexample.com%2Fwp-admin%2F&reauth=1';
$request = Requests::get($url);
$timer->stop("Admin - login page|$url");
// Log in
$timer->start();
$url = 'http://example.com/wp-login.php';
$request = Requests::post($url, array(), array(
'log' => 'admin',
'pwd' => 'admin',
'redirect_to' => 'http://example.com/wp-admin/',
'testcookie' => '1',
));
$timer->stop("Admin - Logging in|$url");
// Parse cookies
preg_match_all('/wordpress_[a-zA-Z0-9_]+=[a-zA-Z0-9%]+/', $request->headers['set-cookie'], $matches);
$cookie = implode('; ', $matches[0]);
// Admin pages
$pages = array(
'Dashboard' => 'http://example.com/wp-admin/',
'All Posts' => 'http://example.com/wp-admin/edit.php',
'Categories' => 'http://example.com/wp-admin/edit-tags.php?taxonomy=category',
'Tags' => 'http://example.com/wp-admin/edit-tags.php?taxonomy=post_tag',
'Plugins' => 'http://example.com/wp-admin/plugins.php',
'Users' => 'http://example.com/wp-admin/users.php',
'Settings' => 'http://example.com/wp-admin/options-general.php',
'Themes' => 'http://example.com/wp-admin/themes.php',
'Comments' => 'http://example.com/wp-admin/edit-comments.php',
'Pages' => 'http://example.com/wp-admin/edit.php?post_type=page',
'Media' => 'http://example.com/wp-admin/upload.php'
);
foreach ($pages as $name => $url) {
$timer->start();
$request = Requests::get($url, array(
'Cookie' => $cookie
));
$timer->stop("Admin - $name|$url");
}
// New post screen
$timer->start();
$url = 'http://example.com/wp-admin/post-new.php';
$request = Requests::get($url, array(
'Cookie' => $cookie
));
$timer->stop("Admin - New Post|$url");
// Get nonce
preg_match('|<input type="hidden" id="_wpnonce" name="_wpnonce" value="([a-f0-9]+)" />|', $request->body, $matches);
$nonce = $matches[1];
// Get user id
preg_match('|<input type="hidden" id="user-id" name="user_ID" value="([0-9]+)" />|', $request->body, $matches);
$user_id = $matches[1];
// Make a test post
$timer->start();
$url = 'http://example.com/wp-admin/post.php';
$request = Requests::post($url, array(), array(
'_wpnonce' => $nonce,
'user_ID' => $user_id,
'action' => 'editpost',
'originalaction' => 'editpost',
'post_author' => $user_id,
'post_type' => 'post',
'original_post_status' => 'auto-draft',
'post_ID' => 0,
'post_title' => 'New Automated Post',
'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec porta posuere lectus, vel mattis ipsum interdum quis. Aliquam erat volutpat. Aenean tincidunt porttitor consectetur. Praesent posuere pellentesque adipiscing. Nulla eu diam velit. Donec id diam nulla, faucibus ultrices orci. Etiam laoreet nisl a mauris porta ultrices. Suspendisse sem leo, pulvinar id bibendum et, luctus sit amet magna. Aenean lacinia auctor magna, eu convallis lorem luctus vestibulum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed ut euismod tortor. Morbi dignissim, mauris sit amet rutrum dictum, ante lorem accumsan dui, id tincidunt lorem odio et leo. Morbi sodales nisi id leo suscipit vel varius risus porttitor. Ut porta sapien eu eros pretium vitae gravida sapien euismod. Nullam vehicula sollicitudin lectus. Sed nec erat eu risus facilisis interdum nec ac erat.'
));
$timer->stop("Admin - create post|$url");
// Customer pages
$pages = array(
'Home page' => 'http://example.com//',
'Post' => 'http://example.com//hello-world/',
'Page' => 'http://example.com//clearing-floats/',
'Comments Page' => 'http://example.com//page-with-comments/',
'Search' => 'http://example.com//?s=e&submit=Search',
'Archives' => 'http://example.com//2012/02/',
);
foreach ($pages as $name => $url) {
$timer->start();
$request = Requests::get($url);
$timer->stop("Customer - $name|$url");
$timer->start();
$request = Requests::get($url, array(
'Cookie' => $cookie
));
$timer->stop("Customer/Admin - $name|$url");
}
}
// Look at the times!
var_dump($timer->get());
// Write file
$fp = fopen('./results.csv', 'w');
fputcsv($fp, array('Name', 'URL', 'Time'));
foreach ($timer->get() as $tmp) {
list($message, $url) = explode('|', $tmp['message']);
fputcsv($fp, array($message, $url, $tmp['time']));
}
fclose($fp);
/**
* Timer class
*/
class Timer {
/**
* Keep track of times
* @var array
*/
protected $timings = array();
/**
* Current timer
* @var int
*/
protected $timer = 0;
/**
* Start timing
*/
public function start() {
$this->timer = microtime(true);
}
/**
* Stop timing
* @param string $message
*/
public function stop($message) {
if ($this->timer <= 0) {
return;
}
$this->timings[] = array(
'message' => $message,
'time' => microtime(true) - $this->timer
);
$this->timer = 0;
}
/**
* Get the timing results
* @return array
*/
public function get() {
return $this->timings;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment