From 0545f86dc4dde6ffe5f624665ea0125eaa369e81 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Mon, 24 Feb 2014 23:40:07 +1100 Subject: [PATCH 1/5] Add benchmark hooks for calculating document distance travelled --- .../js/AlignCurrentSelectionJustified.js | 8 +- programs/benchmark/js/Benchmark.js | 24 +-- programs/benchmark/js/BenchmarkAction.js | 5 +- programs/benchmark/js/BoldCurrentSelection.js | 10 +- programs/benchmark/js/EnterEditMode.js | 11 +- programs/benchmark/js/InsertLetterA.js | 9 +- programs/benchmark/js/MoveCursorLeft.js | 9 +- programs/benchmark/js/MoveCursorToEnd.js | 11 +- .../benchmark/js/MoveCursorToEndDirect.js | 12 +- programs/benchmark/js/MoveCursorToStart.js | 9 +- programs/benchmark/js/OdfBenchmarkContext.js | 139 ++++++++++++++++++ programs/benchmark/js/OpenDocument.js | 9 +- programs/benchmark/js/PreloadDocument.js | 5 +- programs/benchmark/js/RangeCalculator.js | 77 ++++++++++ .../benchmark/js/RemoveCurrentSelection.js | 8 +- programs/benchmark/js/RemovePositions.js | 10 +- programs/benchmark/js/SelectEntireDocument.js | 10 +- 17 files changed, 300 insertions(+), 66 deletions(-) create mode 100644 programs/benchmark/js/OdfBenchmarkContext.js create mode 100644 programs/benchmark/js/RangeCalculator.js diff --git a/programs/benchmark/js/AlignCurrentSelectionJustified.js b/programs/benchmark/js/AlignCurrentSelectionJustified.js index b9f849c9..eb9fb162 100644 --- a/programs/benchmark/js/AlignCurrentSelectionJustified.js +++ b/programs/benchmark/js/AlignCurrentSelectionJustified.js @@ -51,12 +51,14 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context * @return {undefined} */ - this.start = function(sharedState) { + this.start = function(context) { + context.recordDistanceFromCurrentSelection(state); action.start(); - sharedState.sessionController.getDirectFormattingController().alignParagraphJustified(); + context.sessionController.getDirectFormattingController().alignParagraphJustified(); + action.stop(); action.complete(true); } } diff --git a/programs/benchmark/js/Benchmark.js b/programs/benchmark/js/Benchmark.js index 05a37026..999d6229 100644 --- a/programs/benchmark/js/Benchmark.js +++ b/programs/benchmark/js/Benchmark.js @@ -36,25 +36,9 @@ * @source: https://github.com/kogmbh/WebODF/ */ -define([], function() { +define(["OdfBenchmarkContext"], function(OdfBenchmarkContext) { "use strict"; - /** - * @constructor - * @struct - */ - function SharedState() { - /** - * @type {!odf.OdfCanvas} - */ - this.odfCanvas = null; - - /** - * @type {!gui.SessionController} - */ - this.sessionController = null; - } - runtime.loadClass("core.EventNotifier"); runtime.loadClass("odf.OdfCanvas"); @@ -64,7 +48,7 @@ define([], function() { function Benchmark() { var self = this, canvasElement = document.getElementById("canvas"), - sharedState = new SharedState(), + context = new OdfBenchmarkContext(), events = new core.EventNotifier(["start", "complete"]), currentActionIndex, lastProfileAction; @@ -79,7 +63,7 @@ define([], function() { if (currentAction) { lastProfileAction = currentAction.state.description; console.profile(lastProfileAction); - currentAction.start(sharedState); + currentAction.start(context); } else { events.emit("complete", {}); } @@ -96,7 +80,7 @@ define([], function() { }); events.emit("start", {}); - sharedState.odfCanvas = new odf.OdfCanvas(canvasElement); + context.odfCanvas = new odf.OdfCanvas(canvasElement); currentActionIndex = -1; executeNextAction(); }; diff --git a/programs/benchmark/js/BenchmarkAction.js b/programs/benchmark/js/BenchmarkAction.js index 87c39399..55255068 100644 --- a/programs/benchmark/js/BenchmarkAction.js +++ b/programs/benchmark/js/BenchmarkAction.js @@ -57,11 +57,14 @@ define(function() { startedAt = new Date().getTime(); }; + this.stop = function() { + state.elapsedTime = new Date().getTime() - startedAt; + }; + /** * @param {!boolean} result */ this.complete = function(result) { - state.elapsedTime = new Date().getTime() - startedAt; state.status = result; events.emit("complete", state); }; diff --git a/programs/benchmark/js/BoldCurrentSelection.js b/programs/benchmark/js/BoldCurrentSelection.js index 7436d1b8..dfc43a61 100644 --- a/programs/benchmark/js/BoldCurrentSelection.js +++ b/programs/benchmark/js/BoldCurrentSelection.js @@ -51,13 +51,15 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { + context.recordDistanceFromCurrentSelection(state); action.start(); - sharedState.sessionController.getDirectFormattingController().setBold(true); + context.sessionController.getDirectFormattingController().setBold(true); + action.stop(); action.complete(true); - } + }; } return BoldCurrentSelection; diff --git a/programs/benchmark/js/EnterEditMode.js b/programs/benchmark/js/EnterEditMode.js index 4a2ab76e..85e3e02b 100644 --- a/programs/benchmark/js/EnterEditMode.js +++ b/programs/benchmark/js/EnterEditMode.js @@ -71,9 +71,9 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { var session, sessionController, shadowCursor, @@ -83,7 +83,7 @@ define(["BenchmarkAction"], function(BenchmarkAction) { action.start(); - session = new ops.Session(sharedState.odfCanvas); + session = new ops.Session(context.odfCanvas); shadowCursor = new gui.ShadowCursor(session.getOdtDocument()); sessionController = new gui.SessionController(session, localMemberId, shadowCursor, sessionControllerOptions); sessionController.setUndoManager(undoManager); @@ -104,8 +104,11 @@ define(["BenchmarkAction"], function(BenchmarkAction) { sessionController.insertLocalCursor(); sessionController.startEditing(); - sharedState.sessionController = sessionController; + context.session = session; + context.sessionController = sessionController; + + action.stop(); action.complete(true); }; } diff --git a/programs/benchmark/js/InsertLetterA.js b/programs/benchmark/js/InsertLetterA.js index c8eea5f0..3d7ccead 100644 --- a/programs/benchmark/js/InsertLetterA.js +++ b/programs/benchmark/js/InsertLetterA.js @@ -52,14 +52,17 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { var count; + context.storeCurrentPosition(state); action.start(); for (count = 0; count < times; count += 1) { - sharedState.sessionController.getTextController().insertText('A'); + context.sessionController.getTextController().insertText('A'); } + action.stop(); + context.recordDistanceFromPreviousPosition(state); action.complete(true); } } diff --git a/programs/benchmark/js/MoveCursorLeft.js b/programs/benchmark/js/MoveCursorLeft.js index 1916d3ac..1c5bfbc9 100644 --- a/programs/benchmark/js/MoveCursorLeft.js +++ b/programs/benchmark/js/MoveCursorLeft.js @@ -52,14 +52,17 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { var count; + context.storeCurrentPosition(state); action.start(); for (count = 0; count < steps; count += 1) { - sharedState.sessionController.getSelectionController().moveCursorToLeft(); + context.sessionController.getSelectionController().moveCursorToLeft(); } + action.stop(); + context.recordDistanceFromPreviousPosition(state); action.complete(true); } } diff --git a/programs/benchmark/js/MoveCursorToEnd.js b/programs/benchmark/js/MoveCursorToEnd.js index 18bde0dc..258e3dcd 100644 --- a/programs/benchmark/js/MoveCursorToEnd.js +++ b/programs/benchmark/js/MoveCursorToEnd.js @@ -51,13 +51,16 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { + context.storeCurrentPosition(state); action.start(); - sharedState.sessionController.getSelectionController().moveCursorToDocumentEnd(); + context.sessionController.getSelectionController().moveCursorToDocumentEnd(); + action.stop(); + context.recordDistanceFromPreviousPosition(state); action.complete(true); - } + }; } return MoveCursorToEnd; diff --git a/programs/benchmark/js/MoveCursorToEndDirect.js b/programs/benchmark/js/MoveCursorToEndDirect.js index ca42030f..c8d741ea 100644 --- a/programs/benchmark/js/MoveCursorToEndDirect.js +++ b/programs/benchmark/js/MoveCursorToEndDirect.js @@ -51,18 +51,22 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { var canvasElement, range; + + context.storeCurrentPosition(state); action.start(); - canvasElement = sharedState.odfCanvas.getElement(); + canvasElement = context.odfCanvas.getElement(); range = canvasElement.ownerDocument.createRange(); range.setStart(canvasElement, canvasElement.childNodes.length); - sharedState.sessionController.getSelectionController().selectRange(range, true, 1); + context.sessionController.getSelectionController().selectRange(range, true, 1); + action.stop(); + context.recordDistanceFromPreviousPosition(state); action.complete(true); } } diff --git a/programs/benchmark/js/MoveCursorToStart.js b/programs/benchmark/js/MoveCursorToStart.js index 0794a488..719914a0 100644 --- a/programs/benchmark/js/MoveCursorToStart.js +++ b/programs/benchmark/js/MoveCursorToStart.js @@ -39,11 +39,14 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { + context.storeCurrentPosition(state); action.start(); - sharedState.sessionController.getSelectionController().moveCursorToDocumentStart(); + context.sessionController.getSelectionController().moveCursorToDocumentStart(); + action.stop(); + context.recordDistanceFromPreviousPosition(state); action.complete(true); } } diff --git a/programs/benchmark/js/OdfBenchmarkContext.js b/programs/benchmark/js/OdfBenchmarkContext.js new file mode 100644 index 00000000..3db99107 --- /dev/null +++ b/programs/benchmark/js/OdfBenchmarkContext.js @@ -0,0 +1,139 @@ +/** + * @license + * Copyright (C) 2010-2014 KO GmbH + * + * @licstart + * This file is part of WebODF. + * + * WebODF is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License (GNU AGPL) + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * WebODF is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with WebODF. If not, see . + * @licend + * + * @source: http://www.webodf.org/ + * @source: https://github.com/kogmbh/WebODF/ + */ + +define(["RangeCalculator"], function(RangeCalculator) { + /** + * @constructor + */ + function OdfBenchmarkContext() { + var self = this, + rangeCalculator = new RangeCalculator(); + + /** + * @return {boolean} + */ + function initialized() { + return Boolean(self.session && self.sessionController); + } + + /** + * @param {!number} length + * @return {!number} + */ + function measureSelectionLength(length) { + var odtDocument = self.session.getOdtDocument(), + position = odtDocument.getCursorPosition(self.sessionController.getInputMemberId()); + + return rangeCalculator.measureTraversedDistance(odtDocument.convertCursorToDomRange(position, length)); + } + + /** + * @return {!number} + */ + function measureCurrentSelection() { + var range = self.session.getOdtDocument().getCursor(self.sessionController.getInputMemberId()).getSelectedRange(); + return rangeCalculator.measureTraversedDistance(range); + } + + /** + * @type {?odf.OdfCanvas} + */ + this.odfCanvas = null; + + /** + * @type {?ops.Session} + */ + this.session = null; + + /** + * @type {?gui.SessionController} + */ + this.sessionController = null; + + /** + * Calculate the traveled distance from the supplied length in cursor steps + * @param {!Object} state + * @param {!number} length + * @return {undefined} + */ + this.recordDistanceFromLength = function(state, length) { + if (initialized()) { + state.travelledSteps = Math.abs(length); + state.horizontalTravelPx = measureSelectionLength(length); + } + }; + + /** + * Store the current cursor position onto the provided state + * @param {!Object} state + * @return {undefined} + */ + this.storeCurrentPosition = function(state) { + var odtDocument; + + if (initialized()) { + odtDocument = self.session.getOdtDocument(); + state.initialCursorPosition = odtDocument.getCursorPosition(self.sessionController.getInputMemberId()); + } + }; + + /** + * Calculate the traveled distance from the previously recorded position + * @param {!Object} state + * @return {undefined} + */ + this.recordDistanceFromPreviousPosition = function(state) { + var odtDocument, + finalCursorPosition; + + if (initialized()) { + odtDocument = self.session.getOdtDocument(); + runtime.assert(state.initialCursorPosition !== undefined, "Initial cursor position should first be saved with context.storeCurrentPosition"); + finalCursorPosition = odtDocument.getCursorPosition(self.sessionController.getInputMemberId()); + state.travelledSteps = Math.abs(state.initialCursorPosition - finalCursorPosition); + state.horizontalTravelPx = measureSelectionLength(state.initialCursorPosition - finalCursorPosition) + } + }; + + /** + * Calculate the traveled distance from the current selection + * @param {!Object} state + * @return {undefined} + */ + this.recordDistanceFromCurrentSelection = function(state) { + var odtDocument, + selection; + + if (initialized()) { + odtDocument = self.session.getOdtDocument(); + selection = odtDocument.getCursorSelection(self.sessionController.getInputMemberId()); + state.travelledSteps = Math.abs(selection.length); + state.horizontalTravelPx = measureCurrentSelection(); + } + }; + } + + return OdfBenchmarkContext; +}); \ No newline at end of file diff --git a/programs/benchmark/js/OpenDocument.js b/programs/benchmark/js/OpenDocument.js index 5a17279b..e4f2bf9e 100644 --- a/programs/benchmark/js/OpenDocument.js +++ b/programs/benchmark/js/OpenDocument.js @@ -49,6 +49,7 @@ define(["BenchmarkAction"], function(BenchmarkAction) { action = new BenchmarkAction(state); function onDocumentLoaded() { + action.stop(); action.complete(true); } @@ -56,12 +57,12 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { action.start(); - sharedState.odfCanvas.addListener("statereadychange", onDocumentLoaded); - sharedState.odfCanvas.load(docUrl); + context.odfCanvas.addListener("statereadychange", onDocumentLoaded); + context.odfCanvas.load(docUrl); }; } diff --git a/programs/benchmark/js/PreloadDocument.js b/programs/benchmark/js/PreloadDocument.js index b4da8a22..5953f6bb 100644 --- a/programs/benchmark/js/PreloadDocument.js +++ b/programs/benchmark/js/PreloadDocument.js @@ -49,6 +49,7 @@ define(["BenchmarkAction"], function(BenchmarkAction) { action = new BenchmarkAction(state); function onDocumentDownloaded() { + action.stop(); action.complete(true); } @@ -56,9 +57,9 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { action.start(); runtime.readFile(docUrl, 'binary', onDocumentDownloaded); }; diff --git a/programs/benchmark/js/RangeCalculator.js b/programs/benchmark/js/RangeCalculator.js new file mode 100644 index 00000000..614a6b3e --- /dev/null +++ b/programs/benchmark/js/RangeCalculator.js @@ -0,0 +1,77 @@ +/** + * @license + * Copyright (C) 2010-2014 KO GmbH + * + * @licstart + * This file is part of WebODF. + * + * WebODF is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License (GNU AGPL) + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * WebODF is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with WebODF. If not, see . + * @licend + * + * @source: http://www.webodf.org/ + * @source: https://github.com/kogmbh/WebODF/ + */ + +define([], function() { + + runtime.loadClass("odf.OdfUtils"); + + /** + * @constructor + */ + function RangeCalculator() { + var odfUtils = new odf.OdfUtils(), + textRange = document.createRange(), + TEXT_NODE = Node.TEXT_NODE; + + /** + * Calculate the amount of horizontal movement experienced while traversing + * the supplied range. + * @param {!Range} range + * @return {!number} + */ + this.measureTraversedDistance = function (range) { + var textElements = odfUtils.getTextElements(range, true, false), + horizontalDistance = 0; + + textElements.forEach(function(node) { + var clientRects, + i, + rect; + + if (node.nodeType === TEXT_NODE) { + textRange.selectNodeContents(node); + if (range.startContainer === node) { + textRange.setStart(range.startContainer, range.startOffset); + } + if (range.endContainer === node) { + range.setEnd(range.endContainer, range.endOffset); + } + clientRects = textRange.getClientRects(); + } else { + clientRects = node.getClientRects(); + } + for (i = 0; i < clientRects.length; i += 1) { + rect = clientRects[i]; + horizontalDistance += rect.width; + } + }); + + return horizontalDistance; + }; + } + + return RangeCalculator; + +}); \ No newline at end of file diff --git a/programs/benchmark/js/RemoveCurrentSelection.js b/programs/benchmark/js/RemoveCurrentSelection.js index 815b49d4..cdec6925 100644 --- a/programs/benchmark/js/RemoveCurrentSelection.js +++ b/programs/benchmark/js/RemoveCurrentSelection.js @@ -51,11 +51,13 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { + context.recordDistanceFromCurrentSelection(state); action.start(); - sharedState.sessionController.getTextController().removeCurrentSelection(); + context.sessionController.getTextController().removeCurrentSelection(); + action.stop(); action.complete(true); } } diff --git a/programs/benchmark/js/RemovePositions.js b/programs/benchmark/js/RemovePositions.js index eda31735..5a3f2b3f 100644 --- a/programs/benchmark/js/RemovePositions.js +++ b/programs/benchmark/js/RemovePositions.js @@ -53,18 +53,20 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { var count; + context.recordDistanceFromLength(state, useBackspace ? -times : times); action.start(); for (count = 0; count < times; count += 1) { if (useBackspace) { - sharedState.sessionController.getTextController().removeTextByBackspaceKey(); + context.sessionController.getTextController().removeTextByBackspaceKey(); } else { - sharedState.sessionController.getTextController().removeTextByDeleteKey(); + context.sessionController.getTextController().removeTextByDeleteKey(); } } + action.stop(); action.complete(true); } } diff --git a/programs/benchmark/js/SelectEntireDocument.js b/programs/benchmark/js/SelectEntireDocument.js index 061746df..0435ef1d 100644 --- a/programs/benchmark/js/SelectEntireDocument.js +++ b/programs/benchmark/js/SelectEntireDocument.js @@ -51,13 +51,15 @@ define(["BenchmarkAction"], function(BenchmarkAction) { this.state = state; /** - * @param {!SharedState} sharedState + * @param {!OdfBenchmarkContext} context */ - this.start = function(sharedState) { + this.start = function(context) { action.start(); - sharedState.sessionController.getSelectionController().extendSelectionToEntireDocument(); + context.sessionController.getSelectionController().extendSelectionToEntireDocument(); + action.stop(); + context.recordDistanceFromCurrentSelection(state); action.complete(true); - } + }; } return SelectEntireDocument; From 22877e576f989d95b125ab5a9bdb719bbd854d46 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Tue, 25 Feb 2014 02:34:06 +1100 Subject: [PATCH 2/5] Add pages per hour and kilometres per hour as benchmark units --- programs/benchmark/index.html | 4 +- programs/benchmark/js/Benchmark.js | 4 +- programs/benchmark/js/HTMLActionResult.js | 108 +++++++++++++++++++ programs/benchmark/js/HTMLBenchmark.js | 6 +- programs/benchmark/js/HTMLResultsRenderer.js | 53 ++++----- programs/benchmark/runner.css | 5 +- programs/benchmark/src-index.html | 4 +- 7 files changed, 143 insertions(+), 41 deletions(-) create mode 100644 programs/benchmark/js/HTMLActionResult.js diff --git a/programs/benchmark/index.html b/programs/benchmark/index.html index 1392142b..56571836 100644 --- a/programs/benchmark/index.html +++ b/programs/benchmark/index.html @@ -17,7 +17,9 @@ Action Status - Elapsed time (ms) + Time taken (ms) + km/h + pages/h diff --git a/programs/benchmark/js/Benchmark.js b/programs/benchmark/js/Benchmark.js index 999d6229..cb25e16f 100644 --- a/programs/benchmark/js/Benchmark.js +++ b/programs/benchmark/js/Benchmark.js @@ -44,10 +44,10 @@ define(["OdfBenchmarkContext"], function(OdfBenchmarkContext) { /** * @constructor + * @param {!HTMLElement} canvasElement */ - function Benchmark() { + function Benchmark(canvasElement) { var self = this, - canvasElement = document.getElementById("canvas"), context = new OdfBenchmarkContext(), events = new core.EventNotifier(["start", "complete"]), currentActionIndex, diff --git a/programs/benchmark/js/HTMLActionResult.js b/programs/benchmark/js/HTMLActionResult.js new file mode 100644 index 00000000..af49b58d --- /dev/null +++ b/programs/benchmark/js/HTMLActionResult.js @@ -0,0 +1,108 @@ +/** + * @license + * Copyright (C) 2010-2014 KO GmbH + * + * @licstart + * This file is part of WebODF. + * + * WebODF is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License (GNU AGPL) + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * WebODF is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with WebODF. If not, see . + * @licend + * + * @source: http://www.webodf.org/ + * @source: https://github.com/kogmbh/WebODF/ + */ + + +define(function() { + "use strict"; + + /** + * @constructor + * @param {!Object} action + * @param {!number} index + * @param {!HTMLTableElement} outputTable + * @param {!number} pxPerCm + */ + function HTMLActionResult(action, index, outputTable, pxPerCm) { + var descriptionCell = document.createElement('td'), + statusCell = document.createElement('td'), + elapsedMsCell = document.createElement('td'), + speedCell = document.createElement('td'), + pagesPerHr = document.createElement('td'); + + function formatNumber(number) { + if (number > 1000000) { + return (number / 1000000).toFixed(2) + " mill"; + } + return number.toFixed(2); + } + + function pxPerMsToKmPerHr(pxPerMs) { + // Google knows! + // https://www.google.com.au/search?q=centimeters%20per%20millisecond%20to%20kilometers%20per%20hour + var cmPerSecond = (pxPerMs / pxPerCm), + kmsPerHr = (cmPerSecond * 36); + return formatNumber(kmsPerHr); + } + + function toPagesPerMin(stepsPerMs) { + // http://anycount.com/WordCountBlog/how-many-words-in-one-page/ + var pagesPerHr = (stepsPerMs / 3838) * 1000 * 60 * 60; + return formatNumber(pagesPerHr); + } + + function start() { + statusCell.textContent = "running"; + elapsedMsCell.textContent = ""; + speedCell.textContent = ""; + } + + function complete(state) { + statusCell.textContent = state.status ? "done" : "failed"; + elapsedMsCell.textContent = state.elapsedTime; + if (state.horizontalTravelPx !== undefined) { + speedCell.classList.add("active"); + speedCell.textContent = pxPerMsToKmPerHr(state.horizontalTravelPx / state.elapsedTime); + } + if (state.travelledSteps !== undefined) { + pagesPerHr.classList.add("active"); + pagesPerHr.textContent = toPagesPerMin(state.travelledSteps / state.elapsedTime); + } + } + + function init() { + var tr = document.createElement('tr'); + + descriptionCell.className = "action"; + statusCell.className = "status"; + elapsedMsCell.className = "elapsed"; + speedCell.className = "speed"; + pagesPerHr.className = "pages"; + + [descriptionCell, statusCell, elapsedMsCell, speedCell, pagesPerHr].forEach(function(cell) { + tr.appendChild(cell); + }); + + outputTable.appendChild(tr); + descriptionCell.textContent = (index + 1) + ". " + action.state.description; + + action.subscribe("start", start); + action.subscribe("complete", complete); + } + init(); + } + + return HTMLActionResult; + } +); diff --git a/programs/benchmark/js/HTMLBenchmark.js b/programs/benchmark/js/HTMLBenchmark.js index 7d983122..286e408a 100644 --- a/programs/benchmark/js/HTMLBenchmark.js +++ b/programs/benchmark/js/HTMLBenchmark.js @@ -100,9 +100,11 @@ define([ */ function HTMLBenchmark() { var loadingScreen = document.getElementById('loadingScreen'), + canvasElement = document.getElementById("canvas"), + benchmarkResults = document.getElementById("benchmarkResults"), config = getConfiguration(), - benchmark = new Benchmark(), - renderer = new HTMLResultsRenderer(benchmark); + benchmark = new Benchmark(canvasElement), + renderer = new HTMLResultsRenderer(benchmark, benchmarkResults); renderer.setBackgroundColour(config.colour); diff --git a/programs/benchmark/js/HTMLResultsRenderer.js b/programs/benchmark/js/HTMLResultsRenderer.js index a8989ecb..2baba1ff 100644 --- a/programs/benchmark/js/HTMLResultsRenderer.js +++ b/programs/benchmark/js/HTMLResultsRenderer.js @@ -36,48 +36,21 @@ * @source: https://github.com/kogmbh/WebODF/ */ -define(function() { +define(["HTMLActionResult"], function(HTMLActionResult) { "use strict"; /** - * @param {!Benchmark} benchmark * @constructor + * @param {!Benchmark} benchmark + * @param {!HTMLTableElement} outputTable */ - function HTMLResultsRenderer(benchmark) { - var outputTable = document.getElementById("benchmarkResults"); - - /** - * @param {!Text} node - * @param {!string} newValue - */ - function setText(node, newValue) { - node.replaceData(0, node.length, newValue); - } + function HTMLResultsRenderer(benchmark, outputTable) { + var results = [], + pxPerCm; function onStart() { benchmark.actions.forEach(function(action, index) { - var tr = document.createElement('tr'), - cells = ["action", "status", "elapsed"].map(function(clazz) { - var td = document.createElement('td'), - text = document.createTextNode(""); - td.className = clazz; - td.appendChild(text); - tr.appendChild(td); - return text; - }); - - outputTable.appendChild(tr); - setText(cells[0], (index + 1) + ". " + action.state.description); - - action.subscribe("start", function() { - setText(cells[1], "running"); - setText(cells[2], ""); - }); - - action.subscribe("complete", function(state) { - setText(cells[1], state.status ? "done" : "failed"); - setText(cells[2], state.elapsedTime); - }); + results.push(new HTMLActionResult(action, index, outputTable, pxPerCm)); }); } @@ -96,6 +69,18 @@ define(function() { }; function init() { + var parent, + testChild; + + // Determine how many px per cm on the current screen + parent = outputTable.parentNode; + testChild = document.createElement("div"); + testChild.style.width = "1cm"; + testChild.style.height = "1cm"; + parent.appendChild(testChild); + pxPerCm = testChild.getBoundingClientRect().width; + parent.removeChild(testChild); + benchmark.subscribe("start", onStart); benchmark.subscribe("complete", onComplete); } diff --git a/programs/benchmark/runner.css b/programs/benchmark/runner.css index 1f8dfc21..207fe805 100644 --- a/programs/benchmark/runner.css +++ b/programs/benchmark/runner.css @@ -25,7 +25,10 @@ body { display: none; } -#benchmarkResults .elapsed { +#benchmarkResults .elapsed, +#benchmarkResults .speed, +#benchmarkResults .pages +{ text-align: right; } diff --git a/programs/benchmark/src-index.html b/programs/benchmark/src-index.html index a9ccf3b9..e0e679b5 100644 --- a/programs/benchmark/src-index.html +++ b/programs/benchmark/src-index.html @@ -37,7 +37,9 @@ Action Status - Elapsed time (ms) + Time taken (ms) + km/h + pages/h From 8c68d5d1214eabd401f3506ed1c840337390060c Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Wed, 26 Feb 2014 17:46:55 +1100 Subject: [PATCH 3/5] Output the webodf version the benchmark is running against --- programs/benchmark/index.html | 3 ++- programs/benchmark/js/Benchmark.js | 10 ++++++++++ programs/benchmark/js/HTMLBenchmark.js | 2 ++ programs/benchmark/src-index.html | 3 ++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/programs/benchmark/index.html b/programs/benchmark/index.html index 56571836..ad12eccb 100644 --- a/programs/benchmark/index.html +++ b/programs/benchmark/index.html @@ -11,7 +11,8 @@

