Skip to content

Instantly share code, notes, and snippets.

@mbijon
Created September 18, 2012 08:57
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mbijon/3742129 to your computer and use it in GitHub Desktop.
Quick benchmarker for WordPress Plugin: SEO Auto Linker. For @danielbachhuber b/c it uses many preg_matches
<?php
/*
* Usage:
* 1. Paste into Debug Bar console and Run
* 2. Run it several times (just in case iTunes is running on your dev server)
*
* Benchmarking Alternatives:
* - Wrap the real SEO Auto Linker for wp-cli and replace the call to content()
* - Add this quick & dirty benchmark loop to Debug Bar
*
*/
// Get us some content to filter
$remote = wp_remote_get( 'http://www.danielbachhuber.com/' );
$content = $remote['body'];
// Repeat times
$repeat = 20;
// For the record
echo "Looping $repeat times<br /><br />";
// Time the preg_match way
$time_a1 = microtime( true );
echo "Start preg_match: $time_a1<br />";
// The real work
$filtered = null;
for ( $i = 0; $i < $repeat; $i++ ){
$filtered .= content( $content );
}
// Timing output
$time_a2 = microtime( true );
echo "End preg_match: $time_a2<br /><br />";
echo "Total time: " . 1000 * ( $time_a2 - $time_a1 ) . "&nbsp;milliseconds<br />";
echo "Time per execution: " . 1000 * ( ( $time_a2 - $time_a1 ) / $repeat ) . "&nbsp;milliseconds<br /><br />";
echo "<h2>Original</h2>" . $content;
echo "<br /><br />";
echo "<h2>Filtered</h2>" . $filtered;
// -- Plugin Stubs--------------------------------------------------------------
// From SEO Auto Linker, http://wordpress.org/extend/plugins/seo-auto-linker/
// -----------------------------------------------------------------------------
function content( $content ) {
$header_replacements = array();
$link_replacements = array();
$other_replacements = array();
$shortcode_replacements = array();
$filtered = $content;
preg_match_all( '/' . get_shortcode_regex() . '/', $filtered, $scodes );
if( ! empty( $scodes[0] ) ) {
$shortcode_replacements = gen_replacements( $scodes[0], 'shortcode' );
$filtered = replace( $shortcode_replacements, $filtered );
}
preg_match_all( '/<h[1-6][^>]*>.+?<\/h[1-6]>/iu', $filtered, $headers );
if( ! empty( $headers[0] ) ) {
$header_replacements = gen_replacements( $headers[0], 'header' );
$filtered = replace( $header_replacements, $filtered );
}
preg_match_all( '/<(img|input)(.*?) \/?>/iu', $filtered, $others );
if( ! empty( $others[0] ) ) {
$other_replacements = gen_replacements( $others[0], 'others' );
$filtered = replace( $other_replacements, $filtered );
}
// Not using Links post types, build here
preg_match_all(
'/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/iu',
$filtered,
$links
);
if( ! empty( $links[0] ) ) {
$start = count( $link_replacements );
$tmp = gen_replacements( $links[0], 'links', $start );
$filtered = replace( $tmp, $filtered );
$link_replacements = array_merge(
$link_replacements,
$tmp
);
}
$regex = get_kw_regex();
$url = 'http://www.danielbachhuber.com';
$max = 1;
if( ! $regex || !$url || !$max )
continue;
$target = '_self';
$filtered = preg_replace(
$regex,
'$1<a href="' . esc_url( $url ) . '" title="$2" target="' . $target . '">$2</a>$3',
$filtered,
absint( $max )
);
return $filtered;
}
function replace( $arr, $content ) {
return str_replace(
array_values( $arr ),
array_keys( $arr ),
$content
);
}
function gen_replacements( $arr, $key, $start = 0 ) {
$hash = md5( 'seo-auto-linker' );
$rv = array();
$h = $hash;
foreach( $arr as $a ) {
$rv["<!--{$h}-{$key}-{$start}-->"] = $a;
$start++;
}
return $rv;
}
function get_kw_regex() {
$keywords = array(
'wordpress',
'post',
'url',
'test',
'image',
'hello',
'world',
'WordPress',
'code',
'git'
);
return sprintf( '/(\b)(%s)(\b)/ui', implode( '|', $keywords ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment