f99fcf62c1429e058233c8b3e49fe7359281ef92.svn-base 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // $Rev: 179 $
  3. error_reporting(E_ALL);
  4. include_once('../simple_html_dom.php');
  5. // -----------------------------------------------------------------------------
  6. function dump_memory($init_size) {
  7. $peak = number_format(memory_get_peak_usage()/1024, 0, '.', ',');
  8. $curr = number_format(memory_get_usage()/1024, 0, '.', ',');
  9. $diff = $curr - $init_size;
  10. echo 'peak: ' . $peak . ' kb, end: ' . $curr . ' kb, add: ' . $diff . " kb<br>";
  11. }
  12. // -----------------------------------------------------------------------------
  13. $filename = './html/google.htm';
  14. //$filename = 'test.htm';
  15. // -----------------------------------------------------------------------------
  16. // test_load_file_memory
  17. function test_load_file_memory($filename, $init_size) {
  18. echo '[load file] init memory: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br>';
  19. echo '--------------------------------------------------------------------<br>';
  20. flush();
  21. for($i=0; $i<3; ++$i) {
  22. $str = file_get_contents($filename);
  23. dump_memory($init_size);
  24. unset($str);
  25. }
  26. echo 'after loop: '.number_format(memory_get_usage(), 0, '.', ',').'<br>';
  27. echo '--------------------------------------------------------------------<br>';
  28. flush();
  29. }
  30. // -----------------------------------------------------------------------------
  31. // test_multi_objects_str_get_html
  32. function test_multi_str_get_html($filename, $init_size) {
  33. global $__g_node_mgr;
  34. $str = file_get_contents($filename);
  35. echo '[str_get_html] init memory: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br>';
  36. echo '--------------------------------------------------------------------<br>';
  37. flush();
  38. for($i=0; $i<3; ++$i) {
  39. $html = str_get_html($str);
  40. dump_memory($init_size);
  41. flush();
  42. }
  43. echo 'after loop: '.number_format(memory_get_usage(), 0, '.', ',').'<br>';
  44. echo '--------------------------------------------------------------------<br>';
  45. flush();
  46. unset($str);
  47. }
  48. // -----------------------------------------------------------------------------
  49. // test_multi_file_get_html
  50. function test_multi_file_get_html($filename, $init_size) {
  51. echo '[file_get_html] init memory: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br>';
  52. echo '--------------------------------------------------------------------<br>';
  53. flush();
  54. for($i=0; $i<3; ++$i) {
  55. $html = file_get_html($filename);
  56. //$html->clear();
  57. unset($html);
  58. dump_memory($init_size);
  59. flush();
  60. }
  61. echo 'after loop: '.number_format(memory_get_usage(), 0, '.', ',').'<br>';
  62. echo '--------------------------------------------------------------------<br>';
  63. flush();
  64. }
  65. /*
  66. // -----------------------------------------------------------------------------
  67. // test_multi_objects_clear_memory
  68. function test_multi_objects_file_get_html_clear_memory($filename) {
  69. echo '<br><br>[one object]<br>init memory: '.number_format(memory_get_usage(), 0, '.', ',').'<br>';
  70. echo '------------------------------------------<br>';
  71. flush();
  72. $html = new simple_html_dom;
  73. for($i=0; $i<3; ++$i) {
  74. $html->load_file($filename);
  75. $html->clear();
  76. dump_memory();
  77. }
  78. unset($dom);
  79. echo 'final: '.number_format(memory_get_usage(), 0, '.', ',').'<br>';
  80. flush();
  81. echo '<br><br>[multi objects without clear memory]<br>init memory: '.number_format(memory_get_usage(), 0, '.', ',').'<br>';
  82. echo '------------------------------------------<br>';
  83. flush();
  84. for($i=0; $i<3; ++$i) {
  85. $html = file_get_html($filename);
  86. dump_memory();
  87. }
  88. echo 'final: '.number_format(memory_get_usage(), 0, '.', ',').'<br>';
  89. flush();
  90. */
  91. // -----------------------------------------------------------------------------
  92. // begin test
  93. $init_size = number_format(memory_get_usage(), 0, '.', ',');
  94. echo 'init ' . $init_size . " bytes<br>";
  95. flush();
  96. echo '<br>before function: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br>';
  97. test_load_file_memory($filename, $init_size);
  98. echo 'after function: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br><br>';
  99. flush();
  100. echo '<br>before function: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br>';
  101. test_multi_file_get_html($filename, $init_size);
  102. echo 'after function: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br><br>';
  103. flush();
  104. echo '<br>before function: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br>';
  105. test_multi_file_get_html($filename, $init_size);
  106. echo 'after function: '.number_format(memory_get_usage()/1024, 0, '.', ',').'<br><br>';
  107. flush();
  108. ?>