Skip to content

Commit

Permalink
Change default formatter to adjust the table column widths to the con…
Browse files Browse the repository at this point in the history
…tent.
  • Loading branch information
UlrichEckhardt committed Nov 8, 2014
1 parent 26b7e47 commit e8228fe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 34 deletions.
62 changes: 45 additions & 17 deletions src/Athletic/Formatters/DefaultFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,60 @@ public function getFormattedResults($results)
{
$returnString = "\n";

$header = array(
'Method Name',
'Iterations',
'Average Time',
'Ops/second',
);

foreach ($results as $result) {
$returnString .= $result->getClassName() . "\n";

$longest = 0;
/** @var MethodResults $methodResult */
// build a table containing the formatted numbers
$table = array();
foreach ($result as $methodResult) {
if (strlen($methodResult->methodName) > $longest) {
$longest = strlen($methodResult->methodName);
}
$table[] = array(
$methodResult->methodName,
number_format($methodResult->iterations),
number_format($methodResult->avg, 13),
number_format($methodResult->ops, 5),
);
}
$returnString .= ' ' . str_pad(
'Method Name',
$longest
) . " Iterations Average Time Ops/second\n";

$returnString .= ' ' . str_repeat('-', $longest) . " ------------ -------------- -------------\n";

foreach ($result as $methodResult) {
// determine column widths for table layout
$lengths = array_map('strlen', $header);
foreach ($table as $row) {
foreach ($row as $name => $value) {
$lengths[$name] = max(strlen($value), $lengths[$name]);
}
}

$method = str_pad($methodResult->methodName, $longest);
$iterations = str_pad(number_format($methodResult->iterations), 10);
$avg = str_pad(number_format($methodResult->avg, 13), 13);
$ops = str_pad(number_format($methodResult->ops, 5), 7);
$returnString .= " $method: [$iterations] [$avg] [$ops]\n";
// format header and table rows
$returnString .= sprintf(
" %s %s %s %s\n",
str_pad($header[0], $lengths[0]),
str_pad($header[1], $lengths[1]),
str_pad($header[2], $lengths[2]),
str_pad($header[3], $lengths[3])
);
$returnString .= sprintf(
" %s %s %s %s\n",
str_repeat('-', $lengths[0] + 1),
str_repeat('-', $lengths[1] + 2),
str_repeat('-', $lengths[2] + 2),
str_repeat('-', $lengths[3] + 2)
);
foreach ($table as $row) {
$returnString .= sprintf(
" %s: [%s] [%s] [%s]\n",
str_pad($row[0], $lengths[0]),
str_pad($row[1], $lengths[1]),
str_pad($row[2], $lengths[2]),
str_pad($row[3], $lengths[3])
);
}

$returnString .= "\n\n";
}

Expand Down
34 changes: 17 additions & 17 deletions tests/Athletic/Formatters/DefaultFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function testOneClassOneMethod()
$expected = <<<EOF
testClass
Method Name Iterations Average Time Ops/second
-------- ------------ -------------- -------------
testName: [5 ] [5.0000000000000] [5.00000]
Method Name Iterations Average Time Ops/second
------------ ------------ ----------------- ------------
testName : [5 ] [5.0000000000000] [5.00000 ]
Expand Down Expand Up @@ -97,11 +97,11 @@ public function testOneClassThreeMethods()
$expected = <<<EOF
testClass
Method Name Iterations Average Time Ops/second
-------- ------------ -------------- -------------
testName: [5 ] [5.0000000000000] [5.00000]
testName: [5 ] [5.0000000000000] [5.00000]
testName: [5 ] [5.0000000000000] [5.00000]
Method Name Iterations Average Time Ops/second
------------ ------------ ----------------- ------------
testName : [5 ] [5.0000000000000] [5.00000 ]
testName : [5 ] [5.0000000000000] [5.00000 ]
testName : [5 ] [5.0000000000000] [5.00000 ]
Expand Down Expand Up @@ -143,21 +143,21 @@ public function testThreeClassOneMethod()
$expected = <<<EOF
testClass
Method Name Iterations Average Time Ops/second
-------- ------------ -------------- -------------
testName: [5 ] [5.0000000000000] [5.00000]
Method Name Iterations Average Time Ops/second
------------ ------------ ----------------- ------------
testName : [5 ] [5.0000000000000] [5.00000 ]
testClass
Method Name Iterations Average Time Ops/second
-------- ------------ -------------- -------------
testName: [5 ] [5.0000000000000] [5.00000]
Method Name Iterations Average Time Ops/second
------------ ------------ ----------------- ------------
testName : [5 ] [5.0000000000000] [5.00000 ]
testClass
Method Name Iterations Average Time Ops/second
-------- ------------ -------------- -------------
testName: [5 ] [5.0000000000000] [5.00000]
Method Name Iterations Average Time Ops/second
------------ ------------ ----------------- ------------
testName : [5 ] [5.0000000000000] [5.00000 ]
Expand Down

0 comments on commit e8228fe

Please sign in to comment.