Loading required files. This may take a while.

-

Benchmark results

+

Benchmark

+
version: unknown
diff --git a/programs/benchmark/js/Benchmark.js b/programs/benchmark/js/Benchmark.js index cb25e16f..e9ead23c 100644 --- a/programs/benchmark/js/Benchmark.js +++ b/programs/benchmark/js/Benchmark.js @@ -84,6 +84,16 @@ define(["OdfBenchmarkContext"], function(OdfBenchmarkContext) { currentActionIndex = -1; executeNextAction(); }; + + /** + * @returns {!string} + */ + this.getWebODFVersion = function() { + return (typeof webodf_version !== "undefined" + ? String(webodf_version) + : "uncompiled" + ); + }; } return Benchmark; diff --git a/programs/benchmark/js/HTMLBenchmark.js b/programs/benchmark/js/HTMLBenchmark.js index 286e408a..ee6ecc46 100644 --- a/programs/benchmark/js/HTMLBenchmark.js +++ b/programs/benchmark/js/HTMLBenchmark.js @@ -102,10 +102,12 @@ define([ var loadingScreen = document.getElementById('loadingScreen'), canvasElement = document.getElementById("canvas"), benchmarkResults = document.getElementById("benchmarkResults"), + version = document.getElementById("version"), config = getConfiguration(), benchmark = new Benchmark(canvasElement), renderer = new HTMLResultsRenderer(benchmark, benchmarkResults); + version.textContent = benchmark.getWebODFVersion(); renderer.setBackgroundColour(config.colour); loadingScreen.style.display = "none"; diff --git a/programs/benchmark/src-index.html b/programs/benchmark/src-index.html index e0e679b5..bc9323ac 100644 --- a/programs/benchmark/src-index.html +++ b/programs/benchmark/src-index.html @@ -31,7 +31,8 @@

Loading required files. This may take a while.

-

Benchmark results

+

Benchmark

+
version: unknown
From 4f0d00e86885881b13fb8644ae2f745aa2a76b19 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Wed, 26 Feb 2014 23:43:08 +1100 Subject: [PATCH 4/5] Make benchmark UI slightly more colourful --- programs/benchmark/index.html | 6 +-- programs/benchmark/js/HTMLBenchmark.js | 2 +- programs/benchmark/runner.css | 60 ++++++++++++++++++++++++-- programs/benchmark/src-index.html | 6 +-- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/programs/benchmark/index.html b/programs/benchmark/index.html index ad12eccb..456cdef4 100644 --- a/programs/benchmark/index.html +++ b/programs/benchmark/index.html @@ -11,18 +11,18 @@

Loading required files. This may take a while.

-

Benchmark

-
version: unknown
+

Benchmark [unknown]

- + +
Action StatusTime taken (ms)Time taken
(ms)
km/h pages/h
diff --git a/programs/benchmark/js/HTMLBenchmark.js b/programs/benchmark/js/HTMLBenchmark.js index ee6ecc46..72a3e800 100644 --- a/programs/benchmark/js/HTMLBenchmark.js +++ b/programs/benchmark/js/HTMLBenchmark.js @@ -101,7 +101,7 @@ define([ function HTMLBenchmark() { var loadingScreen = document.getElementById('loadingScreen'), canvasElement = document.getElementById("canvas"), - benchmarkResults = document.getElementById("benchmarkResults"), + benchmarkResults = document.getElementById("benchmarkResults").getElementsByTagName("tbody")[0], version = document.getElementById("version"), config = getConfiguration(), benchmark = new Benchmark(canvasElement), diff --git a/programs/benchmark/runner.css b/programs/benchmark/runner.css index 207fe805..dd355986 100644 --- a/programs/benchmark/runner.css +++ b/programs/benchmark/runner.css @@ -12,23 +12,75 @@ body { max-width: 100%; /*width: 400px;*/ float: left; - background-color: #70a186; + background-color: #49A34B; padding: 0 10px; } +#benchmarkView h1 { + text-align: center; +} + #documentView { min-height: 100%; overflow: auto; } +#benchmarkResults { + border-collapse: collapse; + margin-bottom: 10px; +} + +#benchmarkResults td { + padding-top: 5px; + padding-bottom: 5px; +} + +#benchmarkResults thead { + font-weight: bold; + font-size: small; +} + +#benchmarkResults thead td { + text-align: center; +} + +#benchmarkResults thead td:not(:last-of-type) { + border-right: 1px solid white; +} + +#benchmarkResults tbody td { + border-right: 1px solid white; +} + +#benchmarkResults tbody td:first-of-type { + border-left: 1px solid white; +} + +#benchmarkResults tbody tr { + border-top: 1px solid white; +} + +#benchmarkResults tbody tr:hover td { + background-color: #5AB953; +} + +#benchmarkResults tbody tr:nth-child(2n+0) { + background-color: #E6FFBF; +} + +#benchmarkResults tbody tr:nth-child(2n+1) { + background-color: #BEEE9E; +} + #benchmarkResults .status { display: none; } -#benchmarkResults .elapsed, -#benchmarkResults .speed, -#benchmarkResults .pages +#benchmarkResults tbody .elapsed, +#benchmarkResults tbody .speed, +#benchmarkResults tbody .pages { + padding-left: 1em; text-align: right; } diff --git a/programs/benchmark/src-index.html b/programs/benchmark/src-index.html index bc9323ac..63185b45 100644 --- a/programs/benchmark/src-index.html +++ b/programs/benchmark/src-index.html @@ -31,18 +31,18 @@

Loading required files. This may take a while.

-

Benchmark

-
version: unknown
+

Benchmark [unknown]

- + +
Action StatusTime taken (ms)Time taken
(ms)
km/h pages/h
From 4c87f29a0b15e1c57ee2f4ff2307f2b51b9539a0 Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Fri, 28 Feb 2014 01:33:32 +1100 Subject: [PATCH 5/5] Make element variable names more consistent --- programs/benchmark/js/HTMLBenchmark.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/programs/benchmark/js/HTMLBenchmark.js b/programs/benchmark/js/HTMLBenchmark.js index 72a3e800..b2a696a9 100644 --- a/programs/benchmark/js/HTMLBenchmark.js +++ b/programs/benchmark/js/HTMLBenchmark.js @@ -99,18 +99,18 @@ define([ * @constructor */ function HTMLBenchmark() { - var loadingScreen = document.getElementById('loadingScreen'), + var loadingScreenElement = document.getElementById('loadingScreen'), canvasElement = document.getElementById("canvas"), - benchmarkResults = document.getElementById("benchmarkResults").getElementsByTagName("tbody")[0], - version = document.getElementById("version"), + benchmarkResultsElement = document.getElementById("benchmarkResults").getElementsByTagName("tbody")[0], + versionElement = document.getElementById("version"), config = getConfiguration(), benchmark = new Benchmark(canvasElement), - renderer = new HTMLResultsRenderer(benchmark, benchmarkResults); + renderer = new HTMLResultsRenderer(benchmark, benchmarkResultsElement); - version.textContent = benchmark.getWebODFVersion(); + versionElement.textContent = benchmark.getWebODFVersion(); renderer.setBackgroundColour(config.colour); - loadingScreen.style.display = "none"; + loadingScreenElement.style.display = "none"; benchmark.actions.push(new PreloadDocument(config.fileUrl)); benchmark.actions.push(new OpenDocument(config.fileUrl));