summaryrefslogtreecommitdiffstats
path: root/docs/tests/hntrie-test.html
blob: ada8ecd68b48b4ebbbdf5c65bd3c89ac2fa099a2 (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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="font: 14px sans-serif">
<h1>HNTrieContainer test</h1>
<p><a href="./.">Back</a></p>
<p>&nbsp;</p>
<div><button id="test" type="button">Test!</button></div>
<div id="stdout"></div>
<script src="https://rawcdn.githack.com/gorhill/uBlock/1b6fea16da81d1df3e2efd5a31894f71ea04dbb1/src/js/hntrie.js"></script>
<!-- <script src="../../src/js/hntrie.js"></script> -->
<script src="hostname-pool.js"></script>
<script>
const createRandomLabel = function() {
    return Math.random().toString(36).slice(2);
};

const stdout = function(s) {
    const line = document.createElement('div');
    const parent = document.getElementById('stdout');
    let tooManyErrors = false;
    if ( parent.childElementCount > 100 ) {
        tooManyErrors = true;
        line.textContent = 'Too many errors, aborting';
    } else {
        line.textContent = s;
    }
    parent.appendChild(line)
    if ( tooManyErrors ) {
        throw 'Aborting';
    }
}

/******************************************************************************/

// Dictionary of hostnames
//
const FilterHostnameDict = function(hostnames) {
    this.h = ''; // short-lived register
    this.dict = new Set();
    if ( hostnames !== undefined ) {
        this.fromIterable(hostnames);
    }
};

FilterHostnameDict.prototype = {
    add: function(hn) {
        if ( this.dict.has(hn) ) { return false; }
        this.dict.add(hn);
        return true;
    },
    fromIterable: function(hostnames) {
        for ( let hn of hostnames ) {
            this.add(hn);
        }
        return this;
    },
    matches: function(needle) {
        while ( this.dict.has(needle) === false ) {
            const pos = needle.indexOf('.');
            if ( pos === -1 ) {
                this.h = '';
                return false;
            }
            needle = needle.slice(pos + 1);
        }
        this.h = needle;
        return true;
    },
};

/******************************************************************************/

const testFlavor = function(hostnames, name, matchesFn, hitFn) {
    stdout('\xA0');
    stdout('Testing ' + name + '...');

    const t0 = performance.now();

    for ( let i = 0; i < hostnames.length; i++ ) {
        // Exact hits
        let needle = hostnames[i];
        if ( hitFn(matchesFn(needle)) === false ) {
            stdout('Exact hits failed: ' + needle);
        }

        // Subdomain hits
        needle = createRandomLabel() + '.' + hostnames[i];
        if ( hitFn(matchesFn(needle)) === false ) {
            stdout('Subdomain hits failed: ' + needle);
        }

        // Misses batch 1
        needle = createRandomLabel() + '.com';
        if ( hitFn(matchesFn(needle)) !== false ) {
            stdout('Misses batch 1: ' + needle);
        }

        // Misses batch 2
        needle = hostnames[i] + '.' + createRandomLabel();
        if ( hitFn(matchesFn(needle)) !== false ) {
            stdout('Misses batch 2: ' + needle);
        }

        // Misses batch 3
        needle = hostnames[i];
        let pos = needle.lastIndexOf('.');
        if ( pos !== -1 ) {
            needle = needle.slice(0, pos) + needle.slice(pos + 1);
            if ( hitFn(matchesFn(needle)) !== false ) {
                stdout('Misses batch 3: ' + needle);
            }
        }
    }

    const t1 = performance.now();

    stdout(
        name + ': ' +
        (hostnames.length * 5).toLocaleString() +
        ' tests completed in ' +
        (t1 - t0).toFixed(2) + ' ms'
    );
};

const hnBigTrieJS = new HNTrieContainer();
const hnBigTrieWASM = new HNTrieContainer();
const hnBigTrieUnserialized = new HNTrieContainer();

Promise.all([
    hnBigTrieJS.readyToUse(),
    hnBigTrieWASM.readyToUse()
]).then(( ) => {
    let t0 = performance.now();
    const theSet = new FilterHostnameDict(hostnamePool);
    let t1 = performance.now();
    stdout('\xA0');
    stdout(
        'Set creation completed in ' +
        (t1 - t0).toFixed(2) + ' ms'
    );

    t0 = performance.now();
    const theTrieJS = hnBigTrieJS.fromIterable(hostnamePool, 'addJS');
    hnBigTrieJS.optimize();
    t1 = performance.now();
    stdout('\xA0');
    stdout(
        'HNTrieContainer creation (JS) completed in ' +
        (t1 - t0).toFixed(2) + ' ms'
    );

    let theTrieWASM;
    if ( hnBigTrieWASM.addWASM instanceof Function ) {
        t0 = performance.now();
        theTrieWASM = hnBigTrieWASM.fromIterable(hostnamePool, 'addWASM');
        hnBigTrieWASM.optimize();
        t1 = performance.now();
        stdout('\xA0');
        stdout(
            'HNTrieContainer creation (WASM) completed in ' +
            (t1 - t0).toFixed(2) + ' ms'
        );

        const bufJS = theTrieJS.container.buf;
        const bufWASM = theTrieWASM.container.buf;
        for ( let i = 0; i < bufJS.length; i++ ) {
            if ( bufJS[i] !== bufWASM[i] ) {
                stdout('theTrieWASM failure at index ' + i);
                break;
            }
        }
    }

    let selfie = hnBigTrieJS.serialize();
    t0 = performance.now();
    hnBigTrieUnserialized.unserialize(selfie);
    const theTrieUnserialized = hnBigTrieUnserialized.createOne(hnBigTrieJS.compileOne(theTrieJS));
    t1 = performance.now();
    stdout('\xA0');
    stdout(
        'HNTrieContainer creation (unserialized) completed in ' +
        (t1 - t0).toFixed(2) + ' ms'
    );
    selfie = undefined;

    document.getElementById('test').addEventListener('click', ( ) => {
        let parent = document.getElementById('stdout');
        while ( parent.childElementCount !== 0 ) {
            parent.removeChild(parent.firstChild);
        }
        testFlavor(
            hostnamePool,
            'Set (JS)',
            theSet.matches.bind(theSet),
            r => r 
        );
        testFlavor(
            hostnamePool,
            'HNTrieContainer (JS)',
            theTrieJS.matchesJS.bind(theTrieJS),
            r => r >= 0
        );
        if ( theTrieWASM !== undefined ) {
            testFlavor(
                hostnamePool,
                'HNTrieContainer (WASM)',
                theTrieWASM.matchesWASM.bind(theTrieWASM),
                r => r >= 0
            );
        }
        testFlavor(
            hostnamePool,
            'HNTrieContainer (unserialized)',
            theTrieUnserialized.matchesJS.bind(theTrieUnserialized),
            r => r >= 0
        );
    });
});

</script>
</body>
</html>