From 3a273bd2b052c1c04c60a0f6d381961c3dbced48 Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Thu, 27 Mar 2014 16:23:35 -0400 Subject: [PATCH] Make profile and profile-thunk return the value of the profiled expression. Closes PR14404. --- .../profile/scribblings/toplevel.scrbl | 14 +++----- pkgs/profile-pkgs/profile-lib/info.rkt | 2 +- pkgs/profile-pkgs/profile-lib/main.rkt | 35 +++++++++++++------ 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/pkgs/profile-pkgs/profile-doc/profile/scribblings/toplevel.scrbl b/pkgs/profile-pkgs/profile-doc/profile/scribblings/toplevel.scrbl index 15dd7d6b0b..45f13d5955 100644 --- a/pkgs/profile-pkgs/profile-doc/profile/scribblings/toplevel.scrbl +++ b/pkgs/profile-pkgs/profile-doc/profile/scribblings/toplevel.scrbl @@ -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 diff --git a/pkgs/profile-pkgs/profile-lib/info.rkt b/pkgs/profile-pkgs/profile-lib/info.rkt index 9cb70b8b80..5334dffa6e 100644 --- a/pkgs/profile-pkgs/profile-lib/info.rkt +++ b/pkgs/profile-pkgs/profile-lib/info.rkt @@ -7,4 +7,4 @@ (define pkg-desc "implementation (no documentation) part of \"profile\"") -(define pkg-authors '(eli)) +(define pkg-authors '(eli stamourv)) diff --git a/pkgs/profile-pkgs/profile-lib/main.rkt b/pkgs/profile-pkgs/profile-lib/main.rkt index a6bb5f932c..c2992e02d9 100644 --- a/pkgs/profile-pkgs/profile-lib/main.rkt +++ b/pkgs/profile-pkgs/profile-lib/main.rkt @@ -28,17 +28,17 @@ (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" - (if (exn? e) - (exn-message e) - (format "~e" e))))]) - (if threads? - (parameterize ([current-custodian cust]) (run)) - (run))) - (when periodic-thread (kill-thread periodic-thread)) - (sampler 'stop) - (renderer (analyze-samples (sampler 'get-snapshots)))) + (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))))]) + (if threads? + (parameterize ([current-custodian cust]) (run)) + (run))) + (when periodic-thread (kill-thread periodic-thread)) + (sampler 'stop) + (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"))