Make profile and profile-thunk return the value of the profiled expression.
Closes PR14404.
This commit is contained in:
parent
4e51b1d737
commit
3a273bd2b0
|
@ -26,11 +26,12 @@ intended as a convenient tool for profiling code.
|
||||||
(profile? . -> . any/c)))
|
(profile? . -> . any/c)))
|
||||||
#f]
|
#f]
|
||||||
[#:use-errortrace? use-errortrace? any/c #f])
|
[#:use-errortrace? use-errortrace? any/c #f])
|
||||||
void?]{
|
any/c]{
|
||||||
|
|
||||||
Executes the given @racket[thunk] and collect profiling data during
|
Executes the given @racket[thunk] and collect profiling data during
|
||||||
execution, eventually analyzing and rendering this. Keyword arguments
|
execution, eventually analyzing and rendering this. Returns the value
|
||||||
can customize the profiling:
|
of the profiled expression.
|
||||||
|
Keyword arguments can customize the profiling:
|
||||||
@itemize[
|
@itemize[
|
||||||
|
|
||||||
@item{The profiler works by starting a ``sampler'' thread to
|
@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
|
@item{Once the computation is done and the sampler is stopped, the
|
||||||
accumulated data is analyzed (by @racket[analyze-samples]) and the
|
accumulated data is analyzed (by @racket[analyze-samples]) and the
|
||||||
resulting profile value is sent to the @racket[renderer] function.
|
resulting profile value is sent to the @racket[renderer] function.
|
||||||
See @secref["renderers"] for available renderers. You can also use
|
See @secref["renderers"] for available renderers.}
|
||||||
@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.}
|
|
||||||
|
|
||||||
@item{To provide feedback information during execution, specify a
|
@item{To provide feedback information during execution, specify a
|
||||||
@racket[periodic-renderer]. This should be a list holding a delay
|
@racket[periodic-renderer]. This should be a list holding a delay
|
||||||
|
|
|
@ -7,4 +7,4 @@
|
||||||
|
|
||||||
(define pkg-desc "implementation (no documentation) part of \"profile\"")
|
(define pkg-desc "implementation (no documentation) part of \"profile\"")
|
||||||
|
|
||||||
(define pkg-authors '(eli))
|
(define pkg-authors '(eli stamourv))
|
||||||
|
|
|
@ -28,17 +28,17 @@
|
||||||
(renderer (analyze-samples (sampler 'get-snapshots)))
|
(renderer (analyze-samples (sampler 'get-snapshots)))
|
||||||
(loop))
|
(loop))
|
||||||
(thread loop))))
|
(thread loop))))
|
||||||
(define (run) (for ([i (in-range rpt)]) (thunk)))
|
(define (run) (for/last ([i (in-range rpt)]) (thunk)))
|
||||||
(with-handlers ([void (λ (e) (eprintf "profiled thunk error: ~a\n"
|
(begin0 (with-handlers ([void (λ (e) (eprintf "profiled thunk error: ~a\n"
|
||||||
(if (exn? e)
|
(if (exn? e)
|
||||||
(exn-message e)
|
(exn-message e)
|
||||||
(format "~e" e))))])
|
(format "~e" e))))])
|
||||||
(if threads?
|
(if threads?
|
||||||
(parameterize ([current-custodian cust]) (run))
|
(parameterize ([current-custodian cust]) (run))
|
||||||
(run)))
|
(run)))
|
||||||
(when periodic-thread (kill-thread periodic-thread))
|
(when periodic-thread (kill-thread periodic-thread))
|
||||||
(sampler 'stop)
|
(sampler 'stop)
|
||||||
(renderer (analyze-samples (sampler 'get-snapshots))))
|
(renderer (analyze-samples (sampler 'get-snapshots)))))
|
||||||
|
|
||||||
(define-syntax (profile stx)
|
(define-syntax (profile stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
|
@ -85,3 +85,16 @@
|
||||||
(list 0.5 text:render)
|
(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"))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user