OLD | NEW |
1 part of debug; | 1 part of petitparser.debug; |
2 | 2 |
3 /** | 3 /// Returns a transformed [parser] that when being used measures |
4 * Returns a transformed [parser] that when being used measures | 4 /// the activation count and total time of each parser. |
5 * the activation count and total time of each parser. | 5 /// |
6 * | 6 /// For example, the snippet |
7 * For example, the snippet | 7 /// |
8 * | 8 /// var parser = letter() & word().star(); |
9 * var parser = letter() & word().star(); | 9 /// profile(parser).parse('f1234567890'); |
10 * profile(parser).parse('f1234567890'); | 10 /// |
11 * | 11 /// produces the following output: |
12 * produces the following output: | 12 /// |
13 * | 13 /// 1 2006 Instance of 'SequenceParser' |
14 * 1 2006 Instance of 'SequenceParser' | 14 /// 1 697 Instance of 'PossessiveRepeatingParser'[0..*] |
15 * 1 697 Instance of 'PossessiveRepeatingParser'[0..*] | 15 /// 11 406 Instance of 'CharacterParser'[letter or digit expected] |
16 * 11 406 Instance of 'CharacterParser'[letter or digit expected] | 16 /// 1 947 Instance of 'CharacterParser'[letter expected] |
17 * 1 947 Instance of 'CharacterParser'[letter expected] | 17 /// |
18 * | 18 /// The first number refers to the number of activations of each parser, and |
19 * The first number refers to the number of activations of each parser, and | 19 /// the second number is the microseconds spent in this parser and all its |
20 * the second number is the microseconds spent in this parser and all its | 20 /// children. |
21 * children. | |
22 */ | |
23 Parser profile(Parser root, [OutputHandler output = print]) { | 21 Parser profile(Parser root, [OutputHandler output = print]) { |
24 var count = new Map(); | 22 var count = new Map(); |
25 var watch = new Map(); | 23 var watch = new Map(); |
26 var parsers = new List(); | 24 var parsers = new List(); |
27 return new ContinuationParser(transformParser(root, (parser) { | 25 return new ContinuationParser(transformParser(root, (parser) { |
28 parsers.add(parser); | 26 parsers.add(parser); |
29 return new ContinuationParser(parser, (continuation, context) { | 27 return new ContinuationParser(parser, (continuation, context) { |
30 count[parser]++; | 28 count[parser]++; |
31 watch[parser].start(); | 29 watch[parser].start(); |
32 var result = continuation(context); | 30 var result = continuation(context); |
33 watch[parser].stop(); | 31 watch[parser].stop(); |
34 return result; | 32 return result; |
35 }); | 33 }); |
36 }), (continuation, context) { | 34 }), (continuation, context) { |
37 parsers.forEach((parser) { | 35 parsers.forEach((parser) { |
38 count[parser] = 0; | 36 count[parser] = 0; |
39 watch[parser] = new Stopwatch(); | 37 watch[parser] = new Stopwatch(); |
40 }); | 38 }); |
41 var result = continuation(context); | 39 var result = continuation(context); |
42 parsers.forEach((parser) { | 40 parsers.forEach((parser) { |
43 output('${count[parser]}\t' | 41 output('${count[parser]}\t' |
44 '${watch[parser].elapsedMicroseconds}\t' | 42 '${watch[parser].elapsedMicroseconds}\t' |
45 '${parser}'); | 43 '$parser'); |
46 }); | 44 }); |
47 return result; | 45 return result; |
48 }); | 46 }); |
49 } | 47 } |
OLD | NEW |