Ticket #1827: step_profiler.patch
| File step_profiler.patch, 11.3 kB (added by robert.coup@…, 2 years ago) |
|---|
-
src/profile/StepProfiler.js
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 }; -
src/profile/StepProfiler.js
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 }; -
tests/profile/test_StepProfiler.js
1 dojo.require("dojo.profile.StepProfiler"); 2 3 function profile_StepProfiler_delaybusy(time_ms) { 4 // busy-delay since setTimeout isn't available in tests, and it's 5 // asynchronous which messes up the test logic 6 var s = (new Date()).valueOf(); 7 while ((new Date()).valueOf() - s < time_ms); 8 } 9 10 function test_StepProfiler_profiling() { 11 dojo.profile.StepProfiler.start("test0"); 12 13 profile_StepProfiler_delaybusy(100); 14 // this time also make it print "in-line" 15 dojo.profile.StepProfiler.step("test0", "step+100ms", true); 16 jum.debug("profiler should have just printed 'StepProfiler - test0.step+100ms: 100ms'"); 17 18 profile_StepProfiler_delaybusy(150); 19 dojo.profile.StepProfiler.step("test0", "step+250ms"); 20 21 profile_StepProfiler_delaybusy(50); 22 dojo.profile.StepProfiler.step("test0"); 23 24 var ar = dojo.profile.StepProfiler.toArray("test0"); 25 jum.assertEquals("step count", 4, ar.length); 26 jum.assertEquals("start-name", "start", ar[0][0]); 27 jum.assertEquals("start-time", 0, ar[0][1]); 28 jum.assertEquals("mid-name", "step+250ms", ar[2][0]); 29 jum.assertEquals("auto-name", "step_3", ar[3][0]); 30 31 // since JS timing is not so accurate... 32 var diff = ar[2][1] - 150; 33 var closeEnoughTime = ((diff >= 0) && (diff <= 15)); 34 jum.debug("time difference was: " + diff); 35 jum.assertTrue("mid-time=" + diff, closeEnoughTime); 36 37 jum.debug("testing StepProfiler print..."); 38 dojo.profile.StepProfiler.print(); 39 jum.debug("profile should have printed."); 40 } 41 42 function test_StepProfiler_reset() { 43 dojo.profile.StepProfiler.start("test1", "start"); 44 dojo.profile.StepProfiler.step("test1", "step +0"); 45 46 var ar = dojo.profile.StepProfiler.toArray("test1"); 47 jum.assertEquals("initial step count", 2, ar.length); 48 49 dojo.profile.StepProfiler.reset("test1"); 50 var ar = dojo.profile.StepProfiler.toArray("test1"); 51 jum.assertEquals("reset step count", 0, ar.length); 52 } 53 -
tests/profile/test_StepProfiler.js
1 dojo.require("dojo.profile.StepProfiler"); 2 3 function profile_StepProfiler_delaybusy(time_ms) { 4 // busy-delay since setTimeout isn't available in tests, and it's 5 // asynchronous which messes up the test logic 6 var s = (new Date()).valueOf(); 7 while ((new Date()).valueOf() - s < time_ms); 8 } 9 10 function test_StepProfiler_profiling() { 11 dojo.profile.StepProfiler.start("test0"); 12 13 profile_StepProfiler_delaybusy(100); 14 // this time also make it print "in-line" 15 dojo.profile.StepProfiler.step("test0", "step+100ms", true); 16 jum.debug("profiler should have just printed 'StepProfiler - test0.step+100ms: 100ms'"); 17 18 profile_StepProfiler_delaybusy(150); 19 dojo.profile.StepProfiler.step("test0", "step+250ms"); 20 21 profile_StepProfiler_delaybusy(50); 22 dojo.profile.StepProfiler.step("test0"); 23 24 var ar = dojo.profile.StepProfiler.toArray("test0"); 25 jum.assertEquals("step count", 4, ar.length); 26 jum.assertEquals("start-name", "start", ar[0][0]); 27 jum.assertEquals("start-time", 0, ar[0][1]); 28 jum.assertEquals("mid-name", "step+250ms", ar[2][0]); 29 jum.assertEquals("auto-name", "step_3", ar[3][0]); 30 31 // since JS timing is not so accurate... 32 var diff = ar[2][1] - 150; 33 var closeEnoughTime = ((diff >= 0) && (diff <= 15)); 34 jum.debug("time difference was: " + diff); 35 jum.assertTrue("mid-time=" + diff, closeEnoughTime); 36 37 jum.debug("testing StepProfiler print..."); 38 dojo.profile.StepProfiler.print(); 39 jum.debug("profile should have printed."); 40 } 41 42 function test_StepProfiler_reset() { 43 dojo.profile.StepProfiler.start("test1", "start"); 44 dojo.profile.StepProfiler.step("test1", "step +0"); 45 46 var ar = dojo.profile.StepProfiler.toArray("test1"); 47 jum.assertEquals("initial step count", 2, ar.length); 48 49 dojo.profile.StepProfiler.reset("test1"); 50 var ar = dojo.profile.StepProfiler.toArray("test1"); 51 jum.assertEquals("reset step count", 0, ar.length); 52 } 53