-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (122 loc) · 3.75 KB
/
Copy pathindex.js
File metadata and controls
137 lines (122 loc) · 3.75 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
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
const os = require('os');
const benchmark = require('benchmark');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const { exec } = require('child_process');
const FastSpeedtest = require('fast-speedtest-api');
// Get CPU information
const numCPUs = os.cpus().length;
const cpuModel = os.cpus()[0].model;
const totalMemory = os.totalmem();
const freeMemory = os.freemem();
console.log('\nSystem Information:');
console.log(`CPU Model: ${cpuModel}`);
console.log(`Number of CPUs: ${numCPUs}`);
const totalMemoryGB = (totalMemory / (1024 * 1024 * 1024)).toFixed(2);
console.log(`Total Memory: ${totalMemoryGB} GB`);
const freeMemoryGB = (freeMemory / (1024 * 1024 * 1024)).toFixed(2);
console.log(`Free Memory: ${freeMemoryGB} GB`);
// Benchmark suite for CPU and storage tests
const suite = new benchmark.Suite();
const testFileSizeInBytes = 1024 * 1024 * 10; // 10MB file size
const testFilePath = path.join(os.tmpdir(), uuidv4() + '.dat'); // Unique filename in temp dir
// Storage write test
suite.add('Storage Write Test', {
defer: true,
fn: (deferred) => {
fs.writeFile(testFilePath, Buffer.alloc(testFileSizeInBytes), (err) => {
if (err) {
console.error('Error in write test:', err);
deferred.resolve(); // Resolve even on error to continue the suite
} else {
deferred.resolve();
}
});
}
});
// Storage read test
suite.add('Storage Read Test', {
defer: true,
fn: (deferred) => {
fs.readFile(testFilePath, (err, data) => {
if (err) {
console.error('Error in read test:', err);
deferred.resolve(); // Resolve even on error to continue the suite
} else {
deferred.resolve();
}
});
}
});
// Single-core test
suite.add('Single Core Test', () => {
let sum = 0;
for (let i = 0; i < 100000000; i++) {
sum += i;
}
});
// Multi-core test
suite.add('Multi Core Test', {
defer: true,
fn: (deferred) => {
const tasks = [];
for (let i = 0; i < numCPUs; i++) {
tasks.push(new Promise((resolve) => {
let sum = 0;
for (let j = 0; j < 100000000 / numCPUs; j++) { // Divide work across cores
sum += j;
}
resolve(sum);
}));
}
Promise.all(tasks).then(() => deferred.resolve());
}
});
// Cleanup
suite.on('complete', () => {
fs.unlink(testFilePath, (err) => {
if (err) {
console.error('Cleanup error:', err);
} else {
console.log('Temporary file deleted successfully');
}
});
// Run network tests once
runNetworkTests();
});
suite
.on('cycle', (event) => {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ 'async': true }); // Important for async tests
// Function to run network tests once
function runNetworkTests() {
// Speed test
const speedtest = new FastSpeedtest({
token: 'YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm', // Replace with your Fast.com API token
verbose: false,
});
speedtest.getSpeed().then(speed => {
const speedMbps = speed / 1000000; // Convert bps to Mbps
console.log(`Download speed: ${speedMbps.toFixed(2)} Mbps`);
}).catch(err => {
console.error('Speed test error:', err);
}).finally(() => {
// Ping test
const gateway = '8.8.8.8'; // Use a known IP address for ping test
const pingCommand = os.platform() === 'win32' ? `ping -n 3 ${gateway}` : `ping -c 3 ${gateway}`; // -n 3 for Windows, -c 3 for Linux
exec(pingCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Ping error: ${error}`);
} else {
console.log(`Ping output:\n${stdout}`);
}
// Exit the process after all tests are complete
process.exit(0);
});
});
}