@maxpert/DebounceSimulator
Nodejs

No description

fork
loading
repl talk
(games)
Files
  • index.js
  • debouncer.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
/* Reference debouncer implementation as descibed by article "Avoiding cache stampede at DoorDash" https://blog.doordash.com/avoiding-cache-stampede-at-doordash-55bbf596d94b

This code simulates and logs stats of potential reads debounced. Try playing around with varion parameters in object below.
*/

const Debouncer = require('./debouncer');
const debouncer = new Debouncer();

function delay(ms) {
	return new Promise(resolve => setTimeout(resolve, ms));
}

async function fetchMenu(id, config) {
    // Simulate fetch load
	await delay(config.latency + Math.random() * config.jitter); 
	return [
        // Imagine some results
    ];
}

async function handleRequest(id, db_config) {
	return await debouncer.debounce(`fetch-${id}`, (id) => fetchMenu(id, db_config));
}

function print_stats(stats) {
	console.log('-------');
	console.log('Debouncer stats');
    console.log(stats);
	console.log(
		`Debounced: ${Math.round((stats.debounced / stats.invoked) * 100)}%`
	);
	console.log('-------');
}

async function startSimulation(configuration) {
	for (let e = 0; e < configuration.epochs; e++) {
		console.log('Generating random requests...');
		const unique_requests_ids = new Set();
		for (let j = 0; j < configuration.requestsPerEpoch; j++) {
			const id = Math.floor(Math.random() * configuration.uniqueRequests);
			unique_requests_ids.add(id);
			handleRequest(id, configuration.db_simulation);
		}

        const waitFor = configuration.epochDelay + Math.random() * configuration.epochDelayJitter;
		console.log('Generated', unique_requests_ids.size, 'unique IDs over ', configuration.requestsPerEpoch, 'parallel requests. Wait for', waitFor, 'ms');
		await delay(waitFor);
        print_stats(debouncer.stats);
	}
}

startSimulation({
    epochs: 200,
    epochDelay: 20,
	epochDelayJitter: 10,
    requestsPerEpoch: 200,
	uniqueRequests: 250,
    db_simulation: {
        latency: 20,
        jitter: 50
    }
});
node v9.7.1 linux/amd64