File size: 4,779 Bytes
32c56b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

function createRow(table, cellName, items) {
    var tr = document.createElement('tr');
    var res = [];

    items.forEach(function(x, i) {
        if (x === undefined) {
            res.push(null);
            return;
        }

        var td = document.createElement(cellName);
        td.textContent = x;
        tr.appendChild(td);
        res.push(td);

        var colspan = 1;
        for (var n = i + 1; n < items.length; n++) {
            if (items[n] !== undefined) {
                break;
            }

            colspan += 1;
        }

        if (colspan > 1) {
            td.colSpan = colspan;
        }
    });

    table.appendChild(tr);

    return res;
}

function showProfile(path, cutoff = 0.05) {
    requestGet(path, {}, function(data) {
        var table = document.createElement('table');
        table.className = 'popup-table';

        data.records['total'] = data.total;
        var keys = Object.keys(data.records).sort(function(a, b) {
            return data.records[b] - data.records[a];
        });
        var items = keys.map(function(x) {
            return {key: x, parts: x.split('/'), time: data.records[x]};
        });
        var maxLength = items.reduce(function(a, b) {
            return Math.max(a, b.parts.length);
        }, 0);

        var cols = createRow(table, 'th', ['record', 'seconds']);
        cols[0].colSpan = maxLength;

        function arraysEqual(a, b) {
            return !(a < b || b < a);
        }

        var addLevel = function(level, parent, hide) {
            var matching = items.filter(function(x) {
                return x.parts[level] && !x.parts[level + 1] && arraysEqual(x.parts.slice(0, level), parent);
            });
            var sorted = matching.sort(function(a, b) {
                return b.time - a.time;
            });
            var othersTime = 0;
            var othersList = [];
            var othersRows = [];
            var childrenRows = [];
            sorted.forEach(function(x) {
                var visible = x.time >= cutoff && !hide;

                var cells = [];
                for (var i = 0; i < maxLength; i++) {
                    cells.push(x.parts[i]);
                }
                cells.push(x.time.toFixed(3));
                var cols = createRow(table, 'td', cells);
                for (i = 0; i < level; i++) {
                    cols[i].className = 'muted';
                }

                var tr = cols[0].parentNode;
                if (!visible) {
                    tr.classList.add("hidden");
                }

                if (x.time >= cutoff) {
                    childrenRows.push(tr);
                } else {
                    othersTime += x.time;
                    othersList.push(x.parts[level]);
                    othersRows.push(tr);
                }

                var children = addLevel(level + 1, parent.concat([x.parts[level]]), true);
                if (children.length > 0) {
                    var cell = cols[level];
                    var onclick = function() {
                        cell.classList.remove("link");
                        cell.removeEventListener("click", onclick);
                        children.forEach(function(x) {
                            x.classList.remove("hidden");
                        });
                    };
                    cell.classList.add("link");
                    cell.addEventListener("click", onclick);
                }
            });

            if (othersTime > 0) {
                var cells = [];
                for (var i = 0; i < maxLength; i++) {
                    cells.push(parent[i]);
                }
                cells.push(othersTime.toFixed(3));
                cells[level] = 'others';
                var cols = createRow(table, 'td', cells);
                for (i = 0; i < level; i++) {
                    cols[i].className = 'muted';
                }

                var cell = cols[level];
                var tr = cell.parentNode;
                var onclick = function() {
                    tr.classList.add("hidden");
                    cell.classList.remove("link");
                    cell.removeEventListener("click", onclick);
                    othersRows.forEach(function(x) {
                        x.classList.remove("hidden");
                    });
                };

                cell.title = othersList.join(", ");
                cell.classList.add("link");
                cell.addEventListener("click", onclick);

                if (hide) {
                    tr.classList.add("hidden");
                }

                childrenRows.push(tr);
            }

            return childrenRows;
        };

        addLevel(0, []);

        popup(table);
    });
}