Skip to content

Accelerating File System (fs) Read Write Operations

Hüseyin Tuğrul BÜYÜKIŞIK edited this page Sep 29, 2021 · 2 revisions
"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);

Output for cache size / dataset size = 0.95:

without LRU: 380 milliseconds
without LRU: 373 milliseconds
without LRU: 327 milliseconds
without LRU: 332 milliseconds
without LRU: 305 milliseconds
without LRU: 291 milliseconds
without LRU: 311 milliseconds
without LRU: 296 milliseconds
without LRU: 313 milliseconds
without LRU: 317 milliseconds
with LRU: 95 milliseconds
with LRU: 63 milliseconds
with LRU: 43 milliseconds
with LRU: 45 milliseconds
with LRU: 46 milliseconds
with LRU: 55 milliseconds
with LRU: 50 milliseconds
with LRU: 51 milliseconds
with LRU: 47 milliseconds
with LRU: 216 milliseconds

Clone this wiki locally