-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_scheduler.js
More file actions
executable file
·161 lines (127 loc) · 4.17 KB
/
node_scheduler.js
File metadata and controls
executable file
·161 lines (127 loc) · 4.17 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const fs = require('fs').promises;
// ---------------------------
// CPU Tasks
async function generatePrimes(limit) {
let isPrime = new Array(limit + 1).fill(true);
let primes = [];
for (let p = 2; p * p <= limit; ++p) {
if (isPrime[p]) {
for (let i = p * p; i <= limit; i += p) {
isPrime[i] = false;
}
}
}
for (let p = 2; p <= limit; ++p) {
if (isPrime[p]) {
primes.push(p);
}
}
return primes;
}
async function generateCpuTaskChain(length) {
let new_length = length * 2
for (let i = 0; i < new_length - 1; i++) {
let a = await generatePrimes(10000);
}
return 'Done'
}
// ---------------------------
// IO Tasks
async function openReadFile(filePath) {
try {
const fileHandle = await fs.open(filePath, 'r', fs.constants.O_RDONLY | fs.constants.O_DIRECT | fs.constants.O_SYNC);
return fileHandle;
} catch (error) {
throw new Error(`Error opening read file: ${error}`);
}
}
async function readToBuffer(fileHandle) {
try {
const buffer = await fileHandle.readFile();
return buffer;
} catch (error) {
throw new Error(`Error reading from file: ${error}`);
}
}
async function closeReadFile(fileHandle) {
try {
await fileHandle.close();
} catch (error) {
throw new Error(`Error closing read file: ${error}`);
}
}
async function openWriteFile(filePath) {
try {
const fileHandle = await fs.open(filePath, 'w', fs.constants.O_RDWR | fs.constants.O_CREAT | fs.constants.O_TRUNC | fs.constants.O_DIRECT | fs.constants.O_SYNC);
return fileHandle;
} catch (error) {
throw new Error(`Error opening write file: ${error}`);
}
}
async function writeToFile(fileHandle, data) {
try {
await fileHandle.writeFile(data);
} catch (error) {
throw new Error(`Error writing to file: ${error}`);
}
}
async function closeWriteFile(fileHandle) {
try {
await fileHandle.close();
} catch (error) {
throw new Error(`Error closing write file: ${error}`);
}
}
async function generateIoTaskChain(sourceFilePath, destinationFilePath) {
let readHandle, writeHandle;
try {
// Open the read file
readHandle = await openReadFile(sourceFilePath);
// Read file to buffer
const buffer = await readToBuffer(readHandle);
// Close the read file
await closeReadFile(readHandle);
// Open the write file
writeHandle = await openWriteFile(destinationFilePath);
// Write buffer to file
for (let k = 0; k < 20; k++) {
await writeToFile(writeHandle, buffer);
}
// Close the write file
await closeWriteFile(writeHandle);
} catch (error) {
console.error(error);
}
}
// ---------------------------
// Benchmark functions
async function multipleSchedulerMultiIoCpuTasksBenchmark() {
const start_time = new Date();
let all_tasks = []
for (let i = 0; i < 256; i++) {
all_tasks.push(generateIoTaskChain('logs/log.txt', 'logs/log'+i+'.txt'));
}
for (let i = 0; i < 256; i++) {
all_tasks.push(generateCpuTaskChain(20*20));
}
Promise.all(all_tasks).then(e => console.log('All tasks finished in : ', new Date() - start_time, ' milliseconds.'));
}
async function multipleSchedulerMultiCpuCpuTasksBenchmark() {
const start_time = new Date();
let all_tasks = []
for (let i = 0; i < 512; i++) {
all_tasks.push(generateCpuTaskChain(20*20));
}
Promise.all(all_tasks).then(e => console.log('All tasks finished in : ', new Date() - start_time, ' milliseconds.'));
}
async function multipleSchedulerMultiIoIoTasksBenchmark() {
const start_time = new Date();
let all_tasks = []
for (let i = 0; i < 512; i++) {
all_tasks.push(generateIoTaskChain('logs/log.txt', 'logs/log'+i+'.txt'));
}
Promise.all(all_tasks).then(e => console.log('All tasks finished in : ', new Date() - start_time, ' milliseconds.'));
}
// multipleSchedulerMultiIoCpuTasksBenchmark();
// multipleSchedulerMultiCpuCpuTasksBenchmark();
multipleSchedulerMultiIoIoTasksBenchmark();