| 48 | * a new '''org.sophie2.system''' module will be introduced |
| 49 | * a new '''org.sophie2.system.profiler''' package will be introduced |
| 50 | * there will be two classes, '''Profiler''' (described below) and '''ProfileUtil''' which will contain of static helper methods. |
| 51 | * here's what the Profiler class will look like: |
| 52 | {{{ |
| 53 | /** |
| 54 | * A profiler class which provides a plain-text report for a given application's performance. |
| 55 | * |
| 56 | * After an instance of this profiler is started, it starts monitoring all the stack traces |
| 57 | * in all the {@link Thread}s which execute in the application. It does so by collecting |
| 58 | * statistics for the application by performing a snapshot of the application's state at a |
| 59 | * given time interval. Thus, this is a statistical profiler, not an event-based profiler |
| 60 | * which uses hooks to the JVM. |
| 61 | * |
| 62 | * The profiler can dump the report to a given {@link PrintStream} at any time by calling |
| 63 | * some of the dump*() methods. |
| 64 | * |
| 65 | * Currently, three types of reports can be produced: |
| 66 | * |
| 67 | * 1) For each {@link Thread}, a hierarchical view of the stack trace execution is provided: |
| 68 | * Each stack trace element is displayed with indent corresponding to its position in the |
| 69 | * stack trace when the snapshot was performed along with a number which is the percentage |
| 70 | * of the total snapshots done during the {@link Profiler}'s life when this element was present |
| 71 | * at this position in the stack trace of the given {@link Thread}. This number is somewhat |
| 72 | * identical to the percentage of the {@link Thread}'s execution time this particular method |
| 73 | * or a method which it invoked was running. |
| 74 | * Also, for each {@Thread}, the percentage of total runtime of the application which the |
| 75 | * {@link Thread} consumed is shown. |
| 76 | * At each line the {@link Profiler} might put a <b>*</b> which indicates that this method |
| 77 | * has spent much time executing (this excludes time spent in execution of other methods |
| 78 | * called from the current method) and thus detects a potential optimization spot. |
| 79 | * |
| 80 | * 2) All stack trace elements in all {@link Thread}s are sorted in decreasing order of their |
| 81 | * total execution time. Using this report one can spot methods which have been in the stack |
| 82 | * traces much of the time and thus optimizing them could lead to an optimization. |
| 83 | * |
| 84 | * 3) Same as above but not displaying stack trace elements but methods in all the classes used |
| 85 | * during the application's execution. Useful for similar reasons. |
| 86 | * |
| 87 | * The {@link Profiler} can be killed using the kill() method. |
| 88 | * |
| 89 | * @author gogov |
| 90 | */ |
| 91 | public class Profiler { |
| 92 | /** |
| 93 | * Constructor. |
| 94 | * @param snapshotInterval |
| 95 | * The interval at which the {@link Profiler} performs a snapshot, |
| 96 | * measured in milliseconds. |
| 97 | * @param stream |
| 98 | * The {@link PrintStream} which the {@Profiler} writes the reports to. |
| 99 | */ |
| 100 | public Profiler(int snapshotInterval, PrintStream stream); |
| 101 | |
| 102 | /** |
| 103 | * Takes a snapshot of the current application state and updates the inner statistic |
| 104 | * records appropriately. |
| 105 | */ |
| 106 | public void takeSnapshot(); |
| 107 | |
| 108 | /** |
| 109 | * Runs the {@link Profiler}. |
| 110 | */ |
| 111 | public void run(); |
| 112 | |
| 113 | /** |
| 114 | * Kills the {@link Profiler}. |
| 115 | */ |
| 116 | public void kill(); |
| 118 | |
| 119 | /** |
| 120 | * Dumps all the stack traces for all {@link Thread}s according to spec. |
| 121 | * @param hitsPercentageThreshold |
| 122 | * @param ownPercentageWarningThreshold |
| 123 | * @param depthLimit |
| 124 | */ |
| 125 | public void dumpForest(float hitsPercentageThreshold, float ownPercentageWarningThreshold, int depthLimit); |
| 126 | |
| 127 | /** |
| 128 | * Dumps all {@link StackTraceElement}s according to spec. |
| 129 | */ |
| 130 | public void dumpStackTraceElements(); |
| 131 | |
| 132 | /** |
| 133 | * Dumps all methods according to spec. |
| 134 | */ |
| 135 | public void dumpMethods(); |
| 136 | } |
| 137 | }}} |
| 138 | * All explanation of the functionality is included in the javadoc of this sample structure. |
| 139 | * A helper class which runs a given method, launching a Profiler before the method starts execution and respectively killing it and dumping a report to a file will be added in '''org.sophie2.system'''. |
| 140 | * The ''Debug'' launch configurations for Author and Reader will be renamed to ''Profiler'' due to milo's request (: |
| 141 | * They will be altered to run helper clases which run FakeAuthorMain and FakeReaderMain's main() methods and dump a report. |
| 142 | * Besides running and showing the report, there's no deterministic test for this task because the stack traces of the execution threads are not deterministic so I'm not including a unit test because it's kind of unwriteable at this moment (: |
| 143 | * There wasn't enough time to optimize particular pieces of client code in Sophie2 so this is left for the next revision. |