-
Notifications
You must be signed in to change notification settings - Fork 2
/
screeps-profiler.js
285 lines (246 loc) · 7.53 KB
/
screeps-profiler.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
let usedOnStart = 0;
let enabled = false;
let depth = 0;
function setupProfiler() {
depth = 0; // reset depth, this needs to be done each tick.
Game.profiler = {
stream(duration, filter) {
setupMemory('stream', duration || 10, filter);
},
email(duration, filter) {
setupMemory('email', duration || 100, filter);
},
profile(duration, filter) {
setupMemory('profile', duration || 100, filter);
},
background(filter) {
setupMemory('background', false, filter);
},
restart() {
if (Profiler.isProfiling()) {
const filter = Memory.profiler.filter;
let duration = false;
if (!!Memory.profiler.disableTick) {
// Calculate the original duration, profile is enabled on the tick after the first call,
// so add 1.
duration = Memory.profiler.disableTick - Memory.profiler.enabledTick + 1;
}
const type = Memory.profiler.type;
setupMemory(type, duration, filter);
}
},
reset: resetMemory,
output: Profiler.output,
};
overloadCPUCalc();
}
function setupMemory(profileType, duration, filter) {
resetMemory();
const disableTick = Number.isInteger(duration) ? Game.time + duration : false;
if (!Memory.profiler) {
Memory.profiler = {
map: {},
totalTime: 0,
enabledTick: Game.time + 1,
disableTick,
type: profileType,
filter,
};
}
}
function resetMemory() {
Memory.profiler = null;
}
function overloadCPUCalc() {
if (Game.rooms.sim) {
usedOnStart = 0; // This needs to be reset, but only in the sim.
Game.cpu.getUsed = function getUsed() {
return performance.now() - usedOnStart;
};
}
}
function getFilter() {
return Memory.profiler.filter;
}
const functionBlackList = [
'getUsed', // Let's avoid wrapping this... may lead to recursion issues and should be inexpensive.
'constructor', // es6 class constructors need to be called with `new`
];
function wrapFunction(name, originalFunction) {
return function wrappedFunction() {
if (Profiler.isProfiling()) {
const nameMatchesFilter = name === getFilter();
const start = Game.cpu.getUsed();
if (nameMatchesFilter) {
depth++;
}
const result = originalFunction.apply(this, arguments);
if (depth > 0 || !getFilter()) {
const end = Game.cpu.getUsed();
Profiler.record(name, end - start);
}
if (nameMatchesFilter) {
depth--;
}
return result;
}
return originalFunction.apply(this, arguments);
};
}
function hookUpPrototypes() {
Profiler.prototypes.forEach(proto => {
profileObjectFunctions(proto.val, proto.name);
});
}
function profileObjectFunctions(object, label) {
const objectToWrap = object.prototype ? object.prototype : object;
Object.getOwnPropertyNames(objectToWrap).forEach(functionName => {
const extendedLabel = `${label}.${functionName}`;
try {
const isFunction = typeof objectToWrap[functionName] === 'function';
const notBlackListed = functionBlackList.indexOf(functionName) === -1;
if (isFunction && notBlackListed) {
const originalFunction = objectToWrap[functionName];
objectToWrap[functionName] = profileFunction(originalFunction, extendedLabel);
}
} catch (e) { } /* eslint no-empty:0 */
});
return objectToWrap;
}
function profileFunction(fn, functionName) {
const fnName = functionName || fn.name;
if (!fnName) {
console.log('Couldn\'t find a function name for - ', fn);
console.log('Will not profile this function.');
return fn;
}
return wrapFunction(fnName, fn);
}
const Profiler = {
printProfile() {
console.log(Profiler.output());
},
emailProfile() {
Game.notify(Profiler.output());
},
output(numresults) {
const displayresults = !!numresults ? numresults : 20;
if (!Memory.profiler || !Memory.profiler.enabledTick) {
return 'Profiler not active.';
}
const elapsedTicks = Game.time - Memory.profiler.enabledTick + 1;
const header = 'calls\t\ttime\t\tavg\t\tfunction';
const footer = [
`Avg: ${(Memory.profiler.totalTime / elapsedTicks).toFixed(2)}`,
`Total: ${Memory.profiler.totalTime.toFixed(2)}`,
`Ticks: ${elapsedTicks}`,
].join('\t');
return [].concat(header, Profiler.lines().slice(0, displayresults), footer).join('\n');
},
lines() {
const stats = Object.keys(Memory.profiler.map).map(functionName => {
const functionCalls = Memory.profiler.map[functionName];
return {
name: functionName,
calls: functionCalls.calls,
totalTime: functionCalls.time,
averageTime: functionCalls.time / functionCalls.calls,
};
}).sort((val1, val2) => {
return val2.totalTime - val1.totalTime;
});
const lines = stats.map(data => {
return [
data.calls,
data.totalTime.toFixed(1),
data.averageTime.toFixed(3),
data.name,
].join('\t\t');
});
return lines;
},
prototypes: [
{ name: 'Game', val: Game },
{ name: 'Room', val: Room },
{ name: 'Structure', val: Structure },
{ name: 'Spawn', val: Spawn },
{ name: 'Creep', val: Creep },
{ name: 'RoomPosition', val: RoomPosition },
{ name: 'Source', val: Source },
{ name: 'Flag', val: Flag },
],
record(functionName, time) {
if (!Memory.profiler.map[functionName]) {
Memory.profiler.map[functionName] = {
time: 0,
calls: 0,
};
}
Memory.profiler.map[functionName].calls++;
Memory.profiler.map[functionName].time += time;
},
endTick() {
if (Game.time >= Memory.profiler.enabledTick) {
const cpuUsed = Game.cpu.getUsed();
Memory.profiler.totalTime += cpuUsed;
Profiler.report();
}
},
report() {
if (Profiler.shouldPrint()) {
Profiler.printProfile();
} else if (Profiler.shouldEmail()) {
Profiler.emailProfile();
}
},
isProfiling() {
if (!enabled || !Memory.profiler) {
return false;
}
return !Memory.profiler.disableTick || Game.time <= Memory.profiler.disableTick;
},
type() {
return Memory.profiler.type;
},
shouldPrint() {
const streaming = Profiler.type() === 'stream';
const profiling = Profiler.type() === 'profile';
const onEndingTick = Memory.profiler.disableTick === Game.time;
return streaming || (profiling && onEndingTick);
},
shouldEmail() {
return Profiler.type() === 'email' && Memory.profiler.disableTick === Game.time;
},
};
module.exports = {
wrap(callback) {
if (enabled) {
setupProfiler();
}
if (Profiler.isProfiling()) {
usedOnStart = Game.cpu.getUsed();
// Commented lines are part of an on going experiment to keep the profiler
// performant, and measure certain types of overhead.
// var callbackStart = Game.cpu.getUsed();
const returnVal = callback();
// var callbackEnd = Game.cpu.getUsed();
Profiler.endTick();
// var end = Game.cpu.getUsed();
// var profilerTime = (end - start) - (callbackEnd - callbackStart);
// var callbackTime = callbackEnd - callbackStart;
// var unaccounted = end - profilerTime - callbackTime;
// console.log('total-', end, 'profiler-', profilerTime, 'callbacktime-',
// callbackTime, 'start-', start, 'unaccounted', unaccounted);
return returnVal;
}
return callback();
},
enable() {
enabled = true;
hookUpPrototypes();
},
output: Profiler.output,
registerObject: profileObjectFunctions,
registerFN: profileFunction,
registerClass: profileObjectFunctions,
};