andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 1 | // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the LICENSE file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
| 8 | // |
| 9 | // This script loads the test file in the virtual machine and runs it in a |
| 10 | // context that only exposes a test variable with methods for testing and to |
| 11 | // spawn bots. |
| 12 | // |
| 13 | // Note: an important part of this script is to keep nodejs-isms away from test |
| 14 | // code and isolate it from implementation details. |
| 15 | var fs = require('fs'); |
| 16 | var vm = require('vm'); |
| 17 | var BotManager = require('./botmanager.js'); |
| 18 | |
houssainy@google.com | c77e4d6 | 2014-09-08 10:36:11 +0000 | [diff] [blame] | 19 | function Test(botType) { |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 20 | this.timeout_ = setTimeout( |
| 21 | this.fail.bind(this, "Test timeout!"), |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 22 | 100000); |
houssainy@google.com | c77e4d6 | 2014-09-08 10:36:11 +0000 | [diff] [blame] | 23 | this.botType_ = botType; |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | Test.prototype = { |
| 27 | log: function () { |
| 28 | console.log.apply(console.log, arguments); |
| 29 | }, |
| 30 | |
| 31 | abort: function (error) { |
houssainy@google.com | e0761d0 | 2014-09-10 14:35:35 +0000 | [diff] [blame] | 32 | var error = new Error(error || "Test aborted"); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 33 | console.log(error.stack); |
| 34 | process.exit(1); |
| 35 | }, |
| 36 | |
| 37 | assert: function (value, message) { |
| 38 | if (value !== true) { |
| 39 | this.abort(message || "Assert failed."); |
| 40 | } |
| 41 | }, |
| 42 | |
| 43 | fail: function () { |
| 44 | this.assert(false, "Test failed."); |
| 45 | }, |
| 46 | |
| 47 | done: function () { |
| 48 | clearTimeout(this.timeout_); |
| 49 | console.log("Test succeeded"); |
| 50 | process.exit(0); |
| 51 | }, |
| 52 | |
| 53 | // Utility method to wait for multiple callbacks to be executed. |
| 54 | // functions - array of functions to call with a callback. |
| 55 | // doneCallback - called when all callbacks on the array have completed. |
| 56 | wait: function (functions, doneCallback) { |
| 57 | var result = new Array(functions.length); |
| 58 | var missingResult = functions.length; |
| 59 | for (var i = 0; i != functions.length; ++i) |
| 60 | functions[i](complete.bind(this, i)); |
| 61 | |
| 62 | function complete(index, value) { |
| 63 | missingResult--; |
| 64 | result[index] = value; |
| 65 | if (missingResult == 0) |
| 66 | doneCallback.apply(null, result); |
| 67 | } |
| 68 | }, |
| 69 | |
| 70 | spawnBot: function (name, doneCallback) { |
| 71 | // Lazy initialization of botmanager. |
| 72 | if (!this.botManager_) |
| 73 | this.botManager_ = new BotManager(); |
houssainy@google.com | c77e4d6 | 2014-09-08 10:36:11 +0000 | [diff] [blame] | 74 | this.botManager_.spawnNewBot(name, this.botType_, doneCallback); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 75 | }, |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 76 | |
| 77 | createStatisticsReport: function (outputFileName) { |
| 78 | return new StatisticsReport(outputFileName); |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | StatisticsReport = function (outputFileName) { |
| 83 | this.output_ = []; |
| 84 | this.output_.push("Version: 1"); |
| 85 | this.outputFileName_ = outputFileName; |
| 86 | } |
| 87 | |
| 88 | StatisticsReport.prototype = { |
| 89 | collectStatsFromPeerConnection: function (prefix, pc) { |
| 90 | setInterval(this.addPeerConnectionStats.bind(this, prefix, pc), 100); |
| 91 | }, |
| 92 | |
| 93 | addPeerConnectionStats: function (prefix, pc) { |
| 94 | pc.getStats(onStatsReady.bind(this)); |
| 95 | |
| 96 | function onStatsReady(reports) { |
| 97 | for (index in reports) { |
| 98 | var stats = {}; |
| 99 | stats[reports[index].id] = collectStats(reports[index].stats); |
| 100 | |
| 101 | var data = {}; |
| 102 | data[prefix] = stats; |
| 103 | |
| 104 | this.output_.push({ |
| 105 | type: "UpdateCounters", |
| 106 | startTime: (new Date()).getTime(), |
| 107 | data: data, |
| 108 | }); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | function collectStats(stats) { |
| 113 | var outputStats = {}; |
| 114 | for (index in stats) { |
| 115 | var statValue = parseFloat(stats[index].stat); |
| 116 | outputStats[stats[index].name] = isNaN(statValue)? |
| 117 | stats[index].stat : statValue; |
| 118 | } |
| 119 | return outputStats; |
| 120 | }; |
| 121 | }, |
| 122 | |
| 123 | finish: function (doneCallback) { |
houssainy@google.com | 5d3e7ac | 2014-10-06 17:21:27 +0000 | [diff] [blame^] | 124 | fs.exists("test/reports/", function (exists) { |
| 125 | if(exists) { |
| 126 | writeFile.bind(this)(); |
| 127 | } else { |
| 128 | fs.mkdir("test/reports/", 0777, writeFile.bind(this)); |
| 129 | } |
| 130 | }.bind(this)); |
| 131 | |
| 132 | function writeFile () { |
| 133 | fs.writeFile("test/reports/" + this.outputFileName_ + "_" + |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 134 | (new Date()).getTime() +".json", JSON.stringify(this.output_), |
| 135 | doneCallback); |
houssainy@google.com | 5d3e7ac | 2014-10-06 17:21:27 +0000 | [diff] [blame^] | 136 | } |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 137 | }, |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 138 | } |
| 139 | |
houssainy@google.com | 35850ff | 2014-09-22 15:24:56 +0000 | [diff] [blame] | 140 | function runTest(botType, testfile) { |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 141 | console.log("Running test: " + testfile); |
| 142 | var script = vm.createScript(fs.readFileSync(testfile), testfile); |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 143 | script.runInNewContext({ test: new Test(botType), setInterval: setInterval, |
houssainy@google.com | 24f62e1 | 2014-09-29 09:36:28 +0000 | [diff] [blame] | 144 | setTimeout: setTimeout }); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
houssainy@google.com | 35850ff | 2014-09-22 15:24:56 +0000 | [diff] [blame] | 147 | runTest(process.argv[2], process.argv[3]); |