Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmark for different approaches to registering forms.
<?php
include( __DIR__ . '/wp-load.php' );
const MAXTIMES = 10000;
class WP_Form {
static $forms = array();
var $object_id;
var $form_name;
var $form_title;
var $form_hidden;
var $object_type;
var $fields;
function __construct( $args ) {
$this->assign( $args );
}
function assign( $args ) {
foreach( $args as $name => $value ) {
if ( method_exists( $this, $method_name = "set_{$name}" ) ) {
call_user_func( array( $this, $method_name ), $value );
} else if ( property_exists( $this, $name ) ) {
$this->{$name} = $value;
} else if ( $this->non_public_property_exists( $property_name = "_{$name}" ) ) {
$this->{$property_name} = $value;
} else {
$this->extra[$name] = $value;
}
}
}
/**
*
*/
function non_public_property_exists( $property ) {
$reflection = new ReflectionClass( get_class( $this ) );
if ( ! $reflection->hasProperty( $property ) ) {
$exists = false;
} else {
$property = $reflection->getProperty( $property );
$exists = $property->isProtected() || $property->isPrivate();
}
return $exists;
}
}
class Object_Classifier {
var $object_type = false;
var $subtype = false;
protected static $_core_object_types = array(
'post' => array( 'has_subtype' => true ),
'user' => array( 'has_subtype' => false ),
'comment' => array( 'has_subtype' => false ),
);
function __construct( $object_type = false ) {
if ( $object_type ) {
$this->assign( $object_type );
}
}
function assign( $object_type = false ) {
if ( empty( $object_type ) ) {
global $post;
$object_type = isset( $post->post_type ) ? "post/{$post->post_type}" : 'post/post';
}
if ( is_a( $object_type, __CLASS__ ) ) {
$this->object_type = $object_type->object_type;
$this->subtype = $object_type->subtype;
} else {
if ( is_string( $object_type ) ) {
if ( isset( self::$_core_object_types[$object_type] ) && ! self::$_core_object_types[$object_type]['has_subtype'] ) {
$this->object_type = $object_type;
$this->subtype = false;
} else {
if ( false == strpos( $object_type, '/' ) ) {
$this->object_type = 'post';
$this->subtype = $object_type;
} else {
list( $this->object_type, $this->subtype ) = explode( '/', $object_type );
}
}
} else {
if ( is_array( $object_type ) ) {
$object_type = (object)$object_type;
}
$this->object_type = property_exists( $object_type, 'object_type' ) ? $object_type->object_type : false;
$this->subtype = property_exists( $object_type, 'subtype' ) ? $object_type->subtype : false;
}
$this->object_type = sanitize_key( $this->object_type );
if ( $this->subtype ) {
$this->subtype = sanitize_key( $this->subtype );
}
if ( 'post' == $this->object_type && ! post_type_exists( $this->subtype ) ) {
$this->subtype = false;
}
}
}
function __toString() {
return "{$this->object_type}/{$this->subtype}";
}
}
//###########################################################################
function register_form_heavy( $object_type, $args ) {
if ( ! is_object( $object_type ) ) {
$object_type = new Object_Classifier( $object_type );
}
$args = wp_parse_args( $args, array(
'object_id' => 0,
'form_name' => 'main',
'form_title' => __( 'Title not Specified' ),
'form_hidden' => false,
'object_type' => $object_type,
));
WP_Form::$forms[count( $object_type )] = new WP_Form( $args );
}
function register_form_light( $object_type, $args ) {
WP_Form::$forms[count( $object_type )] = $args;
}
function test_register_form_heavy() {
for( $i = 0; $i < 10; $i++ ) {
register_form_heavy( "my_1st_post_type{$i}", array(
'object_id' => 123+$i,
'form_title' => __( "My 1st Title #{$i}", 'mysite' ),
'object_type' => "my_1st_post_type #{$i}",
));
}
}
function test_register_form_light() {
for( $i = 0; $i < 10; $i++ ) {
register_form_light( "my_2nd_post_type{$i}", array(
'object_id' => 234+$i,
'form_title' => __( "My 2nd Title #{$i}", 'mysite' ),
'object_type' => "my_2nd_post_type #{$i}",
));
}
}
function test_load_from_serialized( $serialized ) {
WP_Form::$forms = unserialize( $serialized );
}
function timeit( $label, $callable, $arg = false ) {
$time = microtime(true);
for( $i = 0; $i < MAXTIMES; $i++ ) {
call_user_func( $callable, $arg );
}
echo '<hr>';
echo "<strong>{$label}:</strong><br>&nbsp;&nbsp;&nbsp;Time Elapsed: ".((microtime(true) - $time)).'s / ' . MAXTIMES . "<br>";
}
timeit( '10 Lightweight Form Registrations', function(){ test_register_form_light(); } );
WP_Form::$forms = array();
timeit( '10 Heavyweight Form Registrations', function(){ test_register_form_heavy(); } );
$forms = WP_Form::$forms;
WP_Form::$forms = array();
timeit( 'Load 10 Forms from Serialized', function( $forms ){ test_load_from_serialized( serialize( $forms ) ); }, $forms );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.