-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-client.js
155 lines (147 loc) · 5.89 KB
/
test-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var process = require("process");
var freedom = require("freedom-for-node");
var randgen = require('randgen');
var request = require('request');
var fs = require('fs');
var argv = require('yargs')
.usage('Usage: $0 [-h] [-m mean] [-v variance] [-n num_samples] [-s] [-p paramfile] [-f savefile] [-H host] [-P path] [-V variable]')
.default('mean', 9)
.default('variance', 4)
.default('samples', 10)
.default('submit', false)
.default('host', 'https://www.uproxy.org')
.default('path', 'submit-rappor-stats')
.default('variable', 'test-v1')
.default('paramfile', 'params.csv')
.alias('h', 'help')
.help('help')
.alias('m', 'mean')
.alias('v', 'variance')
.alias('n', 'samples')
.alias('H', 'host')
.alias('P', 'path')
.alias('V', 'variable')
.alias('s', 'submit')
.alias('f', 'savefile')
.alias('p', 'paramfile')
.argv;
function post_values(val) {
var path = argv.host + "/" + argv.path;
console.log("Posting " + Object.keys(val).length + " items to " + path);
function post_value_n(idx) {
var params = {};
params[argv.variable] = val[Object.keys(val)[idx]];
var param_str = JSON.stringify(params);
var options = {
method: 'post',
body: params,
json: true,
url: path
}
console.log("Posting " + JSON.stringify(params));
request(options, function (err, res, body) {
if (err) {
inspect(err, 'error posting json')
return
}
var headers = res.headers
var statusCode = res.statusCode
if (statusCode != 200) {
console.log("Failed: " + JSON.stringify(res));
} else if ((1+idx) < Object.keys(val).length) {
post_value_n(idx + 1);
} else {
process.exit(0);
}
})
}
post_value_n(0);
}
function save_values(vals, samples, outputFileName) {
var stats = [];
var date = new Date();
var hourFields = [ date.getFullYear(), 1+date.getMonth(), date.getDate(),
1+date.getHours(), date.getMinutes(), date.getSeconds() ]
var basis = parseInt('cab73c58', 16);
var keys = Object.keys(vals);
for (var i = 0; i < keys.length; i++) {
stat = { 'submission_id' : '8cbd24dc-45b6-41b3-b9d5-1a7d' + (basis + i).toString(16),
'metric' : 'test-v1',
'hour': hourFields,
'value': vals[keys[i]] }
stats.push(stat);
}
values = { 'count': stats.length, 'rapporStats': stats };
fs.writeFileSync(outputFileName, JSON.stringify(values));
fs.writeFileSync(outputFileName.split(".")[0] + ".real", JSON.stringify(samples));
}
var READ_SIZE = 16384;
var param_bloombits, param_hashes, param_cohorts, param_prob_p, param_prob_q, param_prob_f;
console.log("Opening", argv.paramfile);
fs.read(fs.openSync(argv.paramfile, 'r'), new Buffer(READ_SIZE), 0, READ_SIZE, null,
function (e,bytesRead, paramBuf) {
var paramText = paramBuf.toString('utf8', 0, bytesRead);
// values are k,h,m,p,q,f
var varNames = paramText.split('\n')[0].split(',');
var params = paramText.split('\n')[1].split(',');
param_bloombits = parseInt(params[varNames.indexOf('k')]);
param_hashes = parseInt(params[varNames.indexOf('h')]);
param_cohorts = parseInt(params[varNames.indexOf('m')]);
param_prob_p = parseFloat(params[varNames.indexOf('p')]);
param_prob_q = parseFloat(params[varNames.indexOf('q')]);
param_prob_f = parseFloat(params[varNames.indexOf('f')]);
console.log(argv.paramfile,":\n", paramText);
// Now that we have the parameters, use them.
generateData();
});
function generateData() {
freedom.freedom("node_modules/freedomjs-anonymized-metrics/anonmetrics.json", {}).then(
function (rappor_proto) {
console.log("starting data generation");
var metrics_def = {"name":"TestMetrics",
"definition": {}};
metrics_def.definition["test-v1"] = {
"type": "logarithmic",
"base": 2,
"num_bloombits": param_bloombits,
"num_hashes": param_hashes,
"num_cohorts": param_cohorts,
"prob_p": param_prob_p,
"prob_q": param_prob_q,
"prob_f": param_prob_f,
"flag_oneprr": true
};
var metrics = [];
for (var i = 0; i < argv.samples; i++) {
metrics.push(new rappor_proto(metrics_def));
}
var all_reports = [];
var samples = [];
for (var i = 0; i < argv.samples; i++) {
var sample;
do {
sample = Math.ceil(randgen.rnorm(argv.mean, argv.variance));
} while (sample < 0);
samples.push(sample);
all_reports.push(metrics[i].report("test-v1", sample));
};
Promise.all(all_reports).then(function() {
Promise.all(metrics.map(function(met) { return met.retrieve(); })).then(
function(val_kvs) {
console.log("Samples: " + samples);
var val = val_kvs.map(function(m) { return m["test-v1"];});
for (var k in val) {
console.log(" " + val[k]);
}
if (argv.savefile) {
save_values(val, samples, argv.savefile);
}
if (argv.submit) {
post_values(val);
} else {
process.exit(0);
}
});
});
});
};