profile-check.scm 4.4 KB

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