public
function
run()
{
$methods
=
array_filter
(get_class_methods(
$this
),
array
(
$this
,
'_method_filter'
));
if
(
empty
(
$this
->subjects))
{
$this
->subjects =
array
(
'NULL'
=> NULL);
}
$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
(),
);
foreach
(
$methods
as
$method
)
{
$codebench
[
'benchmarks'
][
$method
] =
array
(
'time'
=> 0,
'memory'
=> 0);
$reflection
=
new
ReflectionMethod(get_class(
$this
),
$method
);
foreach
(
$this
->subjects
as
$subject_key
=>
$subject
)
{
$return
=
$reflection
->invoke(
$this
,
$subject
);
$token
= Profiler::start(
'codebench'
,
$method
.
$subject_key
);
for
(
$i
= 0;
$i
<
$this
->loops; ++
$i
)
{
$reflection
->invoke(
$this
,
$subject
);
}
$benchmark
= Profiler::total(
$token
);
$codebench
[
'benchmarks'
][
$method
][
'subjects'
][
$subject_key
] =
array
(
'return'
=>
$return
,
'time'
=>
$benchmark
[0],
'memory'
=>
$benchmark
[1],
);
$codebench
[
'benchmarks'
][
$method
][
'time'
] +=
$benchmark
[0];
$codebench
[
'benchmarks'
][
$method
][
'memory'
] +=
$benchmark
[1];
}
}
$fastest_method
=
$fastest_subject
=
array
(
'time'
=> 999999999,
'memory'
=> 999999999);
$slowest_method
=
$slowest_subject
=
array
(
'time'
=> 0,
'memory'
=> 0);
foreach
(
$methods
as
$method
)
{
$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
)
{
$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'
]);
}
}
foreach
(
$codebench
[
'benchmarks'
]
as
&
$method
)
{
$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);
$method
[
'grade'
][
'time'
] =
$this
->_grade(
$method
[
'percent'
][
'fastest'
][
'time'
]);
$method
[
'grade'
][
'memory'
] =
$this
->_grade(
$method
[
'percent'
][
'fastest'
][
'memory'
]);
foreach
(
$method
[
'subjects'
]
as
&
$subject
)
{
$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);
$subject
[
'grade'
][
'time'
] =
$this
->_grade(
$subject
[
'percent'
][
'fastest'
][
'time'
]);
$subject
[
'grade'
][
'memory'
] =
$this
->_grade(
$subject
[
'percent'
][
'fastest'
][
'memory'
]);
}
}
return
$codebench
;
}