-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtest.js
More file actions
143 lines (135 loc) · 6.4 KB
/
test.js
File metadata and controls
143 lines (135 loc) · 6.4 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
138
139
140
141
142
143
/* -----------------------------------------------------------------------------
Trying to figure out how to print a single line with an incrementing variable.
Would be awesome to figure out how to get multiple lines to update with just
the variable so I could log out the four lines in the HDDseek script as the
values increment, e.g.
`Target for data sought: ${dataSizeKB}KB`); <~~~ static value
`Total data sought: ${data}KB\n`); <~~~ UPDATING value
`Target # of blocks: ${target}`); <~~~ static value
`Total blocks processed: ${blocks}\n`); <~~~ UPDATING value
----------------------------------------------------------------------------- */
// const process = require('process');
//
// let x = 1;
// process.stdout.write(`The count is now at: ${x}`);
// const counter = () => {
// process.stdout.clearLine();
// process.stdout.cursorTo(0);
// x++;
// process.stdout.write(`The count is now at: ${x}`);
// if(x >= 10) {
// clearInterval(cycle);
// process.stdout.write('\n');
// }
// };
//
// const cycle = setInterval(counter, 1000);
// // variant with "readline" & os.EOL
// const process = require('process');
// const readline = require('readline');
// const os = require('os');
//
// let x = 1;
// process.stdout.write(`The count is now at: ${x}${os.EOL}POOP ${10 - x}`);
// const counter = () => {
// readline.clearLine(process.stdout, 0);
// readline.cursorTo(process.stdout, 0, null);
// x++;
// process.stdout.write(`The count is now at: ${x}${os.EOL}POOP ${10 - x}`);
// if(x >= 10) {
// clearInterval(cycle);
// process.stdout.write('\n');
// }
// };
//
// const cycle = setInterval(counter, 1000);
// // ....and then there's this package:
// const logUpdate = require('log-update');
// const frames = ['-', '\\', '|', '/'];
// let i = 0;
//
// setInterval(() => {
// const frame = frames[i = ++i % frames.length];
//
// logUpdate(
// `
// ♥♥
// ${frame} unicorns ${frame}
// ♥♥
// `
// );
// }, 80);
'use-strict';
/* eslint no-console: 0 */
const process = require('process'); // <~~~ Not needed, just making linter happy
const logUpdate = require('log-update'); // requires: npm install log-update --save-dev
/****************************************************************
SCRIPT REQUIRES: `npm install log-update --save-dev`
invoke: `$ node script [opt: dataSizeMB seekTime_ms blockSizeKB]`
example `$ node HDDseek.js 5 9 4` <--- also DEFAULT arguments
Computer Architecture TWO example: `$ node HDDseek.js 100 9 4`
****************************************************************/
/* 1. file size entered in Megabytes (defaults to 5MB) */
const dataSizeMB = Number(process.argv[2]) || 5;
/* 2. convert MB to KB */
const dataSizeKB = dataSizeMB * 1024;
/* 3. HDD seek time in milliseconds (defauts to 9) */
const seekTime = Number(process.argv[3]) || 9;
/* 4. HDD block size in KB (default 4KB) */
const blockSize = Number(process.argv[4]) || 4;
/* 5. HDD fragmentation */
const fragmentation = process.argv[5] || 100;
const fragPercent = fragmentation / 100;
/* 6. Target number of blocks to seek which contain the data size */
const target = dataSizeKB / blockSize;
/* 7. total ACTUAL data sought in KB */
let data = 0;
/* 8. total number of ACTUAL data blocks sought */
let blocks = 0;
/* 9. program timer */
const timeStart = process.hrtime();
/* 10. HDD seek emulator */
const seek = () => {
if(data >= dataSizeKB) {
clearInterval(cycle);
const blockSeekPerSec = blocks * seekTime / 1000;
console.log(`\n "In THEORY it will take ${blockSeekPerSec} seconds to seek ${dataSizeMB}MB of HDD data while`);
console.log(` constrained to both ${blockSize}KB HDD blocks & an average seek time of ${seekTime} milliseconds.`);
console.log(` Well... ${blockSeekPerSec} seconds if, and only if, the ${dataSizeMB}MB of data is ${fragmentation}% fragmented.`);
console.log(' IT DEPENDS some on the rotational latency, the HDD state at time of the');
console.log(' read request, and some other factors. Of course, we\'re only considering the');
console.log(' seek time and not the read-in time of the head travelling over the block to');
console.log(' deterministically react to the magnetic state of the block, nor the time');
console.log(' travelling over the block relative to the HDD rpm, nor the amount of time');
console.log(' for the read-in signal to be transduced into a signal which is sent along');
console.log(' wires via electrons travelling at sub-lightspeed, and well, there\'s some');
console.log(' considerable calculations to be made to factor in all the marginal aspects.');
const timeEnd = process.hrtime(timeStart);
const timeTotalSeconds = timeEnd[0];
const timeTotalNanoseconds = timeEnd[0] + (timeEnd[1] / 1000000000);
console.log(` In PRACTICE, this simulation took roughly ${timeTotalNanoseconds} seconds. Yeah...`);
console.log(` ${timeTotalNanoseconds.toFixed(2)}... Yeah, about ${timeTotalSeconds}. M-hmm... ${timeTotalNanoseconds} seconds. Yeah. ${timeTotalSeconds}. . . ${timeTotalNanoseconds.toFixed(1)}. . .`);
console.log(` On AVERAGE, that ${seekTime} milliseconds per seek? Ah, ${seekTime}. . . maybe ${(((timeTotalNanoseconds) / target) * 1000).toFixed(0)}? ${(((timeTotalNanoseconds) / target) * 1000).toFixed(1)}, no...`);
console.log(` ${(((timeTotalNanoseconds) / target) * 1000).toFixed(3)} milliseconds. Yeah, definitely ${(((timeTotalNanoseconds) / target) * 1000)} milliseconds, definitely`);
console.log(` per seek it's ${(((timeTotalNanoseconds) / target) * 1000).toFixed(0)}, ${(((timeTotalSeconds + (timeEnd[1] / 1000000000)) / target) * 1000).toFixed(2)} milliseconds, definitely ${(((timeTotalNanoseconds) / target) * 1000)}...aaaaaaAAH!`);
console.log(' HOT WATER BURN BABY!! HOT WATER BURN BABY!!!!"\n');
console.log(' -- "Raymond Babbit" as played by Dustin Hoffman (1998, "Rain Man")\n');
return;
}
// Some fudge
let blocksPerSeek;
if (Number(fragmentation) < 1) {
blocksPerSeek = dataSizeKB;
} else { blocksPerSeek = blockSize / fragPercent; }
logUpdate(`
Target for data sought: ${dataSizeKB}KB
Total data sought: ${data}KB
Target # of blocks: ${target}
Total blocks processed: ${blocks}
Percentage completed: ${((((data / dataSizeKB) + (blocks / target)) / 2) * 100).toFixed(0)}%`);
data += blocksPerSeek;
blocks++;
};
/* 11. loop */
const cycle = setInterval(seek, seekTime);
// Q.E.F