blob: abf0e173d4b9947b8cb9e1d4c6f46c83054c39a7 [file] [log] [blame]
andresp@webrtc.org468516c2014-09-02 10:52:54 +00001// 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.org458c2c32014-10-16 07:36:37 +00009// Provides a Test class that exposes api to the tests.
10// Read test.prototype to see what methods are exposed.
andresp@webrtc.org468516c2014-09-02 10:52:54 +000011var fs = require('fs');
andresp@webrtc.org468516c2014-09-02 10:52:54 +000012var BotManager = require('./botmanager.js');
13
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +000014function Test() {
andresp@webrtc.org468516c2014-09-02 10:52:54 +000015 this.timeout_ = setTimeout(
16 this.fail.bind(this, "Test timeout!"),
houssainy@google.comd0bb5862014-09-30 15:20:15 +000017 100000);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000018}
19
20Test.prototype = {
21 log: function () {
22 console.log.apply(console.log, arguments);
23 },
24
25 abort: function (error) {
houssainy@google.come0761d02014-09-10 14:35:35 +000026 var error = new Error(error || "Test aborted");
andresp@webrtc.org468516c2014-09-02 10:52:54 +000027 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.com3e2f8ff2014-10-15 15:01:11 +000064 spawnBot: function (name, botType, doneCallback) {
andresp@webrtc.org468516c2014-09-02 10:52:54 +000065 // Lazy initialization of botmanager.
66 if (!this.botManager_)
67 this.botManager_ = new BotManager();
houssainy@google.com3e2f8ff2014-10-15 15:01:11 +000068 this.botManager_.spawnNewBot(name, botType, doneCallback);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000069 },
houssainy@google.comd0bb5862014-09-30 15:20:15 +000070
71 createStatisticsReport: function (outputFileName) {
72 return new StatisticsReport(outputFileName);
73 },
74}
75
76StatisticsReport = function (outputFileName) {
77 this.output_ = [];
78 this.output_.push("Version: 1");
79 this.outputFileName_ = outputFileName;
80}
81
82StatisticsReport.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.com5d3e7ac2014-10-06 17:21:27 +0000118 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.comd0bb5862014-09-30 15:20:15 +0000128 (new Date()).getTime() +".json", JSON.stringify(this.output_),
129 doneCallback);
houssainy@google.com5d3e7ac2014-10-06 17:21:27 +0000130 }
houssainy@google.comd0bb5862014-09-30 15:20:15 +0000131 },
andresp@webrtc.org458c2c32014-10-16 07:36:37 +0000132};
andresp@webrtc.org468516c2014-09-02 10:52:54 +0000133
andresp@webrtc.org458c2c32014-10-16 07:36:37 +0000134module.exports = Test;