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 | // |
andresp@webrtc.org | 458c2c3 | 2014-10-16 07:36:37 +0000 | [diff] [blame^] | 9 | // Provides a Test class that exposes api to the tests. |
| 10 | // Read test.prototype to see what methods are exposed. |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 11 | var fs = require('fs'); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 12 | var BotManager = require('./botmanager.js'); |
| 13 | |
houssainy@google.com | 3e2f8ff | 2014-10-15 15:01:11 +0000 | [diff] [blame] | 14 | function Test() { |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 15 | this.timeout_ = setTimeout( |
| 16 | this.fail.bind(this, "Test timeout!"), |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 17 | 100000); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | Test.prototype = { |
| 21 | log: function () { |
| 22 | console.log.apply(console.log, arguments); |
| 23 | }, |
| 24 | |
| 25 | abort: function (error) { |
houssainy@google.com | e0761d0 | 2014-09-10 14:35:35 +0000 | [diff] [blame] | 26 | var error = new Error(error || "Test aborted"); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 27 | console.log(error.stack); |
| 28 | process.exit(1); |
| 29 | }, |
| 30 | |
| 31 | assert: function (value, message) { |
| 32 | if (value !== true) { |
| 33 | this.abort(message || "Assert failed."); |
| 34 | } |
| 35 | }, |
| 36 | |
| 37 | fail: function () { |
| 38 | this.assert(false, "Test failed."); |
| 39 | }, |
| 40 | |
| 41 | done: function () { |
| 42 | clearTimeout(this.timeout_); |
| 43 | console.log("Test succeeded"); |
| 44 | process.exit(0); |
| 45 | }, |
| 46 | |
| 47 | // Utility method to wait for multiple callbacks to be executed. |
| 48 | // functions - array of functions to call with a callback. |
| 49 | // doneCallback - called when all callbacks on the array have completed. |
| 50 | wait: function (functions, doneCallback) { |
| 51 | var result = new Array(functions.length); |
| 52 | var missingResult = functions.length; |
| 53 | for (var i = 0; i != functions.length; ++i) |
| 54 | functions[i](complete.bind(this, i)); |
| 55 | |
| 56 | function complete(index, value) { |
| 57 | missingResult--; |
| 58 | result[index] = value; |
| 59 | if (missingResult == 0) |
| 60 | doneCallback.apply(null, result); |
| 61 | } |
| 62 | }, |
| 63 | |
houssainy@google.com | 3e2f8ff | 2014-10-15 15:01:11 +0000 | [diff] [blame] | 64 | spawnBot: function (name, botType, doneCallback) { |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 65 | // Lazy initialization of botmanager. |
| 66 | if (!this.botManager_) |
| 67 | this.botManager_ = new BotManager(); |
houssainy@google.com | 3e2f8ff | 2014-10-15 15:01:11 +0000 | [diff] [blame] | 68 | this.botManager_.spawnNewBot(name, botType, doneCallback); |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 69 | }, |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 70 | |
| 71 | createStatisticsReport: function (outputFileName) { |
| 72 | return new StatisticsReport(outputFileName); |
| 73 | }, |
| 74 | } |
| 75 | |
| 76 | StatisticsReport = function (outputFileName) { |
| 77 | this.output_ = []; |
| 78 | this.output_.push("Version: 1"); |
| 79 | this.outputFileName_ = outputFileName; |
| 80 | } |
| 81 | |
| 82 | StatisticsReport.prototype = { |
| 83 | collectStatsFromPeerConnection: function (prefix, pc) { |
| 84 | setInterval(this.addPeerConnectionStats.bind(this, prefix, pc), 100); |
| 85 | }, |
| 86 | |
| 87 | addPeerConnectionStats: function (prefix, pc) { |
| 88 | pc.getStats(onStatsReady.bind(this)); |
| 89 | |
| 90 | function onStatsReady(reports) { |
| 91 | for (index in reports) { |
| 92 | var stats = {}; |
| 93 | stats[reports[index].id] = collectStats(reports[index].stats); |
| 94 | |
| 95 | var data = {}; |
| 96 | data[prefix] = stats; |
| 97 | |
| 98 | this.output_.push({ |
| 99 | type: "UpdateCounters", |
| 100 | startTime: (new Date()).getTime(), |
| 101 | data: data, |
| 102 | }); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | function collectStats(stats) { |
| 107 | var outputStats = {}; |
| 108 | for (index in stats) { |
| 109 | var statValue = parseFloat(stats[index].stat); |
| 110 | outputStats[stats[index].name] = isNaN(statValue)? |
| 111 | stats[index].stat : statValue; |
| 112 | } |
| 113 | return outputStats; |
| 114 | }; |
| 115 | }, |
| 116 | |
| 117 | finish: function (doneCallback) { |
houssainy@google.com | 5d3e7ac | 2014-10-06 17:21:27 +0000 | [diff] [blame] | 118 | fs.exists("test/reports/", function (exists) { |
| 119 | if(exists) { |
| 120 | writeFile.bind(this)(); |
| 121 | } else { |
| 122 | fs.mkdir("test/reports/", 0777, writeFile.bind(this)); |
| 123 | } |
| 124 | }.bind(this)); |
| 125 | |
| 126 | function writeFile () { |
| 127 | fs.writeFile("test/reports/" + this.outputFileName_ + "_" + |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 128 | (new Date()).getTime() +".json", JSON.stringify(this.output_), |
| 129 | doneCallback); |
houssainy@google.com | 5d3e7ac | 2014-10-06 17:21:27 +0000 | [diff] [blame] | 130 | } |
houssainy@google.com | d0bb586 | 2014-09-30 15:20:15 +0000 | [diff] [blame] | 131 | }, |
andresp@webrtc.org | 458c2c3 | 2014-10-16 07:36:37 +0000 | [diff] [blame^] | 132 | }; |
andresp@webrtc.org | 468516c | 2014-09-02 10:52:54 +0000 | [diff] [blame] | 133 | |
andresp@webrtc.org | 458c2c3 | 2014-10-16 07:36:37 +0000 | [diff] [blame^] | 134 | module.exports = Test; |