Last active
February 11, 2025 19:52
-
-
Save bluelovers/895741321d39a5ffa0303083d35c2a7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const os = require('os'); | |
const fs = require('fs'); | |
const crypto = require('crypto'); | |
const { exec } = require("child_process"); | |
// ANSI escape codes for colors | |
const colors = { | |
reset: "\x1b[0m", | |
blue: "\x1b[34m", | |
yellow: "\x1b[33m", | |
green: "\x1b[32m" | |
}; | |
const showEachResult = false; | |
// Function to perform CPU intensive calculations | |
function cpuTest(iterations) { | |
let result = 0; | |
for (let i = 0; i < iterations; i++) { | |
result += Math.sqrt(i) * Math.log(i + 1); | |
} | |
return result; | |
} | |
// Function to perform memory intensive operations | |
function memoryTest(sizeInMB) { | |
const buffer = Buffer.alloc(sizeInMB * 1024 * 1024, 'a'); | |
return buffer.length; | |
} | |
// Function to perform disk I/O operations | |
function diskIoTest(filePath, sizeInMB) { | |
const buffer = Buffer.alloc(sizeInMB * 1024 * 1024, crypto.randomBytes(1).toString('hex')); | |
fs.writeFileSync(filePath, buffer); | |
const readBuffer = fs.readFileSync(filePath); | |
fs.unlinkSync(filePath); | |
return readBuffer.length; | |
} | |
// Function to format bytes or MB into a readable format | |
function formatBytes(bytes, isMB = false) { | |
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | |
let amount = bytes; | |
if (isMB) { | |
amount = bytes * 1024 * 1024; | |
} | |
if (amount === 0) return '0 Byte'; | |
const i = parseInt(Math.floor(Math.log(amount) / Math.log(1024))); | |
return (amount / Math.pow(1024, i)) + ' ' + sizes[i]; | |
} | |
function formatSeconds(nanoseconds) { | |
const seconds = nanoseconds / 1e9; | |
return seconds.toFixed(4) +' seconds'; | |
} | |
// Function to run memory tests with different sizes | |
function runMemoryTests(iterations, memorySizesInMB) { | |
console.log(colors.blue + 'Running memory tests...' + colors.reset); | |
const memoryResult = {}; | |
memorySizesInMB.forEach(sizeInMB => { | |
let totalMemoryTime = 0; | |
let maxMemoryTime = 0; | |
let minMemoryTime = Number.MAX_SAFE_INTEGER; | |
for (let i = 1; i <= iterations; i++) { | |
console.log(colors.yellow + `Memory Test Iteration ${i}/${iterations} for ${formatBytes(sizeInMB, true)}` + colors.reset); | |
const startMemory = process.hrtime(); | |
const memoryResult = memoryTest(sizeInMB); | |
const endMemory = process.hrtime(startMemory); | |
const memoryTime = endMemory[0] * 1e9 + endMemory[1]; | |
showEachResult && console.log(`Memory Test Time : ${memoryTime} nanoseconds`); | |
totalMemoryTime += memoryTime; | |
maxMemoryTime = Math.max(maxMemoryTime, memoryTime); | |
minMemoryTime = Math.min(minMemoryTime, memoryTime); | |
showEachResult && console.log(`Memory Test Result : ${formatBytes(memoryResult)}`); | |
} | |
const avgMemoryTime = totalMemoryTime / iterations; | |
memoryResult[sizeInMB] = [avgMemoryTime, maxMemoryTime, minMemoryTime]; | |
// console.log(colors.green + `Memory tests for ${formatBytes(sizeInMB, true)} completed.` + colors.reset); | |
// console.log(`Average Memory Test Time : ${formatSeconds(avgMemoryTime)}, Max: ${formatSeconds(maxMemoryTime)}, Min: ${formatSeconds(minMemoryTime)}`); | |
}); | |
console.log(colors.green + 'Memory tests completed.' + colors.reset); | |
return () => { | |
Object.entries(memoryResult).forEach(([sizeInMB, [avgMemoryTime, maxMemoryTime, minMemoryTime]]) => { | |
console.log(colors.yellow + `Memory Test Results for ${formatBytes(sizeInMB, true)}:` + colors.reset); | |
console.log(`Average Memory Test Time : ${formatSeconds(avgMemoryTime)}, Max: ${formatSeconds(maxMemoryTime)}, Min: ${formatSeconds(minMemoryTime)}`); | |
}); | |
} | |
} | |
// Main function to run multiple performance tests and calculate averages | |
function runPerformanceTests(iterations) { | |
const cpuIterations = 1e7; // Adjust based on your CPU performance | |
const diskIoSizeInMB = 512; // 512 MB disk I/O test | |
const tempFilePath = './temp-test-file.bin'; | |
const secFixed = 4; | |
let totalCpuTime = 0; | |
let totalDiskIoTime = 0; | |
let maxCpuTime = 0; | |
let minCpuTime = Number.MAX_SAFE_INTEGER; | |
let maxDiskIoTime = 0; | |
let minDiskIoTime = Number.MAX_SAFE_INTEGER; | |
console.log(colors.blue + 'Running performance tests...' + colors.reset); | |
for (let i = 1; i <= iterations; i++) { | |
console.log(colors.yellow + `Iteration ${i}/${iterations}` + colors.reset); | |
const startCpu = process.hrtime(); | |
const cpuResult = cpuTest(cpuIterations); | |
const endCpu = process.hrtime(startCpu); | |
const cpuTime = endCpu[0] * 1e9 + endCpu[1]; | |
showEachResult && console.log(`CPU Test Time : ${cpuTime} nanoseconds`); | |
totalCpuTime += cpuTime; | |
maxCpuTime = Math.max(maxCpuTime, cpuTime); | |
minCpuTime = Math.min(minCpuTime, cpuTime); | |
const startDiskIo = process.hrtime(); | |
const diskIoResult = diskIoTest(tempFilePath, diskIoSizeInMB); | |
const endDiskIo = process.hrtime(startDiskIo); | |
const diskIoTime = endDiskIo[0] * 1e9 + endDiskIo[1]; | |
showEachResult && console.log(`Disk I/O Test Time : ${diskIoTime} nanoseconds`); | |
totalDiskIoTime += diskIoTime; | |
maxDiskIoTime = Math.max(maxDiskIoTime, diskIoTime); | |
minDiskIoTime = Math.min(minDiskIoTime, diskIoTime); | |
showEachResult && console.log(`CPU Test Result : ${cpuResult}`); | |
showEachResult && console.log(`Disk I/O Test Result: ${formatBytes(diskIoResult)}`); | |
} | |
const avgCpuTime = totalCpuTime / iterations; | |
const avgDiskIoTime = totalDiskIoTime / iterations; | |
console.log(colors.green + 'Performance tests completed.' + colors.reset); | |
return () => { | |
console.log(`Average CPU Test Time : ${formatSeconds(avgCpuTime)}, Max: ${formatSeconds(maxCpuTime)}, Min: ${formatSeconds(minCpuTime)}`); | |
console.log(`Average Disk I/O Test Time : ${formatSeconds(avgDiskIoTime)}, Max: ${formatSeconds(maxDiskIoTime)}, Min: ${formatSeconds(minDiskIoTime)}`); | |
} | |
} | |
// Run performance tests multiple times | |
const testIterations = 5; // Number of times to run the tests | |
// Run CPU and Disk I/O tests | |
const r1 = runPerformanceTests(testIterations); | |
// Run Memory tests with different sizes | |
const memorySizesInMB = [1024, 1024 * 2, 1024 * 3]; | |
const r2 = runMemoryTests(testIterations, memorySizesInMB); | |
r1(); | |
r2(); | |
console.log(colors.blue + 'System Information:' + colors.reset); | |
console.log(`Operating System: ${os.platform()} ${os.release()}`); | |
console.log(`CPU : ${os.cpus()[0].model}`); | |
console.log(`CPU Cores : ${os.cpus().length}`); | |
console.log(`CPU Architecture: ${os.arch()}`); | |
console.log(`Total Memory : ${formatBytes(os.totalmem())}`); | |
console.log(`Free Memory : ${formatBytes(os.freemem())}`); | |
console.log(`Uptime : ${formatSeconds(os.uptime() * 1000)}`); | |
exec("wmic path win32_VideoController get name", (error, stdout, stderr) => { | |
if (error) { | |
console.log(`error: ${error.message}`); | |
return; | |
} | |
if (stderr) { | |
console.log(`stderr: ${stderr}`); | |
return; | |
} | |
// Normalise the result here to get the GPU name | |
console.log(`GPU ${stdout}`); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { performance } = require('perf_hooks'); | |
// ANSI escape codes for colors | |
const colors = { | |
reset: "\x1b[0m", | |
bright: "\x1b[1m", | |
dim: "\x1b[2m", | |
underscore: "\x1b[4m", | |
blink: "\x1b[5m", | |
reverse: "\x1b[7m", | |
hidden: "\x1b[8m", | |
fg: { | |
black: "\x1b[30m", | |
red: "\x1b[31m", | |
green: "\x1b[32m", | |
yellow: "\x1b[33m", | |
blue: "\x1b[34m", | |
magenta: "\x1b[35m", | |
cyan: "\x1b[36m", | |
white: "\x1b[37m", | |
crimson: "\x1b[38m" // Scarlet | |
}, | |
bg: { | |
black: "\x1b[40m", | |
red: "\x1b[41m", | |
green: "\x1b[42m", | |
yellow: "\x1b[43m", | |
blue: "\x1b[44m", | |
magenta: "\x1b[45m", | |
cyan: "\x1b[46m", | |
white: "\x1b[47m", | |
crimson: "\x1b[48m" | |
} | |
}; | |
// Sample NPU function to test performance | |
function simulateNPUCalculation() { | |
// Simulate an NPU task (e.g., matrix multiplication, neural network inference) | |
const size = 100; | |
const matrixA = Array.from({ length: size }, () => Array(size).fill(1)); | |
const matrixB = Array.from({ length: size }, () => Array(size).fill(1)); | |
const result = Array.from({ length: size }, () => Array(size).fill(0)); | |
for (let i = 0; i < size; i++) { | |
for (let j = 0; j < size; j++) { | |
for (let k = 0; k < size; k++) { | |
result[i][j] += matrixA[i][k] * matrixB[k][j]; | |
} | |
} | |
} | |
return result; | |
} | |
// Function to test NPU performance | |
function testNPUPerformance(iterations) { | |
console.log(`${colors.fg.blue}Starting NPU performance test with ${iterations} iterations...${colors.reset}`); | |
const start = performance.now(); | |
for (let i = 0; i < iterations; i++) { | |
simulateNPUCalculation(); | |
} | |
const end = performance.now(); | |
const duration = end - start; | |
console.log(`${colors.fg.green}NPU performance test completed.${colors.reset}`); | |
console.log(`${colors.fg.yellow}Total time taken: ${duration.toFixed(4)} milliseconds${colors.reset}`); | |
console.log(`${colors.fg.cyan}Average time per iteration: ${(duration / iterations).toFixed(4)} milliseconds${colors.reset}`); | |
} | |
// Number of iterations to test | |
const iterations = 10; | |
// Run performance test | |
testNPUPerformance(iterations); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment