-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·140 lines (121 loc) · 3.85 KB
/
cli.js
File metadata and controls
executable file
·140 lines (121 loc) · 3.85 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
#!/usr/bin/env node
const libCacheSim = require('./index');
const path = require('path');
// Simple command line argument parser
function parseArgs() {
const args = process.argv.slice(2);
const options = {
trace: null,
type: null,
algorithm: null,
size: null,
help: false,
version: false
};
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case '--trace':
case '-t':
options.trace = args[++i];
break;
case '--type':
options.type = args[++i];
break;
case '--algorithm':
case '-a':
options.algorithm = args[++i];
break;
case '--size':
case '-s':
options.size = args[++i];
break;
case '--help':
case '-h':
options.help = true;
break;
case '--version':
case '-v':
options.version = true;
break;
default:
console.error(`Unknown option: ${args[i]}`);
process.exit(1);
}
}
return options;
}
function showHelp() {
console.log(`
libcachesim-node CLI
Usage:
cachesim-js --trace <path> --type <type> --algorithm <alg> --size <size>
Options:
--trace, -t <path> Path to trace file (required)
--type <type> Trace type (required)
Supported: ${libCacheSim.getSupportedTraceTypes().join(', ')}
--algorithm, -a <alg> Cache algorithm (required)
Supported: ${libCacheSim.getSupportedAlgorithms().join(', ')}
--size, -s <size> Cache size (required)
Examples: 1mb, 512kb, 2gb, 1024 (bytes)
--help, -h Show this help message
--version, -v Show version information
Examples:
cachesim-js -t trace.vscsi --type vscsi -a lru -s 10mb
cachesim-js --trace data.csv --type csv --algorithm s3fifo --size 50mb
`);
}
function main() {
const options = parseArgs();
if (options.help) {
showHelp();
return;
}
if (options.version) {
console.log(`libcachesim-node v${libCacheSim.getVersion()}`);
return;
}
// Check that all required parameters are provided
if (!options.trace || !options.type || !options.algorithm || !options.size) {
console.error('Error: All parameters are required.');
console.error('Missing:');
if (!options.trace) console.error(' --trace <path>');
if (!options.type) console.error(' --type <type>');
if (!options.algorithm) console.error(' --algorithm <alg>');
if (!options.size) console.error(' --size <size>');
console.error('\nUse --help for usage information.');
process.exit(1);
}
try {
// Check if trace file exists
const fs = require('fs');
if (!fs.existsSync(options.trace)) {
console.error(`Error: Trace file '${options.trace}' not found.`);
process.exit(1);
}
console.log(`Running simulation with trace: ${options.trace}`);
console.log(`Algorithm: ${options.algorithm}, Size: ${options.size}, Type: ${options.type}`);
// Run simulation
const result = libCacheSim.runSimulation(
options.trace,
options.type,
options.algorithm,
options.size
);
// Display results
console.log('\n=== Cache Simulation Results ===');
console.log(`Algorithm: ${result.algorithm}`);
console.log(`Cache Size: ${(result.cacheSize / (1024 * 1024)).toFixed(2)} MB`);
console.log(`Total Requests: ${result.totalRequests.toLocaleString()}`);
console.log(`Cache Hits: ${result.hits.toLocaleString()}`);
console.log(`Cache Misses: ${result.misses.toLocaleString()}`);
console.log(`Hit Ratio: ${(result.hitRatio * 100).toFixed(2)}%`);
console.log(`Miss Ratio: ${(result.missRatio * 100).toFixed(2)}%`);
} catch (error) {
console.error('Error running simulation:', error.message);
process.exit(1);
}
}
if (require.main === module) {
main();
}
module.exports = { parseArgs, showHelp, main };