-
Notifications
You must be signed in to change notification settings - Fork 122
Instrumentation infrastructure #303
base: develop
Are you sure you want to change the base?
Changes from all commits
0e289bf
eeebdad
9329031
294444b
362962b
972bdde
e56cc5f
038240d
e40694d
90af92a
34280d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| /* | ||
| /* | ||
| Copyright (c) 2016 Ermiya Eskandary & Théophile Cailliau and other contributors | ||
| This Source Code Form is subject to the terms of the Mozilla Public | ||
| License, v. 2.0. If a copy of the MPL was not distributed with this | ||
|
|
@@ -60,6 +60,31 @@ window.log = function() { | |
| } | ||
| }; | ||
|
|
||
| // Add standard deviation function to array prototype | ||
| Array.prototype.stats = function() { | ||
| var i; | ||
| var j; | ||
| var total = 0; | ||
| var mean = 0; | ||
| var diffSqredArr = []; | ||
| for (i = 0; i < this.length; i += 1) { | ||
| total += this[i]; | ||
| } | ||
| mean = total / this.length; | ||
| for (j = 0; j < this.length; j += 1) { | ||
| diffSqredArr.push(Math.pow((this[j] - mean), 2)); | ||
| } | ||
| return { | ||
| mean: mean, | ||
| stdev: (Math.sqrt(diffSqredArr.reduce( | ||
| function(firstEl, nextEl) { return firstEl + nextEl; } | ||
| ) / this.length)), | ||
| min: Math.min.apply(null, this), | ||
| max: Math.max.apply(null, this) | ||
| }; | ||
| }; | ||
|
|
||
|
|
||
| var canvasUtil = window.canvasUtil = (function() { | ||
| return { | ||
| // Ratio of screen size divided by canvas size. | ||
|
|
@@ -357,6 +382,42 @@ var canvasUtil = window.canvasUtil = (function() { | |
| }; | ||
| })(); | ||
|
|
||
| var logUtil = window.logUtil = (function() { | ||
| return { | ||
| functionData: [], | ||
| startTimes: [], | ||
|
|
||
| startTime: function(functionName) { | ||
| logUtil.startTimes[functionName] = performance.now(); | ||
| }, | ||
|
|
||
| endTime: function(functionName) { | ||
| // No sense recording end time if start wasn't called. | ||
| if (!(functionName in logUtil.startTimes)) { | ||
| window.log('logUtil.endTime called for "' + functionName + '" without start'); | ||
| return; | ||
| } | ||
| var duration = performance.now() - logUtil.startTimes[functionName]; | ||
|
|
||
| if (!(functionName in logUtil.functionData)) { | ||
| logUtil.functionData[functionName] = []; | ||
| } | ||
| if (logUtil.functionData[functionName].push(duration) > window.bot.opt.logFrames) { | ||
| logUtil.functionData[functionName].shift(); | ||
| } | ||
| }, | ||
|
|
||
| functionStats: function(functionName) { | ||
| if (functionName in logUtil.functionData) { | ||
| return logUtil.functionData[functionName].stats(); | ||
| } else { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unexpected 'else' after 'return'. |
||
| window.log('No function data for ' + functionName); | ||
| return false; | ||
| } | ||
| } | ||
| }; | ||
| })(); | ||
|
|
||
| var bot = window.bot = (function() { | ||
| return { | ||
| isBotRunning: false, | ||
|
|
@@ -386,7 +447,8 @@ var bot = window.bot = (function() { | |
| rearHeadAngle: 3 * Math.PI / 4, | ||
| rearHeadDir: Math.PI / 2, | ||
| radiusApproachSize: 5, | ||
| radiusAvoidSize: 25 | ||
| radiusAvoidSize: 25, | ||
| logFrames: 200 | ||
| }, | ||
| MID_X: 0, | ||
| MID_Y: 0, | ||
|
|
@@ -844,7 +906,8 @@ var bot = window.bot = (function() { | |
| window.snake.xx - (bot.sectorBoxSide / 2), | ||
| window.snake.yy - (bot.sectorBoxSide / 2), | ||
| bot.sectorBoxSide, bot.sectorBoxSide); | ||
| // if (window.visualDebugging) canvasUtil.drawRect(bot.sectorBox, '#c0c0c0', true, 0.1); | ||
| // if (window.visualDebugging) | ||
| // canvasUtil.drawRect(bot.sectorBox, '#c0c0c0', true, 0.1); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to delete this if it's unused. |
||
|
|
||
| bot.cos = Math.cos(window.snake.ang); | ||
| bot.sin = Math.sin(window.snake.ang); | ||
|
|
@@ -899,7 +962,9 @@ var bot = window.bot = (function() { | |
| foodTimer: function() { | ||
| if (window.playing && bot.lookForFood && | ||
| window.snake !== null && window.snake.alive_amt === 1) { | ||
| logUtil.startTime('cfg'); | ||
| bot.computeFoodGoal(); | ||
| logUtil.endTime('cfg'); | ||
| window.goalCoordinates = bot.currentFood; | ||
| canvasUtil.setMouseCoordinates(canvasUtil.mapToMouse(window.goalCoordinates)); | ||
| } | ||
|
|
@@ -1229,10 +1294,14 @@ var userInterface = window.userInterface = (function() { | |
| median = Math.round((bot.scores[Math.floor((bot.scores.length - 1) / 2)] + | ||
| bot.scores[Math.ceil((bot.scores.length - 1) / 2)]) / 2); | ||
|
|
||
| var stats = bot.scores.stats(); | ||
|
|
||
| avg = Math.round(stats.mean); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'avg' is not defined.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
| stdev = Math.round(stats.stdev); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'stdev' is not defined.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here... |
||
|
|
||
| oContent.push('games played: ' + bot.scores.length); | ||
| oContent.push('a: ' + Math.round( | ||
| bot.scores.reduce(function(a, b) { return a + b; }) / (bot.scores.length)) + | ||
| ' m: ' + median); | ||
| oContent.push('avg: ' + avg + ' stdev: ' + stdev + '<br/>' + | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'avg' is not defined. |
||
| 'med: ' + median); | ||
|
|
||
| for (var i = 0; i < bot.scores.length && i < 10; i++) { | ||
| oContent.push(i + 1 + '. ' + bot.scores[i]); | ||
|
|
@@ -1292,6 +1361,15 @@ var userInterface = window.userInterface = (function() { | |
| userInterface.overlays.serverOverlay.innerHTML = | ||
| window.bso.ip + ':' + window.bso.po; | ||
| } | ||
|
|
||
| if (window.logUtil !== undefined) { | ||
| var cfg = window.logUtil.functionStats('cfg'); | ||
|
|
||
| if (cfg) { | ||
| oContent.push('cfg: μ' + Math.round(cfg.mean) + ' ∧' + | ||
| Math.round(cfg.min) + ' ∨' + Math.round(cfg.max)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| userInterface.overlays.botOverlay.innerHTML = oContent.join('<br/>'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array prototype is read only, properties should not be added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, what do you think of this?