Skip to content

Commit

Permalink
Update 2.0
Browse files Browse the repository at this point in the history
Improved Speed
  • Loading branch information
Panonim committed Jul 21, 2024
1 parent 9ea3f6f commit 61f70c9
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 25 deletions.
5 changes: 5 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


EXCEPTION FOR:
benchmark.js
WICH IS UNDER © 2024 Artur Flis, All rights saved.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ Simple command.
node index.js
```

## Release 1.0
## Release 2.0
Improved script speed from:
```
`11.32649 s (11326.49 ms)
to:
7.59787 s (7597.87 ms)
```
This number is based on 100 taken tests
<details>
<summary>SHA-512</summary>
<br>

```
ce4cf603fe965655c3d3930ad0f59738130387121fc5fa998e0d7846f7c1169034c79a98a64306867603d348c6d4f69ca568983019e8722f8b3fc84a5904eaa2 index.js
ce4cf603fe965655c3d3930ad0f59738130387121fc5fa998e0d7846f7c1169034c79a98a64306867603d348c6d4f69ca568983019e8722f8b3fc84a5904eaa2 index_old.js
b5af31ff4bb30b1436df8b42e697756a7c8332eaed0d4e391f58a0a6f8ccd5b959a4dd39c176552b6b5f749b6615a37c7ca6e49c0124113f08d132df05eb65b3 index.js
a8384d5d2c9c3c021d08923ba26e724e649272d6cef530dacc58dc7e60fecfd877135c509666dfc8c1d9f8bc63f604fd3c746c2ac579acac713f8bb3d35631ab benchmark.js
```
</details>
110 changes: 110 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// © 2024 Artur Flis, All rights saved

const { exec } = require('child_process');
const path = require('path');
const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

// Function to ask a question with a default value
function askQuestion(query, defaultValue) {
return new Promise(resolve => {
rl.question(`${query} [${defaultValue}]: `, (answer) => {
resolve(answer.trim() || defaultValue);
});
});
}

// Function to run benchmark on a single script
function runBenchmark(scriptName, numRuns) {
return new Promise((resolve, reject) => {
const scriptPath = path.resolve(__dirname, scriptName);
let durations = [];
let completedRuns = 0;

function runNext(runNumber) {
if (runNumber > numRuns) {
const totalDuration = durations.reduce((acc, duration) => acc + duration, 0);
const averageDuration = totalDuration / durations.length;
resolve({ scriptName, averageDuration });
return;
}

const startTime = process.hrtime();
exec(`node ${scriptPath}`, (error) => {
if (error) {
reject(`Error running ${scriptName}: ${error.message}`);
return;
}
const endTime = process.hrtime(startTime);
const duration = endTime[0] + endTime[1] / 1e9; // Convert to seconds
durations.push(duration);
completedRuns++;

// Print progress
console.log(`Running ${scriptName}: ${completedRuns}/${numRuns} completed`);

runNext(runNumber + 1);
});
}

runNext(1);
});
}

// Function to wait for the user to press Enter
function waitForExit() {
return new Promise(resolve => {
rl.question('Press Enter to exit...', () => {
resolve();
});
});
}

async function main() {
try {
// Ask user for the number of runs with default value of 100
const numRuns = parseInt(await askQuestion('Enter the number of tests', '100'), 10);
if (isNaN(numRuns) || numRuns <= 0) {
console.log('Invalid number of tests. Exiting.');
rl.close();
return;
}

// Ask user for the file names
const scriptsInput = await askQuestion('Enter the script names separated by commas', '');
const scriptNames = scriptsInput.split(',').map(name => name.trim());

if (scriptNames.length === 0) {
console.log('No scripts provided. Exiting.');
rl.close();
return;
}

// Run benchmark and collect results
const results = [];
for (const scriptName of scriptNames) {
console.log(`Starting benchmark for ${scriptName}...`);
const result = await runBenchmark(scriptName, numRuns);
results.push(result);
}

// Print results
console.log('\nBenchmark Results:');
results.forEach(({ scriptName, averageDuration }) => {
console.log(`Average execution time for ${scriptName}: ${averageDuration} seconds`);
});

// Wait for user to press Enter before exiting
await waitForExit();
} catch (error) {
console.error('An error occurred:', error);
} finally {
rl.close();
}
}

