Modules

Bench_Transliterate
extends Codebench
extends Kohana_Codebench

package
Kohana/Codebench
category
Tests
author
Geert De Deckere <[email protected]>

Class declared in MODPATH/codebench/classes/Bench/Transliterate.php on line 7.

Properties

public $description

Default value:
string(79) "Inspired by:
		 http://forum.kohanaframework.org/comments.php?DiscussionID=6113"

public array $grades

Grade letters with their maximum scores. Used to color the graphs.

Default value:
array(6) (
    125 => string(1) "A"
    150 => string(1) "B"
    200 => string(1) "C"
    300 => string(1) "D"
    500 => string(1) "E"
    "default" => string(1) "F"
)

public $loops

Default value:
integer 10

public $subjects

Default value:
array(215) (
    0 => string(1) "a"
    1 => string(1) "b"
    2 => string(1) "c"
    3 => string(1) "d"
    4 => string(1) "1"
    5 => string(1) "2"
    6 => string(1) "3"
    7 => string(2) "à"
    8 => string(2) "ô"
    9 => string(2) "ď"
    10 => string(3) "ḟ"
    11 => string(2) "ë"
    12 => string(2) "š"
    13 => string(2) "ơ"
    14 => string(2) "ß"
    15 => string(2) "ă"
    16 => string(2) "ř"
    17 => string(2) "ț"
    18 => string(2) "ň"
    19 => string(2) "ā"
    20 => string(2) "ķ"
    21 => string(2) "ŝ"
    22 => string(3) "ỳ"
    23 => string(2) "ņ"
    24 => string(2) "ĺ"
    25 => string(2) "ħ"
    26 => string(3) "ṗ"
    27 => string(2) "ó"
    28 => string(2) "ú"
    29 => string(2) "ě"
    30 => string(2) "é"
    31 => string(2) "ç"
    32 => string(3) "ẁ"
    33 => string(2) "ċ"
    34 => string(2) "õ"
    35 => string(3) "ṡ"
    36 => string(2) "ø"
    37 => string(2) "ģ"
    38 => string(2) "ŧ"
    39 => string(2) "ș"
    40 => string(2) "ė"
    41 => string(2) "ĉ"
    42 => string(2) "ś"
    43 => string(2) "î"
    44 => string(2) "ű"
    45 => string(2) "ć"
    46 => string(2) "ę"
    47 => string(2) "ŵ"
    48 => string(3) "ṫ"
    49 => string(2) "ū"
    50 => string(2) "č"
    51 => string(2) "ö"
    52 => string(2) "è"
    53 => string(2) "ŷ"
    54 => string(2) "ą"
    55 => string(2) "ł"
    56 => string(2) "ų"
    57 => string(2) "ů"
    58 => string(2) "ş"
    59 => string(2) "ğ"
    60 => string(2) "ļ"
    61 => string(2) "ƒ"
    62 => string(2) "ž"
    63 => string(3) "ẃ"
    64 => string(3) "ḃ"
    65 => string(2) "å"
    66 => string(2) "ì"
    67 => string(2) "ï"
    68 => string(3) "ḋ"
    69 => string(2) "ť"
    70 => string(2) "ŗ"
    71 => string(2) "ä"
    72 => string(2) "í"
    73 => string(2) "ŕ"
    74 => string(2) "ê"
    75 => string(2) "ü"
    76 => string(2) "ò"
    77 => string(2) "ē"
    78 => string(2) "ñ"
    79 => string(2) "ń"
    80 => string(2) "ĥ"
    81 => string(2) "ĝ"
    82 => string(2) "đ"
    83 => string(2) "ĵ"
    84 => string(2) "ÿ"
    85 => string(2) "ũ"
    86 => string(2) "ŭ"
    87 => string(2) "ư"
    88 => string(2) "ţ"
    89 => string(2) "ý"
    90 => string(2) "ő"
    91 => string(2) "â"
    92 => string(2) "ľ"
    93 => string(3) "ẅ"
    94 => string(2) "ż"
    95 => string(2) "ī"
    96 => string(2) "ã"
    97 => string(2) "ġ"
    98 => string(3) "ṁ"
    99 => string(2) "ō"
    100 => string(2) "ĩ"
    101 => string(2) "ù"
    102 => string(2) "į"
    103 => string(2) "ź"
    104 => string(2) "á"
    105 => string(2) "û"
    106 => string(2) "þ"
    107 => string(2) "ð"
    108 => string(2) "æ"
    109 => string(2) "µ"
    110 => string(2) "ĕ"
    111 => string(2) "ı"
    112 => string(2) "À"
    113 => string(2) "Ô"
    114 => string(2) "Ď"
    115 => string(3) "Ḟ"
    116 => string(2) "Ë"
    117 => string(2) "Š"
    118 => string(2) "Ơ"
    119 => string(2) "Ă"
    120 => string(2) "Ř"
    121 => string(2) "Ț"
    122 => string(2) "Ň"
    123 => string(2) "Ā"
    124 => string(2) "Ķ"
    125 => string(2) "Ĕ"
    126 => string(2) "Ŝ"
    127 => string(3) "Ỳ"
    128 => string(2) "Ņ"
    129 => string(2) "Ĺ"
    130 => string(2) "Ħ"
    131 => string(3) "Ṗ"
    132 => string(2) "Ó"
    133 => string(2) "Ú"
    134 => string(2) "Ě"
    135 => string(2) "É"
    136 => string(2) "Ç"
    137 => string(3) "Ẁ"
    138 => string(2) "Ċ"
    139 => string(2) "Õ"
    140 => string(3) "Ṡ"
    141 => string(2) "Ø"
    142 => string(2) "Ģ"
    143 => string(2) "Ŧ"
    144 => string(2) "Ș"
    145 => string(2) "Ė"
    146 => string(2) "Ĉ"
    147 => string(2) "Ś"
    148 => string(2) "Î"
    149 => string(2) "Ű"
    150 => string(2) "Ć"
    151 => string(2) "Ę"
    152 => string(2) "Ŵ"
    153 => string(3) "Ṫ"
    154 => string(2) "Ū"
    155 => string(2) "Č"
    156 => string(2) "Ö"
    157 => string(2) "È"
    158 => string(2) "Ŷ"
    159 => string(2) "Ą"
    160 => string(2) "Ł"
    161 => string(2) "Ų"
    162 => string(2) "Ů"
    163 => string(2) "Ş"
    164 => string(2) "Ğ"
    165 => string(2) "Ļ"
    166 => string(2) "Ƒ"
    167 => string(2) "Ž"
    168 => string(3) "Ẃ"
    169 => string(3) "Ḃ"
    170 => string(2) "Å"
    171 => string(2) "Ì"
    172 => string(2) "Ï"
    173 => string(3) "Ḋ"
    174 => string(2) "Ť"
    175 => string(2) "Ŗ"
    176 => string(2) "Ä"
    177 => string(2) "Í"
    178 => string(2) "Ŕ"
    179 => string(2) "Ê"
    180 => string(2) "Ü"
    181 => string(2) "Ò"
    182 => string(2) "Ē"
    183 => string(2) "Ñ"
    184 => string(2) "Ń"
    185 => string(2) "Ĥ"
    186 => string(2) "Ĝ"
    187 => string(2) "Đ"
    188 => string(2) "Ĵ"
    189 => string(2) "Ÿ"
    190 => string(2) "Ũ"
    191 => string(2) "Ŭ"
    192 => string(2) "Ư"
    193 => string(2) "Ţ"
    194 => string(2) "Ý"
    195 => string(2) "Ő"
    196 => string(2) "Â"
    197 => string(2) "Ľ"
    198 => string(3) "Ẅ"
    199 => string(2) "Ż"
    200 => string(2) "Ī"
    201 => string(2) "Ã"
    202 => string(2) "Ġ"
    203 => string(3) "Ṁ"
    204 => string(2) "Ō"
    205 => string(2) "Ĩ"
    206 => string(2) "Ù"
    207 => string(2) "Į"
    208 => string(2) "Ź"
    209 => string(2) "Á"
    210 => string(2) "Û"
    211 => string(2) "Þ"
    212 => string(2) "Ð"
    213 => string(2) "Æ"
    214 => string(2) "İ"
)

