PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/atk4/lib/Page/Tester.php

https://github.com/git86/todo
PHP | 101 lines | 68 code | 16 blank | 17 comment | 7 complexity | c9c133e4fbd53c9ca22886e837aacd43 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. class Page_Tester extends Page {
  3. public $variances=array();
  4. public $input;
  5. function setVariance($arr){
  6. $this->variances=$arr;
  7. foreach($arr as $key=>$item){
  8. if(is_numeric($key))$key=$item;
  9. $this->grid->addColumn('html',$key.'_inf',$key.' info');
  10. $this->grid->addColumn('text,wrap',$key.'_res',$key.' result');
  11. }
  12. }
  13. function init(){
  14. parent::init();
  15. $this->grid=$this->add('Grid');
  16. $this->grid->addColumn('text','name');
  17. //$this->setVariance(array('GiTemplate','SMlite'));
  18. $this->setVariance(array('Test'));
  19. $this->runTests();
  20. }
  21. function runTests($test_obj=null){
  22. if(!$test_obj)$test_obj=$this;
  23. $tested=array();
  24. $data=array();
  25. foreach(get_class_methods($test_obj) as $method){
  26. $m='';
  27. if(substr($method,0,5)=='test_'){
  28. $m=substr($method,5);
  29. }elseif(substr($method,0,8)=='prepare_'){
  30. $m=substr($method,8);
  31. }else continue;
  32. // Do not retest same function even if it has both prepare and test
  33. if($tested[$m])continue;$tested[$m]=true;
  34. // Row contains test result data
  35. $row=array('name'=>$m,'id'=>$m);
  36. foreach($this->variances as $key=>$vari){
  37. if(is_numeric($key))$key=$vari;
  38. // Input is a result of preparation function
  39. if(method_exists($test_obj,'prepare_'.$m)){
  40. $input=$test_obj->{'prepare_'.$m}($vari,$method);
  41. }else{
  42. $input=$test_obj->prepare($vari,$method);
  43. }
  44. $this->input=$input;
  45. $test_func=method_exists($test_obj,'test_'.$m)?
  46. 'test_'.$m:'test';
  47. // Test speed
  48. $me=memory_get_peak_usage();
  49. $ms=microtime(true);
  50. /*
  51. $limit=20;$hl=round($limit /2);
  52. for($i=0;$i<$limit;$i++){
  53. //$result=call_user_func_array(array($test_obj,$test_func),$input);
  54. */
  55. $result=(string)$test_obj->$test_func($input[0],$input[1]);
  56. //$this->$method($vari);
  57. /*
  58. if($i==$hl){
  59. $meh=memory_get_peak_usage();
  60. }
  61. */
  62. //}
  63. $ms=microtime(true)-$ms;
  64. $me=($mend=memory_get_peak_usage())-$me;
  65. $row[$key.'_inf']='Speed: '.round($ms,3).'<br/>Memory: '.$me;
  66. $this->formatResult($row,$key,$result);
  67. }
  68. $data[]=$row;
  69. }
  70. $this->grid->setStaticSource($data);
  71. }
  72. function formatResult(&$row,$key,$result){
  73. $row[$key.'_res']=$result;
  74. }
  75. function expect($value,$expectation){
  76. return $value==$expectation?'OK':'ERR';
  77. }
  78. function _prepare($t,$str){
  79. $result='';
  80. for($i=0;$i<100;$i++){
  81. $result.=$str;
  82. }
  83. return array($this->add($t),$result);
  84. }
  85. }