Perf test for once() on 1k records
Test perf of individual lookups.
by Kato Richardson
HTML
<script src="https://www.gstatic.com/firebasejs/3.4.1/firebase.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script>
<div>Number of individual lookups:
<span data-id="num">--</span>
<button disabled="disabled">start</button>
</div>
<div>Time elapsed (millis): <span data-id="time">--</span></div>
<div>Avg per record (millis): <span data-id="avg">--</span></div>
<p> </p>
<div data-id="log"></div>
CSS
[data-id=log] {
font-size: 80%;
color: gray;
}
button, .running {
color: #00aa00;
}
button[disabled], .error {
color: red;
}
JavaScript
$(() => {
const URL = "https://kato-bigdata.firebaseio.com";
// Initialize Firebase
const config = {
apiKey: "AIzaSyCNjdzYB6_5pR4UFH878ggyeKnEmHp8On8",
authDomain: "kato-bigdata.firebaseapp.com",
databaseURL: URL
};
firebase.initializeApp(config);
const ref = firebase.database().ref().child('bigdata/node1');
const $els = { log: $('[data-id=log]'), time: $('[data-id=time]'), num: $('[data-id=num]'), span: $('span'), avg: $('[data-id=avg]') };
const NUMKEYS = 1000;
$('button').click(init);
auth().then(enableButton);
function init() {
$els.span.addClass('running').removeClass('error');
fakeGetRecords()
.then(printStats)
.then(getRecords)
.then(printStats)
.catch(e => {
console.error(e);
log(e);
$els.span.addClass('error');
})
.then((stats) => {
$els.span.removeClass('running');
log('Done.');
});
}
function auth() {
log('Authenticating');
return firebase.auth()
.signInWithEmailAndPassword('kato@kato.com', 'katokato')
.then(user => {
log('Authenticated as ' + user.uid);
})
.catch(e => {log(e); console.error(e); return e;});
}
function progress() {
count++;
}
function getKeys() {
return $.getJSON(URL+'/node1/.json?shallow=true');
}
function fakeGetRecords() {
log(`Testing time to just iterate ${NUMKEYS} without actually involving Firebase`);
let stats = new StatsCounter(() => {});
stats.start(NUMKEYS);
let promises = [];
for(let i=1; i <= NUMKEYS; i++) {
promises.push(Promise.resolve(stats.inc()))
}
return Promise.all(promises).then(() => stats);
}
function getRecords() {
log(`Okay, fetching ${NUMKEYS} keys individually.`);
let stats = new StatsCounter(updateDom);
stats.start(NUMKEYS);
let promises = [];
for(let i=1; i <= NUMKEYS; i++) {
promises.push(getKey('rec' + i, stats))
}
return...