-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
94 lines (83 loc) · 2.83 KB
/
test.js
File metadata and controls
94 lines (83 loc) · 2.83 KB
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
/*
Copyright (C) 2023 by Eric Chen
This file is part of p2p_share_compute.
p2p_share_compute is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
p2p_share_compute 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with p2p_share_compute. If not, see <https://www.gnu.org/licenses/>.
*/
const Peer = require('./index.js');
// do some testing!
const fs = require('fs');
const {performance} = require('perf_hooks');
const config = JSON.parse(fs.readFileSync('./config.json'));
config.onReady = main;
const peer = new Peer(config);
async function main() {
console.log('1. speed tests');
const speedData = {
query: [],
search: []
};
// 1.1 Test query speed
const peers = Object.keys(peer.peerCache);
for (const p of peers) {
if (p===peer.origin) continue;
for (let i = 0; i < 50; ++i) {
const start = performance.now();
const res = await peer.sendHttp(p, {type:'reqconn'})
const end = performance.now();
speedData.query.push(end-start);
}
}
console.log('query speed')
console.log(speedData.query);
// approximations so that we won't run into a case where we fail but there actually is no such machine
const machineData = [
//{ram:11671076864,flops:470716473}, /* testing machine data, ignore */
{ram:1600000000,flops:470000000},
{ram:4000000000,flops:530000000},
{ram:3300000000,flops:500000000},
{ram:3600000000,flops:530000000}
]
// 1.2
for (const m of machineData) {
for (let i = 0; i < 10; ++i) {
const start = performance.now();
const p = await peer.search(m.ram, m.flops, [peer.origin]);
const end = performance.now();
speedData.search.push(end-start);
}
}
console.log('search speed');
console.log(speedData.search);
const querydata = {
search: ['ram','flops','start','ttl','origTtl','id'],
searchRes: ['peer','id']
};
for (const p in peers) {
// test every invalid query
for (const q in Object.keys(querydata)) {
const types = querydata[q];
for (const t in types) {
const data = {};
for (const x in types) {
if (x != t) data[x] = 'test';
}
peer.sendHttp(p, data);
await (new Promise((resolve, reject) => {
peer.on('error', d => {
if (d.type === 'INVALID') resolve();
});
setTimeout(() => reject('timeout'), 5 * 1000);
}));
}
}
}
console.log('all ok');
const out = {speedData};
out.invalidTests = true;
console.log(out);
fs.writeFileSync('data/out.json', JSON.stringify(out));
console.log('done');
process.exit();
}