-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
94 lines (80 loc) · 3.82 KB
/
bench.js
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
const fs = require('fs');
const zlib = require('zlib');
const generateRevocationList = async (totalCredentials, totalRevoked, testNumber) => {
const filePaths = []
const percentageRevoked = Math.ceil(100 * totalRevoked / totalCredentials);
filePaths.push(`./input.${testNumber}.txt`);
// Make this code async
const file = fs.createWriteStream(`./input.${testNumber}.txt`);
// Push 0 or 1 base don the percentage to input.txt for totalCredentials times
for (let i = 0; i < totalCredentials; i++) {
const random = Math.floor(Math.random() * 100);
if (random < percentageRevoked) {
// Drain - https://stackoverflow.com/questions/50357777/why-does-attempting-to-write-a-large-file-cause-js-heap-to-run-out-of-memory
if (!file.write('1')) {
// Will pause every 16384 iterations until `drain` is emitted
await new Promise(resolve => file.once('drain', resolve));
}
} else {
if (!file.write('0')) {
// Will pause every 16384 iterations until `drain` is emitted
await new Promise(resolve => file.once('drain', resolve));
}
}
}
file.end();
file.on('finish', () => {
// console.log('Finished writing to file');
// Check if the file is generated correctly
const inputFile = fs.readFileSync(`./input.${testNumber}.txt`, 'utf8');
if (inputFile.length === totalCredentials) {
} else {
console.log('Input file length mismatched - Failure');
}
// Compress the file
filePaths.push(`./input.${testNumber}.txt.gz`);
const fileContentsToBeZipped = fs.createReadStream(`./input.${testNumber}.txt`);
const writeStreamToBeZippd = fs.createWriteStream(`./input.${testNumber}.txt.gz`);
const zip = zlib.createGzip();
fileContentsToBeZipped.pipe(zip).pipe(writeStreamToBeZippd);
fileContentsToBeZipped.on('end', () => {
//console.log('Finished compressing file');
// Print compression Ratio
var stats = fs.statSync(`./input.${testNumber}.txt.gz`)
var outputFileSizeInBytes = stats.size;
var stats = fs.statSync(`./input.${testNumber}.txt`)
var inputFileSizeInBytes = stats.size;
console.log(testNumber, totalCredentials, totalRevoked, inputFileSizeInBytes / outputFileSizeInBytes);
filePaths.forEach((filePath) => {
fs.unlinkSync(filePath);
});
// // Decompress the file
// filePaths.push(`./output.${testNumber}.txt`);
// const writeStream = fs.createWriteStream(`./output.${testNumber}.txt`);
// const unzip = zlib.createGunzip();
// fileContents.pipe(unzip).pipe(writeStream);
// fileContents.on('end', () => {
// console.log('Finished decompressing file');
// // Check if the file is decompressed correctly
// const input = fs.readFileSync(`./input.${testNumber}.txt`, 'utf8');
// const output = fs.readFileSync(`./output.${testNumber}.txt`, 'utf8');
// if (input === output) {
// console.log('Input Output Matched - Success');
// // Delete the files
// filePaths.forEach((filePath) => {
// fs.unlinkSync(filePath);
// });
// } else {
// console.log('Input Output Mismatched - Failure', input.length, output.length);
// }
// });
});
});
}
(async () => {
for (let i = 6; i <= 8; i++) {
for (let j = 1; j <= 10; j++) {
await generateRevocationList(10 ** i, Math.ceil(10 ** i / (10 * j)), (i - 6) * 10 + j);
}
}
})();