1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-24 03:12:46 +01:00
uBlock/docs/tests/hntrie-test.html
2018-11-06 13:38:37 -02:00

109 lines
3.4 KiB
HTML

<!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>HNTrie test</h1>
<div><button id="test" type="button">Test!</button></div>
<div id="stdout"></div>
<script src="https://rawcdn.githack.com/gorhill/uBlock/e83ffde5af29bd44ae529c5a60e2506970e7af34/src/js/hntrie.js"></script>
<script src="https://raw.githack.com/gorhill/uBlock/master/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';
}
}
/******************************************************************************/
const testFlavor = function(hostnames, name, matchesFn, hit, miss) {
stdout('\xA0');
stdout('Testing ' + name + '...');
const t0 = performance.now();
for ( let i = 0; i < hostnames.length; i++ ) {
// Exact hits
let needle = hostnames[i];
if ( matchesFn(needle) !== hit ) {
stdout('Exact hits failed: ' + needle);
}
// Subdomain hits
needle = createRandomLabel() + '.' + hostnames[i];
if ( matchesFn(needle) !== hit ) {
stdout('Subdomain hits failed: ' + needle);
}
// Misses batch 1
needle = createRandomLabel() + '.com';
if ( matchesFn(needle) !== miss ) {
stdout('Misses batch 1: ' + needle);
}
// Misses batch 2
needle = hostnames[i] + '.' + createRandomLabel();
if ( matchesFn(needle) !== miss ) {
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 ( matchesFn(needle) !== miss ) {
stdout('Misses batch 3: ' + needle);
}
}
}
const t1 = performance.now();
stdout(
name + ': ' +
(hostnames.length * 5).toLocaleString() +
' tests completed in ' +
(t1 - t0).toFixed(2) + ' ms'
);
};
hnTrieManager.readyToUse().then(( ) => {
const oldTrie = HNTrieBuilder.fromIterable(hostnamePool);
const theTrie = hnTrieManager.fromIterable(hostnamePool);
document.getElementById('test').addEventListener('click', ( ) => {
let parent = document.getElementById('stdout');
while ( parent.childElementCount !== 0 ) {
parent.removeChild(parent.firstChild);
}
testFlavor(hostnamePool, 'Old Trie (JS)', oldTrie.matches.bind(oldTrie), true, false);
testFlavor(hostnamePool, 'New Trie (JS)', theTrie.matchesJS.bind(theTrie), 1, 0);
if ( hnTrieManager.matchesWASM instanceof Function ) {
testFlavor(hostnamePool, 'New Trie (WASM)', theTrie.matchesWASM.bind(theTrie), 1, 0);
}
});
});
</script>
</body>
</html>