-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
321 lines (274 loc) · 10.5 KB
/
app.js
File metadata and controls
321 lines (274 loc) · 10.5 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const fs = require('fs');
const { pipeline } = require('stream/promises');
const path = require('path');
// Konfiguracja
const DEFAULT_FILE_SIZE = 100 * 1024 * 1024; // 100MB
const DEFAULT_BUFFER_SIZE = 64 * 1024; // 64KB
const DEFAULT_SLOW_DELAY = 10; // ms opóźnienia przy wolnym zapisie
/**
* Monitoruje zużycie pamięci i loguje statystyki
*/
function logMemoryUsage(label) {
const usage = process.memoryUsage();
console.log(`\n📊 ${label}:`);
console.log(` RSS: ${(usage.rss / 1024 / 1024).toFixed(2)} MB`);
console.log(` Heap Used: ${(usage.heapUsed / 1024 / 1024).toFixed(2)} MB`);
console.log(` Heap Total: ${(usage.heapTotal / 1024 / 1024).toFixed(2)} MB`);
console.log(` External: ${(usage.external / 1024 / 1024).toFixed(2)} MB`);
}
/**
* CZĘŚĆ 1: DEMONSTRACJA PROBLEMU BACKPRESSURE
*
* Problem: Gdy szybko czytamy z pliku i wolno zapisujemy, bufor może się przepełnić.
* Bez obsługi backpressure, Node.js będzie przechowywał wszystkie dane w pamięci,
* co może prowadzić do problemów z pamięcią.
*/
async function demonstrateBackpressureProblem(inputFile, outputFile, slowMode = false) {
console.log('\n🔴 CZĘŚĆ 1: Problem backpressure');
console.log('=====================================');
console.log(`Plik wejściowy: ${inputFile}`);
console.log(`Plik wyjściowy: ${outputFile}`);
console.log(`Tryb wolny: ${slowMode ? 'TAK' : 'NIE'}`);
logMemoryUsage('Pamięć przed rozpoczęciem');
const startTime = Date.now();
// Tworzymy strumień odczytu - SZYBKI
const readStream = fs.createReadStream(inputFile, {
highWaterMark: DEFAULT_BUFFER_SIZE // Rozmiar bufora wewnętrznego
});
// Tworzymy strumień zapisu - WOLNY (symulowany przez opóźnienie)
const writeStream = fs.createWriteStream(outputFile, {
highWaterMark: DEFAULT_BUFFER_SIZE
});
let bytesRead = 0;
let bytesWritten = 0;
let bufferBackpressure = 0;
// Jeśli tryb wolny, używamy Transform stream do symulacji opóźnienia
let actualWriteStream = writeStream;
if (slowMode) {
const Transform = require('stream').Transform;
const slowTransform = new Transform({
transform(chunk, encoding, callback) {
bytesWritten += chunk.length;
// Symulacja wolnego zapisu - dodajemy opóźnienie
setTimeout(() => {
callback(null, chunk);
}, DEFAULT_SLOW_DELAY);
}
});
// Pipe przez slow transform do write stream
slowTransform.pipe(writeStream);
actualWriteStream = slowTransform;
}
// Monitorowanie problemu backpressure
readStream.on('data', (chunk) => {
bytesRead += chunk.length;
// Sprawdzamy, czy bufor zapisu jest pełny (backpressure)
if (!actualWriteStream.write(chunk)) {
bufferBackpressure++;
// Logujemy tylko co 100 eventów, aby nie zaśmiecać konsoli
if (bufferBackpressure === 1 || bufferBackpressure % 100 === 0) {
console.log(`⚠️ Backpressure wykryty! Bufor zapisu pełny (event #${bufferBackpressure})`);
console.log(` Przerwano odczyt, czekamy na opróżnienie bufora...`);
}
// ZATRZYMY odczyt do momentu, aż bufor zapisu się opróżni
readStream.pause();
} else {
if (!slowMode) {
bytesWritten += chunk.length;
}
}
});
// Gdy bufor zapisu jest gotowy na więcej danych
actualWriteStream.on('drain', () => {
// Logujemy tylko co 100 eventów
if (bufferBackpressure % 100 === 0 || bufferBackpressure < 100) {
console.log(`✅ Bufor zapisu opróżniony. Wznowienie odczytu...`);
}
readStream.resume(); // Wznawiamy odczyt
});
return new Promise((resolve, reject) => {
readStream.on('end', () => {
console.log(`\n✅ Odczyt zakończony: ${(bytesRead / 1024 / 1024).toFixed(2)} MB`);
actualWriteStream.end();
});
writeStream.on('finish', () => {
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
// Pobieramy rzeczywisty rozmiar zapisanego pliku
const stats = fs.existsSync(outputFile) ? fs.statSync(outputFile) : null;
const writtenSize = stats ? stats.size : bytesWritten;
console.log(`\n✅ Zapis zakończony: ${(writtenSize / 1024 / 1024).toFixed(2)} MB`);
console.log(`⏱️ Czas wykonania: ${duration}s`);
console.log(`⚠️ Liczba wystąpień backpressure: ${bufferBackpressure}`);
logMemoryUsage('Pamięć po zakończeniu');
resolve();
});
readStream.on('error', reject);
actualWriteStream.on('error', reject);
writeStream.on('error', reject);
});
}
/**
* CZĘŚĆ 2A: ROZWIĄZANIE Z PIPELINE() - AUTOMATYCZNA OBSŁUGA BACKPRESSURE
*
* pipeline() automatycznie obsługuje backpressure i czyści strumienie.
* Jest to zalecane podejście w Node.js.
*/
async function solutionWithPipeline(inputFile, outputFile, slowMode = false) {
console.log('\n🟢 CZĘŚĆ 2A: Rozwiązanie z pipeline()');
console.log('=====================================');
console.log(`Plik wejściowy: ${inputFile}`);
console.log(`Plik wyjściowy: ${outputFile}`);
console.log(`Tryb wolny: ${slowMode ? 'TAK' : 'NIE'}`);
logMemoryUsage('Pamięć przed rozpoczęciem');
const startTime = Date.now();
const readStream = fs.createReadStream(inputFile, {
highWaterMark: DEFAULT_BUFFER_SIZE
});
const writeStream = fs.createWriteStream(outputFile, {
highWaterMark: DEFAULT_BUFFER_SIZE
});
let totalBytes = 0;
// Monitorowanie przepływu danych
readStream.on('data', (chunk) => {
totalBytes += chunk.length;
if (totalBytes % (10 * 1024 * 1024) === 0) {
console.log(` Przetworzono: ${(totalBytes / 1024 / 1024).toFixed(2)} MB`);
}
});
// Symulacja wolnego zapisu (opcjonalnie)
if (slowMode) {
const Transform = require('stream').Transform;
const slowTransform = new Transform({
transform(chunk, encoding, callback) {
setTimeout(() => {
callback(null, chunk);
}, DEFAULT_SLOW_DELAY);
}
});
// Używamy pipeline z transform stream do symulacji opóźnienia
await pipeline(readStream, slowTransform, writeStream);
} else {
// pipeline automatycznie obsługuje backpressure
await pipeline(readStream, writeStream);
}
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
console.log(`\n✅ Pipeline zakończony pomyślnie`);
console.log(` Przetworzono: ${(totalBytes / 1024 / 1024).toFixed(2)} MB`);
console.log(`⏱️ Czas wykonania: ${duration}s`);
logMemoryUsage('Pamięć po zakończeniu');
}
/**
* CZĘŚĆ 2B: ROZWIĄZANIE Z PIPE() - AUTOMATYCZNA OBSŁUGA BACKPRESSURE
*
* pipe() również automatycznie obsługuje backpressure, ale wymaga ręcznego
* czyszczenia strumieni w przypadku błędów.
*/
async function solutionWithPipe(inputFile, outputFile, slowMode = false) {
console.log('\n🟢 CZĘŚĆ 2B: Rozwiązanie z pipe()');
console.log('=====================================');
console.log(`Plik wejściowy: ${inputFile}`);
console.log(`Plik wyjściowy: ${outputFile}`);
console.log(`Tryb wolny: ${slowMode ? 'TAK' : 'NIE'}`);
logMemoryUsage('Pamięć przed rozpoczęciem');
const startTime = Date.now();
const readStream = fs.createReadStream(inputFile, {
highWaterMark: DEFAULT_BUFFER_SIZE // Możemy dostosować rozmiar bufora
});
const writeStream = fs.createWriteStream(outputFile, {
highWaterMark: DEFAULT_BUFFER_SIZE
});
let totalBytes = 0;
readStream.on('data', (chunk) => {
totalBytes += chunk.length;
if (totalBytes % (10 * 1024 * 1024) === 0) {
console.log(` Przetworzono: ${(totalBytes / 1024 / 1024).toFixed(2)} MB`);
}
});
// Symulacja wolnego zapisu (opcjonalnie)
if (slowMode) {
const Transform = require('stream').Transform;
const slowTransform = new Transform({
transform(chunk, encoding, callback) {
setTimeout(() => {
callback(null, chunk);
}, DEFAULT_SLOW_DELAY);
}
});
readStream.pipe(slowTransform).pipe(writeStream);
} else {
// pipe() automatycznie obsługuje backpressure
readStream.pipe(writeStream);
}
return new Promise((resolve, reject) => {
writeStream.on('finish', () => {
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
console.log(`\n✅ Pipe zakończony pomyślnie`);
console.log(` Przetworzono: ${(totalBytes / 1024 / 1024).toFixed(2)} MB`);
console.log(`⏱️ Czas wykonania: ${duration}s`);
logMemoryUsage('Pamięć po zakończeniu');
resolve();
});
readStream.on('error', (err) => {
readStream.destroy();
writeStream.destroy();
reject(err);
});
writeStream.on('error', (err) => {
readStream.destroy();
writeStream.destroy();
reject(err);
});
});
}
/**
* Główna funkcja - parsowanie argumentów CLI i uruchomienie odpowiedniego scenariusza
*/
async function main() {
const args = process.argv.slice(2);
const slowMode = args.includes('--slow');
const solution = args.find(arg => arg.startsWith('--solution='))?.split('=')[1] || 'problem';
const inputFile = path.join(__dirname, 'large.txt');
const outputFile1 = path.join(__dirname, 'output1.txt');
const outputFile2 = path.join(__dirname, 'output2.txt');
// Sprawdzamy, czy plik testowy istnieje
if (!fs.existsSync(inputFile)) {
console.error(`❌ Plik testowy ${inputFile} nie istnieje!`);
console.log('💡 Uruchom najpierw: npm run generate-file');
process.exit(1);
}
try {
switch (solution) {
case 'problem':
await demonstrateBackpressureProblem(inputFile, outputFile1, slowMode);
break;
case 'pipeline':
await solutionWithPipeline(inputFile, outputFile2, slowMode);
break;
case 'pipe':
await solutionWithPipe(inputFile, outputFile2, slowMode);
break;
case 'all':
await demonstrateBackpressureProblem(inputFile, outputFile1, slowMode);
await solutionWithPipeline(inputFile, outputFile2, slowMode);
break;
default:
console.error(`❌ Nieznana opcja: --solution=${solution}`);
console.log('Dostępne opcje: problem, pipeline, pipe, all');
process.exit(1);
}
console.log('\n✅ Wszystkie operacje zakończone pomyślnie!');
} catch (error) {
console.error('\n❌ Błąd:', error.message);
process.exit(1);
}
}
// Uruchomienie, jeśli plik jest wykonywany bezpośrednio
if (require.main === module) {
main();
}
module.exports = {
demonstrateBackpressureProblem,
solutionWithPipeline,
solutionWithPipe,
logMemoryUsage
};