-
Notifications
You must be signed in to change notification settings - Fork 0
/
memorytest.js
58 lines (45 loc) · 1.51 KB
/
memorytest.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
var LRUCache = require('./src/js-cache-lru');
require('console-stamp')(console, 'HH:MM:ss.l');
console.log(process.memoryUsage());
(function () {
'use strict';
function randomString(len) {
var str = '';
for (var i = 0; i < len; ++i) {
str += String.fromCharCode(Math.round(Math.random(100)));
}
return str;
}
function addElements(cache, size) {
for (var i = 0; i < size; ++i) {
cache.set(randomString(512), randomString(1024));
}
return cache;
}
function testNonLeastRecentlyUsedCleaning() {
var cache = new LRUCache(100);
var i = 0;
while (true) {
addElements(cache, Math.round(Math.random() * 100));
if (++i % 100 === 0) {
console.log(`${i} iterations done, memory usage: ${JSON.stringify(process.memoryUsage())}`);
}
}
}
function testCacheAutoCleanup() {
var i = 0;
var caches = [];
setInterval(function () {
var cache = new LRUCache(100, 20);
addElements(cache, Math.round(Math.random() * 100));
if (caches.length > 100) {
caches.splice(0, 100);
}
caches.push(cache);
if (++i % 100 === 0) {
console.log(`${i} iterations done, memory usage: ${JSON.stringify(process.memoryUsage())}`);
}
}, 30);
}
testCacheAutoCleanup();
})();