profile-check.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Profiler tests
  3. (define-test-suite profiler-tests)
  4. (define (a-loop y)
  5. (+ 1
  6. (let loop ((i y))
  7. (if (> i 0)
  8. (loop (- i 1))
  9. 0))))
  10. (define (main-simple-loop x)
  11. (+ 1
  12. (a-loop x)
  13. (a-loop x)))
  14. (define (a-rec y)
  15. (+ 1
  16. (if (> y 0)
  17. (a-rec (- y 1))
  18. 0)))
  19. (define (main-rec x)
  20. (+ 1
  21. (a-rec x)
  22. (a-rec x)))
  23. (define (c-mutual x)
  24. (+ 1 x))
  25. (define (b-mutual x)
  26. (let ((y (- x 1)))
  27. (if (> y 0)
  28. (begin
  29. (a-mutual y)
  30. (c-mutual (* 2 y))
  31. (+ 1 (a-mutual y)))
  32. 0)))
  33. (define (a-mutual x)
  34. (let ((y (- x 1)))
  35. (if (> y 0)
  36. (begin
  37. (b-mutual y)
  38. (c-mutual y)
  39. (+ 1 (b-mutual y)))
  40. 0)))
  41. (define (main-mutual x)
  42. (+ 1
  43. (a-mutual x)
  44. (a-mutual x)))
  45. (define (a-exitcont cont x)
  46. (let ((y (- x 1)))
  47. (if (> y 0)
  48. (begin
  49. (check-exception
  50. (profile-thunk (make-empty-profile-data) (lambda () (main-exitcont 10))))
  51. (cont 0))
  52. 0)))
  53. (define (main-exitcont cont x)
  54. (+ 1 (a-exitcont cont x)))
  55. (define-test-case simple-loop profiler-tests
  56. (let ((prof-data (make-empty-profile-data))
  57. (blackhole (make-string-output-port)))
  58. ;; let it run
  59. (profile-thunk prof-data (lambda () (main-simple-loop 5000)) 1)
  60. (test-stability prof-data "a-loop")
  61. (check (profile-function-calls prof-data '("a-loop"))
  62. => 2)
  63. (check (profile-function-calls prof-data '("loop" "a-loop"))
  64. => 10002)
  65. (check (profile-function-reccalls prof-data '("loop"))
  66. => 0) ; tail calls, this could fail when profiler enhances :(
  67. (check (profile-function-nonreccalls prof-data '("a-loop"))
  68. => 2)
  69. ))
  70. (define-test-case recursive profiler-tests
  71. (let ((prof-data (make-empty-profile-data))
  72. (blackhole (make-string-output-port)))
  73. ;; let it run
  74. (profile-thunk prof-data (lambda () (main-rec 500)) 50)
  75. (test-stability prof-data "a-rec")
  76. (check (profile-function-calls prof-data '("a-rec"))
  77. => 1002)
  78. (check (profile-function-calls prof-data '("main-rec"))
  79. => 1)
  80. (check (profile-function-reccalls prof-data '("a-rec" "profiler-test"))
  81. => 1000)
  82. (check (profile-function-nonreccalls prof-data '("a-rec"))
  83. => 2)
  84. ))
  85. (define-test-case mutual profiler-tests
  86. (let ((prof-data (make-empty-profile-data))
  87. (blackhole (make-string-output-port)))
  88. ;; let it run
  89. (profile-thunk prof-data (lambda () (main-mutual 15)) 50 #f)
  90. (test-stability prof-data "a-mutual")
  91. (check (profile-function-calls prof-data '("a-mutual"))
  92. => 43690)
  93. (check (profile-function-calls prof-data '("main-mutual"))
  94. => 1)
  95. (check (profile-function-reccalls prof-data '("a-mutual" "profiler-test"))
  96. => 0)
  97. (check (profile-function-nonreccalls prof-data '("a-mutual"))
  98. => 43690)
  99. ))
  100. (define-test-case exitcont profiler-tests
  101. (let ((prof-data (make-empty-profile-data))
  102. (blackhole (make-string-output-port)))
  103. ;; let it run
  104. (call-with-current-continuation
  105. (lambda (cont)
  106. (profile-thunk prof-data (lambda () (main-exitcont cont 22)) 50 #t)))
  107. (test-stability prof-data "a-exitcont")
  108. (check (profile-function-calls prof-data '("a-exitcont"))
  109. => 1)
  110. (check (profile-function-calls prof-data '("main-exitcont"))
  111. => 1)
  112. (check (profile-function-reccalls prof-data '("a-exitcont" "profiler-test"))
  113. => 0)
  114. (check (profile-function-nonreccalls prof-data '("a-exitcont"))
  115. => 1)
  116. ))
  117. ;; non-deterministic, but should at least not crash...
  118. (define (test-stability prof-data funcname)
  119. (let ((blackhole (make-string-output-port)))
  120. (profile-display prof-data blackhole)
  121. (profile-display-overview prof-data blackhole)
  122. (profile-display-flat prof-data blackhole)
  123. (profile-display-tree prof-data blackhole)
  124. (profile-data-starttime prof-data)
  125. (profile-data-endtime prof-data)
  126. (profile-data-memoryuse prof-data)
  127. (profile-data-gcruns prof-data)
  128. (profile-data-runtime prof-data)
  129. (profile-data-samples prof-data)
  130. (profile-data-templates prof-data)
  131. (profile-data-cycles prof-data)
  132. (profile-data-root prof-data)
  133. (profile-display-function-flat prof-data '(funcname) blackhole)
  134. (profile-display-function-tree prof-data '(funcname "profiler-test" blackhole))
  135. (profile-display-function-tree prof-data "profiler-test" blackhole)
  136. (profile-display-function-cycle prof-data '(funcname) blackhole)
  137. ))