main();
58 changes: 35 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,38 @@ const tasks = [

// Loading bar
async function displaySystemInfo() {
const bar = new ProgressBar('Fetching system information :percent [:bar ] :elapseds', {
complete: '',
incomplete: '',
width: 20,
const bar = new ProgressBar('Fetching system information :percent [:bar] :elapseds', {
complete: '#',
incomplete: ' ',
width: 10,
total: tasks.length
});

// Handle error
setImmediate(() => {
bar.tick(0); // progress bar starts at 0%
});

// Handle errors
const results = {};
const promises = tasks.map(({ name, fn }) => {
return fn()
.then(data => {
results[name] = data;
bar.tick(); // Update the progress bar
})
.catch(error => {
console.error(`Error fetching ${name} information:`, error);
results[name] = null;
bar.tick(); // Update the progress bar even if there's an error
});
});

for (let i = 0; i < tasks.length; i++) {
const { name, fn } = tasks[i];
try {
results[name] = await fn();
bar.tick();
} catch (error) {
console.error(`Error fetching ${name} information:`, error)
}
}
await Promise.all(promises);

// Print info
console.log('\n===============================');
console.log(' PC Specifications ');
console.log('===============================');
console.log('\n===============================');
console.log(' PC Specifications ');
console.log('===============================');
const cpu = results.CPU;
const gpu = results.GPU;
const memory = results.Memory;
Expand All @@ -46,16 +54,20 @@ async function displaySystemInfo() {
console.log(`Cores: ${cpu.cores}`);
console.log('-------------------------------');
console.log('GPU:');
gpu.controllers.forEach((controller, index) => {
console.log(` ${index + 1}. ${controller.model} (${controller.vram} MB)`);
});
if (gpu && gpu.controllers) {
gpu.controllers.forEach((controller, index) => {
console.log(` ${index + 1}. ${controller.model} (${controller.vram} MB)`);
});
}
console.log('-------------------------------');
console.log(`RAM: ${(memory.total / (1024 ** 3)).toFixed(2)} GB`);
console.log('-------------------------------');
console.log('Storage:');
storage.forEach((disk, index) => {
console.log(` ${index + 1}. ${disk.name} ${disk.type} (${(disk.size / (1024 ** 3)).toFixed(2)} GB)`);
});
if (storage) {
storage.forEach((disk, index) => {
console.log(` ${index + 1}. ${disk.name} ${disk.type} (${(disk.size / (1024 ** 3)).toFixed(2)} GB)`);
});
}
console.log('-------------------------------');
console.log(`OS: ${os.distro} ${os.release}`);
}
Expand Down
63 changes: 63 additions & 0 deletions index_old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const si = require('systeminformation');
const ProgressBar = require('progress');

// List of information functions to get
const tasks = [
{ name: 'CPU', fn: si.cpu },
{ name: 'GPU', fn: si.graphics },
{ name: 'Memory', fn: si.mem },
{ name: 'Storage', fn: si.diskLayout },
{ name: 'OS', fn: si.osInfo }
];

// Loading bar
async function displaySystemInfo() {
const bar = new ProgressBar('Fetching system information :percent [:bar] :elapseds', {
complete: '#',
incomplete: ' ',
width: 10,
total: tasks.length
});

// Handle error
const results = {};

for (let i = 0; i < tasks.length; i++) {
const { name, fn } = tasks[i];
try {
results[name] = await fn();
bar.tick();
} catch (error) {
console.error(`Error fetching ${name} information:`, error)
}
}

// Print info
console.log('\n===============================');
console.log(' PC Specifications ');
console.log('===============================');
const cpu = results.CPU;
const gpu = results.GPU;
const memory = results.Memory;
const storage = results.Storage;
const os = results.OS;

console.log(`CPU: ${cpu.manufacturer} ${cpu.brand} (${cpu.speed} GHz)`);
console.log(`Cores: ${cpu.cores}`);
console.log('-------------------------------');
console.log('GPU:');
gpu.controllers.forEach((controller, index) => {
console.log(` ${index + 1}. ${controller.model} (${controller.vram} MB)`);
});
console.log('-------------------------------');
console.log(`RAM: ${(memory.total / (1024 ** 3)).toFixed(2)} GB`);
console.log('-------------------------------');
console.log('Storage:');
storage.forEach((disk, index) => {
console.log(` ${index + 1}. ${disk.name} ${disk.type} (${(disk.size / (1024 ** 3)).toFixed(2)} GB)`);
});
console.log('-------------------------------');
console.log(`OS: ${os.distro} ${os.release}`);
}

displaySystemInfo();

0 comments on commit 61f70c9

Please sign in to comment.