Make profile and profile-thunk return the value of the profiled expression.

Closes PR14404.
This commit is contained in:
Vincent St-Amour 2014-03-27 16:23:35 -04:00
parent 4e51b1d737
commit 3a273bd2b0
3 changed files with 30 additions and 21 deletions

View File

@ -26,11 +26,12 @@ intended as a convenient tool for profiling code.
(profile? . -> . any/c)))
#f]
[#:use-errortrace? use-errortrace? any/c #f])
void?]{
any/c]{
Executes the given @racket[thunk] and collect profiling data during
execution, eventually analyzing and rendering this. Keyword arguments
can customize the profiling:
execution, eventually analyzing and rendering this. Returns the value
of the profiled expression.
Keyword arguments can customize the profiling:
@itemize[
@item{The profiler works by starting a ``sampler'' thread to
@ -69,12 +70,7 @@ can customize the profiling:
@item{Once the computation is done and the sampler is stopped, the
accumulated data is analyzed (by @racket[analyze-samples]) and the
resulting profile value is sent to the @racket[renderer] function.
See @secref["renderers"] for available renderers. You can also use
@racket[values] as a ``renderer''---in this case
@racket[profile-thunk] returns the analyzed information which can
now be rendered multiple times, or saved for rendering directly
using one of the renderers, perhaps multiple times for different
views.}
See @secref["renderers"] for available renderers.}
@item{To provide feedback information during execution, specify a
@racket[periodic-renderer]. This should be a list holding a delay

View File

@ -7,4 +7,4 @@
(define pkg-desc "implementation (no documentation) part of \"profile\"")
(define pkg-authors '(eli))
(define pkg-authors '(eli stamourv))

View File

@ -28,8 +28,8 @@
(renderer (analyze-samples (sampler 'get-snapshots)))
(loop))
(thread loop))))
(define (run) (for ([i (in-range rpt)]) (thunk)))
(with-handlers ([void (λ (e) (eprintf "profiled thunk error: ~a\n"
(define (run) (for/last ([i (in-range rpt)]) (thunk)))
(begin0 (with-handlers ([void (λ (e) (eprintf "profiled thunk error: ~a\n"
(if (exn? e)
(exn-message e)
(format "~e" e))))])
@ -38,7 +38,7 @@
(run)))
(when periodic-thread (kill-thread periodic-thread))
(sampler 'stop)
(renderer (analyze-samples (sampler 'get-snapshots))))
(renderer (analyze-samples (sampler 'get-snapshots)))))
(define-syntax (profile stx)
(syntax-case stx ()
@ -85,3 +85,16 @@
(list 0.5 text:render)
)
|#
(module+ test
(require rackunit racket/string racket/list)
;; `profile' and `profile-thunk' should return the value of the
;; profiled expression
(check-equal?
(profile (for/last ([i (in-range 1000 5 -1)])
(string-join (map number->string (range i)))))
"0 1 2 3 4 5")
(check-equal?
(profile-thunk (lambda () (for/last ([i (in-range 1000 5 -1)])
(string-join (map number->string (range i))))))
"0 1 2 3 4 5"))