| | 1 | dojo.provide("dojo.profile.StepProfiler"); |
| | 2 | |
| | 3 | // summary: This is a simple profiler for measuring the time in milliseconds |
| | 4 | // between steps in a process. |
| | 5 | // example: |
| | 6 | // dojo.profile.StepProfiler.start("my"); |
| | 7 | // ... activity ... |
| | 8 | // dojo.profile.StepProfiler.step("my", "mid-point"); |
| | 9 | // ... activity ... |
| | 10 | // dojo.profile.StepProfiler.step("my", "end"); |
| | 11 | // dojo.profile.StepProfiler.print(); |
| | 12 | // |
| | 13 | // output via dojo.debug: |
| | 14 | // StepProfiler - my.start: |
| | 15 | // StepProfiler - my.mid-point: 32ms |
| | 16 | // StepProfiler - my.end: 13ms |
| | 17 | // |
| | 18 | |
| | 19 | dojo.profile.StepProfiler = new function() { |
| | 20 | var _profiles = {}; |
| | 21 | |
| | 22 | this.step = function(profile, step, print) { |
| | 23 | // summary: add a profile step. This will create a profile if the profile |
| | 24 | // name does not exist. |
| | 25 | // profile: string profile name to record against |
| | 26 | // step: string? name for this step. Default is step_[count] (eg. 'step_1'). |
| | 27 | // print: boolean? whether to print the time since the last step |
| | 28 | // immediately. Default is false. |
| | 29 | var p = _profiles[profile]; |
| | 30 | if (!p) { |
| | 31 | p = []; |
| | 32 | _profiles[profile] = p; |
| | 33 | } |
| | 34 | if (!step) { |
| | 35 | var step = "step_" + p.length; |
| | 36 | } |
| | 37 | p.push([step, (new Date()).valueOf()]); |
| | 38 | if (print) { |
| | 39 | dojo.debug("StepProfiler - " + profile + "." + step + ": " + this._getStepTime(p, p.length-1, true)); |
| | 40 | } |
| | 41 | } |
| | 42 | |
| | 43 | this.start = function(profile, step, print) { |
| | 44 | // summary: resets the profiler and adds an initial step. This will |
| | 45 | // create a profile if the profile name does not exist. |
| | 46 | // profile: string profile name to record against |
| | 47 | // step: string? name for this step. Default is 'start'. |
| | 48 | // print: boolean? whether to print the time since the last step |
| | 49 | // immediately. Default is false. |
| | 50 | this.reset(profile); |
| | 51 | if (!step) |
| | 52 | var step = "start"; |
| | 53 | this.step(profile, step, print); |
| | 54 | } |
| | 55 | |
| | 56 | this._getStepTime = function(profile, index, asText) { |
| | 57 | // summary: return the time between two steps in a profile, in milliseconds |
| | 58 | // profile: profile array a profile |
| | 59 | // index: index to get the step time for |
| | 60 | // asText: boolean return a string or a number |
| | 61 | if (asText) { |
| | 62 | return (index > 0) ? (profile[index][1] - profile[index-1][1])+"ms" : ""; |
| | 63 | } else { |
| | 64 | return (index > 0) ? (profile[index][1] - profile[index-1][1]) : 0; |
| | 65 | } |
| | 66 | } |
| | 67 | |
| | 68 | this.print = function(profile) { |
| | 69 | // summary: print the results for a profile using dojo.debug |
| | 70 | // profile: string profile name to print |
| | 71 | var p = _profiles[profile]; |
| | 72 | if (!p) |
| | 73 | return; |
| | 74 | |
| | 75 | dojo.debug("StepProfiler - " + profile + ":"); |
| | 76 | for (var i=0; i<p.length; i++) { |
| | 77 | dojo.debug(" " + p[i][0] + ": " + this._getStepTime(p, i, true)); |
| | 78 | } |
| | 79 | } |
| | 80 | |
| | 81 | this.toArray = function(profile) { |
| | 82 | // summary: return the profile results as an array |
| | 83 | // profile: string profile name to return |
| | 84 | // returns: profile results in an array [[stepname,steptime_ms], [stepname,steptime_ms], ...] |
| | 85 | var p = _profiles[profile]; |
| | 86 | if (!p) |
| | 87 | return []; |
| | 88 | else { |
| | 89 | var res = []; |
| | 90 | for (var i=0; i<p.length; i++) { |
| | 91 | res.push([p[i][0], this._getStepTime(p, i, false)]); |
| | 92 | } |
| | 93 | return res; |
| | 94 | } |
| | 95 | } |
| | 96 | |
| | 97 | this.reset = function(profile) { |
| | 98 | // summary: clear the steps for a profile |
| | 99 | // profile: string profile name |
| | 100 | _profiles[profile] = []; |
| | 101 | } |
| | 102 | }; |