fix(core): update

This commit is contained in:
Philipp Kunz 2021-04-28 14:27:22 +00:00
parent 9544823401
commit b29e13b162
2 changed files with 95 additions and 21 deletions

View File

@ -10,7 +10,8 @@ tap.test('should create a valid instance of SmartNetwork', async () => {
tap.test('should perform a speedtest', async () => { tap.test('should perform a speedtest', async () => {
const result = await testSmartNetwork.getSpeed(); const result = await testSmartNetwork.getSpeed();
console.log(result); console.log(`Download speed for this instance is ${result.downloadSpeed}`);
console.log(`Upload speed for this instance is ${result.uploadSpeed}`);
}); });
tap.test('should determine wether a port is free', async () => { tap.test('should determine wether a port is free', async () => {

View File

@ -10,6 +10,25 @@ export class CloudflareSpeed {
const serverLocations = await this.fetchServerLocations(); const serverLocations = await this.fetchServerLocations();
const cgiData = await this.fetchCfCdnCgiTrace(); const cgiData = await this.fetchCfCdnCgiTrace();
const testDown1 = await this.measureDownload(101000, 10);
const testDown2 = await this.measureDownload(1001000, 8);
const testDown3 = await this.measureDownload(10001000, 6);
const testDown4 = await this.measureDownload(25001000, 4);
const testDown5 = await this.measureDownload(100001000, 1);
const downloadTests = [...testDown1, ...testDown2, ...testDown3, ...testDown4, ...testDown5];
const speedDownload = stats.median(downloadTests).toFixed(2);
const testUp1 = await this.measureUpload(11000, 10);
const testUp2 = await this.measureUpload(101000, 10);
const testUp3 = await this.measureUpload(1001000, 8);
const uploadTests = [...testUp1, ...testUp2, ...testUp3];
const speedUpload = stats.median(uploadTests).toFixed(2);
return { return {
...latency, ...latency,
ip: cgiData.ip, ip: cgiData.ip,
@ -17,7 +36,9 @@ export class CloudflareSpeed {
shortId: cgiData.colo, shortId: cgiData.colo,
name: serverLocations[cgiData.colo], name: serverLocations[cgiData.colo],
availableLocations: serverLocations, availableLocations: serverLocations,
} },
downloadSpeed: speedDownload,
uploadSpeed: speedUpload,
}; };
} }
@ -42,12 +63,50 @@ export class CloudflareSpeed {
averageTime: stats.average(measurements), averageTime: stats.average(measurements),
medianTime: stats.median(measurements), medianTime: stats.median(measurements),
jitter: stats.jitter(measurements), jitter: stats.jitter(measurements),
};
} }
; public async measureDownload(bytes: number, iterations: number) {
const measurements = [];
for (let i = 0; i < iterations; i += 1) {
await this.download(bytes).then(
async (response) => {
const transferTime = response[5] - response[4];
measurements.push(await this.measureSpeed(bytes, transferTime));
},
(error) => {
console.log(`Error: ${error}`);
}
);
} }
public async fetchServerLocations(): Promise<{[key: string]: string}> { return measurements;
}
public async measureUpload(bytes: number, iterations: number) {
const measurements = [];
for (let i = 0; i < iterations; i += 1) {
await this.upload(bytes).then(
async (response) => {
const transferTime = response[6];
measurements.push(await this.measureSpeed(bytes, transferTime));
},
(error) => {
console.log(`Error: ${error}`);
}
);
}
return measurements;
}
public async measureSpeed(bytes: number, duration: number) {
return (bytes * 8) / (duration / 1000) / 1e6;
}
public async fetchServerLocations(): Promise<{ [key: string]: string }> {
const res = JSON.parse(await this.get('speed.cloudflare.com', '/locations')); const res = JSON.parse(await this.get('speed.cloudflare.com', '/locations'));
return res.reduce((data, { iata, city }) => { return res.reduce((data, { iata, city }) => {
@ -99,6 +158,20 @@ export class CloudflareSpeed {
return this.request(options); return this.request(options);
} }
public async upload(bytes: number) {
const data = '0'.repeat(bytes);
const options = {
hostname: 'speed.cloudflare.com',
path: '/__up',
method: 'POST',
headers: {
'Content-Length': Buffer.byteLength(data),
},
};
return this.request(options, data);
}
public async request(options, data = '') { public async request(options, data = '') {
let started; let started;
let dnsLookup; let dnsLookup;
@ -150,19 +223,19 @@ export class CloudflareSpeed {
} }
public async fetchCfCdnCgiTrace(): Promise<{ public async fetchCfCdnCgiTrace(): Promise<{
fl: string, fl: string;
h: string, h: string;
ip: string, ip: string;
ts: string, ts: string;
visit_scheme: string, visit_scheme: string;
uag: string, uag: string;
colo: string, colo: string;
http: string, http: string;
loc: string, loc: string;
tls: string, tls: string;
sni: string, sni: string;
warp: string, warp: string;
gateway: string, gateway: string;
}> { }> {
const parseCfCdnCgiTrace = (text) => const parseCfCdnCgiTrace = (text) =>
text text