2018-11-03 05:58:46 -06:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
</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>
|
2018-11-04 13:45:59 -07:00
|
|
|
<script src="https://raw.githack.com/gorhill/uBlock/master/src/js/hntrie.js"></script>
|
2018-11-04 17:12:21 -07:00
|
|
|
<!-- script src="../../src/js/hntrie.js"></script> -->
|
2018-11-04 12:31:15 -07:00
|
|
|
<script src="hostname-pool.js"></script>
|
2018-11-03 05:58:46 -06:00
|
|
|
<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(( ) => {
|
2018-11-04 12:31:15 -07:00
|
|
|
const oldTrie = HNTrieBuilder.fromIterable(hostnamePool);
|
|
|
|
const theTrie = hnTrieManager.fromIterable(hostnamePool);
|
2018-11-03 05:58:46 -06:00
|
|
|
document.getElementById('test').addEventListener('click', ( ) => {
|
|
|
|
let parent = document.getElementById('stdout');
|
|
|
|
while ( parent.childElementCount !== 0 ) {
|
|
|
|
parent.removeChild(parent.firstChild);
|
|
|
|
}
|
2018-11-04 12:31:15 -07:00
|
|
|
testFlavor(hostnamePool, 'Old Trie (JS)', oldTrie.matches.bind(oldTrie), true, false);
|
|
|
|
testFlavor(hostnamePool, 'New Trie (JS)', theTrie.matchesJS.bind(theTrie), 1, 0);
|
2018-11-03 05:58:46 -06:00
|
|
|
if ( hnTrieManager.matchesWASM instanceof Function ) {
|
2018-11-04 12:31:15 -07:00
|
|
|
testFlavor(hostnamePool, 'New Trie (WASM)', theTrie.matchesWASM.bind(theTrie), 1, 0);
|
2018-11-03 05:58:46 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|