Methods

public bench_iconv( ) (defined in Bench_Transliterate)

Source Code

public function bench_iconv($subject)
{
	// Note: need to suppress errors on iconv because some chars trigger the following notice:
	// "Detected an illegal character in input string"
	return preg_replace('~[^-a-z0-9]+~i', '', @iconv('UTF-8', 'ASCII//TRANSLIT', $subject));
}

public bench_utf8( ) (defined in Bench_Transliterate)

Source Code

public function bench_utf8($subject)
{
	return UTF8::transliterate_to_ascii($subject);
}

public __construct( ) (defined in Kohana_Codebench)

Constructor.

Return Values

  • void

Source Code

public function __construct()
{
	// Set the maximum execution time
	set_time_limit(Kohana::$config->load('codebench')->max_execution_time);
}

public run( ) (defined in Kohana_Codebench)

Runs Codebench on the extending class.

Return Values

  • array - Benchmark output

Source Code

public function run()
{
	// Array of all methods to loop over
	$methods = array_filter(get_class_methods($this), array($this, '_method_filter'));

	// Make sure the benchmark runs at least once,
	// also if no subject data has been provided.
	if (empty($this->subjects))
	{
		$this->subjects = array('NULL' => NULL);
	}

	// Initialize benchmark output
	$codebench = array
	(
		'class'       => get_class($this),
		'description' => $this->description,
		'loops'       => array
		(
			'base'    => (int) $this->loops,
			'total'   => (int) $this->loops * count($this->subjects) * count($methods),
		),
		'subjects'    => $this->subjects,
		'benchmarks'  => array(),
	);

	// Benchmark each method
	foreach ($methods as $method)
	{
		// Initialize benchmark output for this method
		$codebench['benchmarks'][$method] = array('time' => 0, 'memory' => 0);

		// Using Reflection because simply calling $this->$method($subject) in the loop below
		// results in buggy benchmark times correlating to the length of the method name.
		$reflection = new ReflectionMethod(get_class($this), $method);

		// Benchmark each subject on each method
		foreach ($this->subjects as $subject_key => $subject)
		{
			// Prerun each method/subject combo before the actual benchmark loop.
			// This way relatively expensive initial processes won't be benchmarked, e.g. autoloading.
			// At the same time we capture the return here so we don't have to do that in the loop anymore.
			$return = $reflection->invoke($this, $subject);

			// Start the timer for one subject
			$token = Profiler::start('codebench', $method.$subject_key);

			// The heavy work
			for ($i = 0; $i < $this->loops; ++$i)
			{
				$reflection->invoke($this, $subject);
			}

			// Stop and read the timer
			$benchmark = Profiler::total($token);

			// Benchmark output specific to the current method and subject
			$codebench['benchmarks'][$method]['subjects'][$subject_key] = array
			(
				'return' => $return,
				'time'   => $benchmark[0],
				'memory' => $benchmark[1],
			);

			// Update method totals
			$codebench['benchmarks'][$method]['time']   += $benchmark[0];
			$codebench['benchmarks'][$method]['memory'] += $benchmark[1];
		}
	}

	// Initialize the fastest and slowest benchmarks for both methods and subjects, time and memory,
	// these values will be overwritten using min() and max() later on.
	// The 999999999 values look like a hack, I know, but they work,
	// unless your method runs for more than 31 years or consumes over 1GB of memory.
	$fastest_method = $fastest_subject = array('time' => 999999999, 'memory' => 999999999); 
	$slowest_method = $slowest_subject = array('time' => 0, 'memory' => 0);

	// Find the fastest and slowest benchmarks, needed for the percentage calculations
	foreach ($methods as $method)
	{
		// Update the fastest and slowest method benchmarks
		$fastest_method['time']   = min($fastest_method['time'],   $codebench['benchmarks'][$method]['time']);
		$fastest_method['memory'] = min($fastest_method['memory'], $codebench['benchmarks'][$method]['memory']);
		$slowest_method['time']   = max($slowest_method['time'],   $codebench['benchmarks'][$method]['time']);
		$slowest_method['memory'] = max($slowest_method['memory'], $codebench['benchmarks'][$method]['memory']);

		foreach ($this->subjects as $subject_key => $subject)
		{
			// Update the fastest and slowest subject benchmarks
			$fastest_subject['time']   = min($fastest_subject['time'],   $codebench['benchmarks'][$method]['subjects'][$subject_key]['time']);
			$fastest_subject['memory'] = min($fastest_subject['memory'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['memory']);
			$slowest_subject['time']   = max($slowest_subject['time'],   $codebench['benchmarks'][$method]['subjects'][$subject_key]['time']);
			$slowest_subject['memory'] = max($slowest_subject['memory'], $codebench['benchmarks'][$method]['subjects'][$subject_key]['memory']);
		}
	}

	// Percentage calculations for methods
	foreach ($codebench['benchmarks'] as & $method)
	{
		// Calculate percentage difference relative to fastest and slowest methods
		$method['percent']['fastest']['time']   = (empty($fastest_method['time']))   ? 0 : ($method['time']   / $fastest_method['time']   * 100);
		$method['percent']['fastest']['memory'] = (empty($fastest_method['memory'])) ? 0 : ($method['memory'] / $fastest_method['memory'] * 100);
		$method['percent']['slowest']['time']   = (empty($slowest_method['time']))   ? 0 : ($method['time']   / $slowest_method['time']   * 100);
		$method['percent']['slowest']['memory'] = (empty($slowest_method['memory'])) ? 0 : ($method['memory'] / $slowest_method['memory'] * 100);

		// Assign a grade for time and memory to each method
		$method['grade']['time']   = $this->_grade($method['percent']['fastest']['time']);
		$method['grade']['memory'] = $this->_grade($method['percent']['fastest']['memory']);

		// Percentage calculations for subjects
		foreach ($method['subjects'] as & $subject)
		{
			// Calculate percentage difference relative to fastest and slowest subjects for this method
			$subject['percent']['fastest']['time']   = (empty($fastest_subject['time']))   ? 0 : ($subject['time']   / $fastest_subject['time']   * 100);
			$subject['percent']['fastest']['memory'] = (empty($fastest_subject['memory'])) ? 0 : ($subject['memory'] / $fastest_subject['memory'] * 100);
			$subject['percent']['slowest']['time']   = (empty($slowest_subject['time']))   ? 0 : ($subject['time']   / $slowest_subject['time']   * 100);
			$subject['percent']['slowest']['memory'] = (empty($slowest_subject['memory'])) ? 0 : ($subject['memory'] / $slowest_subject['memory'] * 100);

			// Assign a grade letter for time and memory to each subject
			$subject['grade']['time']   = $this->_grade($subject['percent']['fastest']['time']);
			$subject['grade']['memory'] = $this->_grade($subject['percent']['fastest']['memory']);
		}
	}

	return $codebench;
}

protected _grade( integer|double $score ) (defined in Kohana_Codebench)

Returns the applicable grade letter for a score.

Parameters

  • integer|double $score required - Score

Return Values

  • string - Grade letter

Source Code

protected function _grade($score)
{
	foreach ($this->grades as $max => $grade)
	{
		if ($max === 'default')
			continue;

		if ($score <= $max)
			return $grade;
	}

	return $this->grades['default'];
}

protected _method_filter( string $method ) (defined in Kohana_Codebench)

Callback for array_filter(). Filters out all methods not to benchmark.

Parameters

  • string $method required - Method name

Return Values

  • boolean

Source Code

protected function _method_filter($method)
{
	// Only benchmark methods with the "bench" prefix
	return (substr($method, 0, 5) === 'bench');
}