This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathyardstick.js
185 lines (150 loc) · 4.51 KB
/
yardstick.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Measure elapsed durations from specific beginning points.
import fs from 'fs-extra';
import path from 'path';
// The maximum number of marks within a single DurationSet. A DurationSet will be automatically finished if this many
// marks are recorded.
const MAXIMUM_MARKS = 100;
// Flush all non-active DurationSets to disk each time that this many marks have been accumulated.
const PERSIST_INTERVAL = 1000;
// A sequence of durations measured from a fixed beginning point.
class DurationSet {
constructor(name) {
this.name = name;
this.startTimestamp = performance.now();
this.marks = [];
this.markCount = 0;
if (atom.config.get('github.performanceToConsole')) {
// eslint-disable-next-line no-console
console.log('%cbegin %c%s:begin',
'font-weight: bold',
'font-weight: normal; font-style: italic; color: blue', this.name);
}
if (atom.config.get('github.performanceToProfile')) {
// eslint-disable-next-line no-console
console.profile(this.name);
}
}
mark(eventName) {
const duration = performance.now() - this.startTimestamp;
if (atom.config.get('github.performanceToConsole')) {
// eslint-disable-next-line no-console
console.log('%cmark %c%s:%s %c%dms',
'font-weight: bold',
'font-weight: normal; font-style: italic; color: blue', this.name, eventName,
'font-style: normal; color: black', duration);
}
if (atom.config.get('github.performanceToDirectory') !== '') {
this.marks.push({eventName, duration});
}
this.markCount++;
if (this.markCount >= MAXIMUM_MARKS) {
this.finish();
}
}
finish() {
this.mark('finish');
if (atom.config.get('github.performanceToProfile')) {
// eslint-disable-next-line no-console
console.profileEnd(this.name);
}
}
serialize() {
return {
name: this.name,
markers: this.marks,
};
}
getCount() {
return this.marks.length;
}
}
let durationSets = [];
let totalMarkCount = 0;
const activeSets = new Map();
function shouldCapture(seriesName, eventName) {
const anyActive = ['Console', 'Directory', 'Profile'].some(kind => {
const value = atom.config.get(`github.performanceTo${kind}`);
return value !== '' && value !== false;
});
if (!anyActive) {
return false;
}
const mask = new RegExp(atom.config.get('github.performanceMask'));
if (!mask.test(`${seriesName}:${eventName}`)) {
return false;
}
return true;
}
const yardstick = {
async save() {
const destDir = atom.config.get('github.performanceToDirectory');
if (destDir === '' || destDir === undefined || destDir === null) {
return;
}
const fileName = path.join(destDir, `performance-${Date.now()}.json`);
await new Promise((resolve, reject) => {
fs.ensureDir(destDir, err => (err ? reject(err) : resolve()));
});
const payload = JSON.stringify(durationSets.map(set => set.serialize()));
await fs.writeFile(fileName, payload, {encoding: 'utf8'});
if (atom.config.get('github.performanceToConsole')) {
// eslint-disable-next-line no-console
console.log('%csaved %c%d series to %s',
'font-weight: bold',
'font-weight: normal; color: black', durationSets.length, fileName);
}
durationSets = [];
},
begin(seriesName) {
if (!shouldCapture(seriesName, 'begin')) {
return;
}
const ds = new DurationSet(seriesName);
activeSets.set(seriesName, ds);
},
mark(seriesName, eventName) {
if (seriesName instanceof Array) {
for (let i = 0; i < seriesName.length; i++) {
this.mark(seriesName[i], eventName);
}
return;
}
if (!shouldCapture(seriesName, eventName)) {
return;
}
const ds = activeSets.get(seriesName);
if (ds === undefined) {
return;
}
ds.mark(eventName);
},
finish(seriesName) {
if (seriesName instanceof Array) {
for (let i = 0; i < seriesName.length; i++) {
this.finish(seriesName[i]);
}
return;
}
if (!shouldCapture(seriesName, 'finish')) {
return;
}
const ds = activeSets.get(seriesName);
if (ds === undefined) {
return;
}
ds.finish();
durationSets.push(ds);
activeSets.delete(seriesName);
totalMarkCount += ds.getCount();
if (totalMarkCount >= PERSIST_INTERVAL) {
totalMarkCount = 0;
this.save();
}
},
async flush() {
durationSets.push(...activeSets.values());
activeSets.clear();
await this.save();
},
};
export default yardstick;