1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors words sequences math prettyprint kernel arrays io
4 io.styles namespaces assocs kernel.private strings combinators
5 sorting math.parser vocabs definitions tools.profiler.private
6 continuations generic ;
10 [ t profiling call ] [ f profiling ] [ ] cleanup ;
12 : counters ( words -- assoc )
13 [ dup counter>> ] { } map>assoc ;
15 GENERIC: (profile.) ( obj -- )
17 TUPLE: usage-profile word ;
19 C: <usage-profile> usage-profile
22 dup unparse swap <usage-profile> write-object ;
24 TUPLE: vocab-profile vocab ;
26 C: <vocab-profile> vocab-profile
29 dup <vocab-profile> write-object ;
31 M: method-body (profile.)
32 dup synopsis swap "method-generic" word-prop
33 <usage-profile> write-object ;
35 : counter. ( obj n -- )
37 >r [ (profile.) ] with-cell r>
38 [ number>string write ] with-cell
41 : counters. ( assoc -- )
42 [ second 0 > ] filter sort-values
43 standard-table-style [
44 [ counter. ] assoc-each
48 "Call counts for all words:" print
49 all-words counters counters. ;
51 : vocab-profile. ( vocab -- )
52 "Call counts for words in the " write
53 dup dup vocab write-object
55 words counters counters. ;
57 : usage-profile. ( word -- )
58 "Call counts for words which call " write
61 smart-usage [ word? ] filter counters counters. ;
63 : vocabs-profile. ( -- )
64 "Call counts for all vocabularies:" print
67 [ "predicating" word-prop not ] filter
69 ] { } map>assoc counters. ;
71 : method-profile. ( -- )
72 all-words [ subwords ] map concat