2020-02-06 03:21:18 +01:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
2012-09-19 22:25:13 +02:00
|
|
|
/* WARNING:
|
|
|
|
|
|
|
|
This file is only included when Django's DEBUG = True and your
|
|
|
|
host is in INTERNAL_IPS.
|
|
|
|
|
2012-10-30 02:29:32 +01:00
|
|
|
Do not commit any code elsewhere which uses these functions.
|
2012-09-19 22:25:13 +02:00
|
|
|
They are for debugging use only.
|
|
|
|
|
|
|
|
The file may still be accessible under other circumstances, so do
|
|
|
|
not put sensitive information here. */
|
|
|
|
|
|
|
|
/*
|
2017-05-25 01:44:58 +02:00
|
|
|
debug.print_elapsed_time("foo", foo)
|
2012-09-19 22:25:13 +02:00
|
|
|
|
|
|
|
evaluates to foo() and prints the elapsed time
|
|
|
|
to the console along with the name "foo". */
|
|
|
|
|
2017-05-25 01:44:58 +02:00
|
|
|
export function print_elapsed_time(name, fun) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const t0 = new Date().getTime();
|
|
|
|
const out = fun();
|
|
|
|
const t1 = new Date().getTime();
|
2020-07-15 01:29:15 +02:00
|
|
|
console.log(name + ": " + (t1 - t0) + " ms");
|
2012-09-19 22:25:13 +02:00
|
|
|
return out;
|
|
|
|
}
|
2013-05-07 20:10:33 +02:00
|
|
|
|
2017-05-25 01:44:58 +02:00
|
|
|
export function check_duplicate_ids() {
|
2020-02-12 03:18:00 +01:00
|
|
|
const ids = new Set();
|
2019-11-02 00:06:25 +01:00
|
|
|
const collisions = [];
|
|
|
|
let total_collisions = 0;
|
2016-12-29 20:48:28 +01:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
Array.prototype.slice.call(document.querySelectorAll("*")).forEach((o) => {
|
2020-02-12 03:18:00 +01:00
|
|
|
if (o.id && ids.has(o.id)) {
|
2020-07-02 01:45:54 +02:00
|
|
|
const el = collisions.find((c) => c.id === o.id);
|
2016-12-29 20:48:28 +01:00
|
|
|
|
2020-02-12 03:18:00 +01:00
|
|
|
ids.add(o.id);
|
2016-12-29 20:48:28 +01:00
|
|
|
total_collisions += 1;
|
|
|
|
|
|
|
|
if (!el) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const tag = o.tagName.toLowerCase();
|
2016-12-29 20:48:28 +01:00
|
|
|
collisions.push({
|
|
|
|
id: o.id,
|
|
|
|
count: 1,
|
2020-07-15 00:34:28 +02:00
|
|
|
node:
|
|
|
|
"<" +
|
|
|
|
tag +
|
|
|
|
" className='" +
|
|
|
|
o.className +
|
|
|
|
"' id='" +
|
|
|
|
o.id +
|
|
|
|
"'>" +
|
|
|
|
"</" +
|
|
|
|
tag +
|
|
|
|
">",
|
2016-12-29 20:48:28 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
el.count += 1;
|
|
|
|
}
|
|
|
|
} else if (o.id) {
|
2020-02-12 03:18:00 +01:00
|
|
|
ids.add(o.id);
|
2016-12-29 20:48:28 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
collisions,
|
|
|
|
total_collisions,
|
2016-12-29 20:48:28 +01:00
|
|
|
};
|
|
|
|
}
|
2013-05-07 20:10:33 +02:00
|
|
|
|
|
|
|
/* An IterationProfiler is used for profiling parts of looping
|
2013-07-30 00:35:44 +02:00
|
|
|
* constructs (like a for loop or _.each). You mark sections of the
|
2013-05-07 20:10:33 +02:00
|
|
|
* iteration body and the IterationProfiler will sum the costs of those
|
|
|
|
* sections over all iterations.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2020-03-10 01:49:33 +01:00
|
|
|
* let ip = new debug.IterationProfiler();
|
2013-07-30 00:35:44 +02:00
|
|
|
* _.each(myarray, function (elem) {
|
2013-05-07 20:10:33 +02:00
|
|
|
* ip.iteration_start();
|
|
|
|
*
|
|
|
|
* cheap_op(elem);
|
|
|
|
* ip.section("a");
|
|
|
|
* expensive_op(elem);
|
|
|
|
* ip.section("b");
|
|
|
|
* another_expensive_op(elem);
|
|
|
|
*
|
|
|
|
* ip.iteration_stop();
|
|
|
|
* });
|
|
|
|
* ip.done();
|
|
|
|
*
|
|
|
|
* The console output will look something like:
|
|
|
|
* _iteration_overhead 0.8950002520577982
|
|
|
|
* _rest_of_iteration 153.415000159293413
|
|
|
|
* a 2.361999897402711
|
|
|
|
* b 132.625999901327305
|
|
|
|
*
|
|
|
|
* The _rest_of_iteration section is the region of the iteration body
|
|
|
|
* after section b.
|
|
|
|
*/
|
2020-07-23 00:13:13 +02:00
|
|
|
export class IterationProfiler {
|
|
|
|
sections = new Map();
|
|
|
|
last_time = window.performance.now();
|
2013-05-07 20:10:33 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
iteration_start() {
|
2020-07-15 01:29:15 +02:00
|
|
|
this.section("_iteration_overhead");
|
2020-07-23 00:13:13 +02:00
|
|
|
}
|
2013-05-07 20:10:33 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
iteration_stop() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const now = window.performance.now();
|
|
|
|
const diff = now - this.last_time;
|
2013-05-07 20:10:33 +02:00
|
|
|
if (diff > 1) {
|
2020-02-12 05:58:27 +01:00
|
|
|
this.sections.set(
|
|
|
|
"_rest_of_iteration",
|
2020-07-02 02:16:03 +02:00
|
|
|
(this.sections.get("_rest_of_iteration") || 0) + diff,
|
2020-02-12 05:58:27 +01:00
|
|
|
);
|
2013-05-07 20:10:33 +02:00
|
|
|
}
|
|
|
|
this.last_time = now;
|
2020-07-23 00:13:13 +02:00
|
|
|
}
|
2013-05-07 20:10:33 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
section(label) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const now = window.performance.now();
|
2020-02-12 05:58:27 +01:00
|
|
|
this.sections.set(label, (this.sections.get(label) || 0) + (now - this.last_time));
|
2013-05-07 20:10:33 +02:00
|
|
|
this.last_time = now;
|
2020-07-23 00:13:13 +02:00
|
|
|
}
|
2013-05-07 20:10:33 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
done() {
|
2020-07-15 01:29:15 +02:00
|
|
|
this.section("_iteration_overhead");
|
2013-05-07 20:10:33 +02:00
|
|
|
|
2020-02-12 05:58:27 +01:00
|
|
|
for (const [prop, cost] of this.sections) {
|
|
|
|
console.log(prop, cost);
|
2013-05-07 20:10:33 +02:00
|
|
|
}
|
2020-07-23 00:13:13 +02:00
|
|
|
}
|
|
|
|
}
|