summaryrefslogtreecommitdiff
path: root/platform/node/test/memory.test.js
blob: 997ccdbbe19fc2f773e0739868053b73924ef579 (plain)
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
'use strict';

var mockfs = require('./mockfs');
var mbgl = require('../index');
var test = require('tape');

var testParams = {
    mapPoolSize: 10,
    numRenderings: 100,
    heapGrowthThreshold: 0,
    ratio: 2
};

test('Memory', function(t) {
    // Trigger garbage collection before starting test, then initialize
    // heap size
    if (typeof gc === 'function') gc();
    var lastHeapSize = process.memoryUsage()['heapUsed'];

    var options = {
        request: function(req, callback) {
            callback(null, { data: mockfs.dataForRequest(req) });
        },
        ratio: testParams.ratio,
    };

    var mapPool = []

    for (var i = 0; i < testParams.mapPoolSize; ++i) {
        var map = new mbgl.Map(options);
        mapPool.push(map);
    }

    var renderCount = 0;
    var heapGrowth = 0;

    var interval = setInterval(function () {
        if (mapPool.length == 0) {
            return;
        }

        var map = mapPool.shift();

        if (Math.floor(Math.random() * 2)) {
            map.load(mockfs.style_raster);
        } else {
            map.load(mockfs.style_vector);
        }

        map.render({ zoom: 16 }, function(err, pixels) {
            mapPool.push(map);

            if (renderCount % (testParams.numRenderings / 10) == 0) {
                // Manually trigger garbage collection
                if (typeof gc === 'function') gc();

                var currentHeapSize = process.memoryUsage()['heapUsed'];

                // Print some progress, so slow build bots don't timeout.
                t.comment("Rendering (" + renderCount.toString() +
                    "/" + testParams.numRenderings.toString() + ")");

                heapGrowth = heapGrowth + currentHeapSize - lastHeapSize;
                lastHeapSize = currentHeapSize;
            }

            if (++renderCount == testParams.numRenderings) {
                t.ok(heapGrowth < testParams.heapGrowthThreshold, heapGrowth);
                t.end();

                clearInterval(interval);
            }
        });
    }, 1);
});