-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample_caching_filesystem.js
111 lines (96 loc) · 2.18 KB
/
sample_caching_filesystem.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict"
// backing-store
var fs = require("fs");
// LRU cache
let Lru = require("./lrucache.js").Lru;
let num_cache_elements = 950;
let element_life_time_miliseconds = 10000;
let cache = new Lru(num_cache_elements, async function(key,callback){
fs.readFile(key, function(err, buf) {
if (err) console.log(err);
callback(buf.toString());
});
}, element_life_time_miliseconds, async function(key,value,callback){
fs.writeFile(key, value, (err) => {
if (err) console.log(err);
callback();
});
});
const N_repeat = 10;
const N_bench = 2000;
const N_concurrency = 20;
const N_dataset = 1000;
function randomKey(){ return Math.floor(Math.random()*N_dataset); }
// without LRU caching
async function benchWithout(callback){
for(let i=0;i<N_bench;i+=N_concurrency){
let ctr = 0;
let w8 = new Promise((success,fail)=>{
for(let j=0;j<N_concurrency;j++)
{
fs.writeFile("./testfolder/file"+randomKey(),Math.random().toString(), function (err) {
fs.readFile("./testfolder/file"+randomKey(), function (err, result) {
ctr++;
if(ctr == N_concurrency)
{
success(1);
}
});
});
}
});
let result = await w8;
}
callback();
}
// with LRU caching
async function benchWith(callback){
for(let i=0;i<N_bench;i+=N_concurrency){
let ctr = 0;
let w8 = new Promise((success,fail)=>{
for(let j=0;j<N_concurrency;j++)
{
cache.set("./testfolder/file"+randomKey(),Math.random().toString(), function (result) {
cache.get("./testfolder/file"+randomKey(), function (result) {
ctr++;
if(ctr == N_concurrency)
{
success(1);
}
});
});
}
});
let result = await w8;
}
callback();
}
let ctr = 0;
function restartWithoutLRU(callback){
let t = Date.now();
benchWithout(function(){
console.log("without LRU: "+(Date.now() - t)+" milliseconds");
ctr++;
if(ctr != N_repeat)
{
restartWithoutLRU(callback);
}
else
{
ctr=0;
callback();
}
});
}
function restartWithLRU(){
let t = Date.now();
benchWith(function(){
console.log("with LRU: "+(Date.now() - t)+" milliseconds");
ctr++;
if(ctr != N_repeat)
{
restartWithLRU();
}
});
}
restartWithoutLRU(restartWithLRU);