/extension/tests/xhprof_008.phpt

http://github.com/preinheimer/xhprof · Unknown · 75 lines · 63 code · 12 blank · 0 comment · 0 complexity · 36370336430b77fb238d65b392ebe736 MD5 · raw file

  1. --TEST--
  2. XHPRrof: Sampling Mode Test
  3. Author: kannan
  4. --FILE--
  5. <?php
  6. include_once dirname(__FILE__).'/common.php';
  7. function foo() {
  8. // sleep 0.8 seconds
  9. usleep(800000);
  10. }
  11. function bar() {
  12. foo();
  13. }
  14. function goo() {
  15. bar();
  16. }
  17. // call goo() once
  18. xhprof_sample_enable();
  19. goo();
  20. $output1 = xhprof_sample_disable();
  21. // call goo() twice
  22. xhprof_sample_enable();
  23. goo();
  24. goo();
  25. $output2 = xhprof_sample_disable();
  26. // how many usleep samples did we get in single call to goo()?
  27. $count1 = 0;
  28. foreach ($output1 as $sample) {
  29. if ($sample == "main()==>goo==>bar==>foo==>usleep") {
  30. $count1++;
  31. }
  32. }
  33. // how many usleep samples did we get in two calls to goo()?
  34. $count2 = 0;
  35. foreach ($output2 as $sample) {
  36. if ($sample == "main()==>goo==>bar==>foo==>usleep") {
  37. $count2++;
  38. }
  39. }
  40. //
  41. // our default sampling frequency is 0.1 seconds. So
  42. // we would expect about 8 samples (given that foo()
  43. // sleeps for 0.8 seconds). However, we might in future
  44. // allow the sampling frequency to be modified. So rather
  45. // than depend on the absolute number of samples, we'll
  46. // check to see if $count2 is roughly double of $count1.
  47. //
  48. if (($count1 == 0)
  49. || (($count2 / $count1) > 2.5)
  50. || (($count2 / $count1) < 1.5)) {
  51. echo "Test failed\n";
  52. echo "Count of usleep samples in one call to goo(): $count1\n";
  53. echo "Count of usleep samples in two calls to goo(): $count2\n";
  54. echo "Samples in one call to goo(): \n";
  55. var_dump($output1);
  56. echo "Samples in two calls to goo(): \n";
  57. var_dump($output2);
  58. } else {
  59. echo "Test passed\n";
  60. }
  61. ?>
  62. --EXPECT--
  63. Test passed