-
Notifications
You must be signed in to change notification settings - Fork 40
/
screeps-profiler.js
528 lines (463 loc) · 15.6 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
'use strict';
let usedOnStart = 0;
let enabled = false;
let depth = 0;
let parentFn = '(tick)';
class ProfilerError extends Error {}
// Hack to ensure the InterShardMemory constant exists in sim
try {
// eslint-disable-next-line no-unused-expressions
InterShardMemory;
} catch (e) {
global.InterShardMemory = undefined;
}
function setupProfiler() {
depth = 0; // reset depth, this needs to be done each tick.
parentFn = '(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);
},
callgrind(duration, filter) {
setupMemory('callgrind', duration || 100, 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,
downloadCallgrind: Profiler.downloadCallgrind,
};
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,
};
}
console.log(`Profiling type ${profileType} started at ${Game.time + 1} for ${duration} ticks`);
}
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`
];
const commonProperties = ['length', 'name', 'arguments', 'caller', 'prototype'];
function wrapFunction(name, originalFunction) {
// wrappedFunction.__profiler = Profiler;
if (originalFunction.__profiler) {
// eslint-disable-next-line no-param-reassign
originalFunction.__profiler = Profiler;
return originalFunction;
}
function wrappedFunction() {
const profiler = wrappedFunction.__profiler;
if (profiler.isProfiling()) {
const nameMatchesFilter = name === getFilter();
const start = Game.cpu.getUsed();
if (nameMatchesFilter) {
depth++;
}
const curParent = parentFn;
parentFn = name;
let result;
if (this && this.constructor === wrappedFunction) {
// eslint-disable-next-line new-cap
result = new originalFunction(...arguments);
} else {
result = originalFunction.apply(this, arguments);
}
parentFn = curParent;
if (depth > 0 || !getFilter()) {
const end = Game.cpu.getUsed();
profiler.record(name, end - start, parentFn);
}
if (nameMatchesFilter) {
depth--;
}
return result;
}
if (this && this.constructor === wrappedFunction) {
// eslint-disable-next-line new-cap
return new originalFunction(...arguments);
}
return originalFunction.apply(this, arguments);
}
wrappedFunction.__profiler = Profiler;
wrappedFunction.toString = () =>
`// screeps-profiler wrapped function:\n${originalFunction.toString()}`;
Object.getOwnPropertyNames(originalFunction).forEach(property => {
if (!commonProperties.includes(property)) {
wrappedFunction[property] = originalFunction[property];
}
});
return wrappedFunction;
}
function hookUpPrototypes() {
for (const { name, val } of Profiler.prototypes) {
if (!val) {
console.log(`skipping prototype hook ${name}, object appears to be missing`);
continue;
}
profileObjectFunctions(val, name);
}
}
function profileObjectFunctions(object, label) {
if (!object || !(typeof object === 'object' || typeof object === 'function')) {
throw new ProfilerError(`Asked to profile non-object ${object} for ${label}
(${typeof object})`);
}
if (object.prototype) {
profileObjectFunctions(object.prototype, label);
}
const objectToWrap = object;
Object.getOwnPropertyNames(objectToWrap).forEach(functionName => {
const extendedLabel = `${label}.${functionName}`;
const isBlackListed = functionBlackList.indexOf(functionName) !== -1;
if (isBlackListed) {
return;
}
const descriptor = Object.getOwnPropertyDescriptor(objectToWrap, functionName);
if (!descriptor) {
return;
}
const hasAccessor = descriptor.get || descriptor.set;
if (hasAccessor) {
const configurable = descriptor.configurable;
if (!configurable) {
return;
}
const profileDescriptor = {};
if (descriptor.get) {
const extendedLabelGet = `${extendedLabel}:get`;
profileDescriptor.get = profileFunction(descriptor.get, extendedLabelGet);
}
if (descriptor.set) {
const extendedLabelSet = `${extendedLabel}:set`;
profileDescriptor.set = profileFunction(descriptor.set, extendedLabelSet);
}
Object.defineProperty(objectToWrap, functionName, profileDescriptor);
return;
}
const isFunction = typeof descriptor.value === 'function';
if (!isFunction || !descriptor.writable) {
return;
}
const originalFunction = objectToWrap[functionName];
objectToWrap[functionName] = profileFunction(originalFunction, extendedLabel);
});
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(1000));
},
downloadCallgrind() {
const id = `id${Math.random()}`;
const shardId = Game.shard.name + (Game.shard.ptr ? '-ptr' : '');
const filename = `callgrind.${shardId}.${Game.time}`;
const data = Profiler.callgrind();
if (!data) {
console.log('No profile data to download');
return;
}
/* eslint-disable */
const download = `
<script>
var element = document.getElementById('${id}');
if (!element) {
element = document.createElement('a');
element.setAttribute('id', '${id}');
element.setAttribute('href', 'data:text/plain;charset=utf-8,${encodeURIComponent(data)}');
element.setAttribute('download', '${filename}');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
}
</script>
`;
/* eslint-enable */
console.log(
download
.split('\n')
.map((s) => s.trim())
.join('')
);
},
callgrind() {
if (!Memory.profiler || !Memory.profiler.enabledTick) return null;
const elapsedTicks = Game.time - Memory.profiler.enabledTick + 1;
Profiler.checkMapItem('(tick)');
Memory.profiler.map['(tick)'].calls = elapsedTicks;
Memory.profiler.map['(tick)'].time = Memory.profiler.totalTime;
Profiler.checkMapItem('(root)');
Memory.profiler.map['(root)'].calls = 1;
Memory.profiler.map['(root)'].time = Memory.profiler.totalTime;
Profiler.checkMapItem('(tick)', Memory.profiler.map['(root)'].subs);
Memory.profiler.map['(root)'].subs['(tick)'].calls = elapsedTicks;
Memory.profiler.map['(root)'].subs['(tick)'].time = Memory.profiler.totalTime;
let body = `events: ns\nsummary: ${Math.round(
Memory.profiler.totalTime * 1000000
)}\n`;
for (const fnName of Object.keys(Memory.profiler.map)) {
const fn = Memory.profiler.map[fnName];
let callsBody = '';
let callsTime = 0;
for (const callName of Object.keys(fn.subs)) {
const call = fn.subs[callName];
const ns = Math.round(call.time * 1000000);
callsBody += `cfn=${callName}\ncalls=${call.calls} 1\n1 ${ns}\n`;
callsTime += call.time;
}
body += `\nfn=${fnName}\n1 ${Math.round(
(fn.time - callsTime) * 1000000
)}\n${callsBody}`;
}
return body;
},
output(passedOutputLengthLimit) {
const outputLengthLimit = passedOutputLengthLimit || 1000;
if (!Memory.profiler || !Memory.profiler.enabledTick) {
return 'Profiler not active.';
}
const endTick = Math.min(Memory.profiler.disableTick || Game.time, Game.time);
const startTick = Memory.profiler.enabledTick;
const elapsedTicks = endTick - startTick + 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');
const lines = [header];
let currentLength = header.length + 1 + footer.length;
const allLines = Profiler.lines();
let done = false;
while (!done && allLines.length) {
const line = allLines.shift();
// each line added adds the line length plus a new line character.
if (currentLength + line.length + 1 < outputLengthLimit) {
lines.push(line);
currentLength += line.length + 1;
} else {
done = true;
}
}
lines.push(footer);
return lines.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: 'ConstructionSite', val: ConstructionSite },
{ name: 'Creep', val: Creep },
{ name: 'Deposit', val: Deposit },
{ name: 'Flag', val: Flag },
{ name: 'Game', val: Game },
{ name: 'InterShardMemory', val: InterShardMemory },
{ name: 'Mineral', val: Mineral },
{ name: 'Nuke', val: Nuke },
{ name: 'OwnedStructure', val: OwnedStructure },
{ name: 'PathFinder', val: PathFinder },
{ name: 'PowerCreep', val: PowerCreep },
{ name: 'RawMemory', val: RawMemory },
{ name: 'Resource', val: Resource },
{ name: 'Room', val: Room },
{ name: 'RoomObject', val: RoomObject },
{ name: 'RoomPosition', val: RoomPosition },
{ name: 'RoomVisual', val: RoomVisual },
{ name: 'Ruin', val: Ruin },
{ name: 'Source', val: Source },
{ name: 'Store', val: Store },
{ name: 'Structure', val: Structure },
{ name: 'StructureContainer', val: StructureContainer },
{ name: 'StructureController', val: StructureController },
{ name: 'StructureExtension', val: StructureExtension },
{ name: 'StructureExtractor', val: StructureExtractor },
{ name: 'StructureFactory', val: StructureFactory },
{ name: 'StructureInvaderCore', val: StructureInvaderCore },
{ name: 'StructureKeeperLair', val: StructureKeeperLair },
{ name: 'StructureLab', val: StructureLab },
{ name: 'StructureLink', val: StructureLink },
{ name: 'StructureNuker', val: StructureNuker },
{ name: 'StructureObserver', val: StructureObserver },
{ name: 'StructurePortal', val: StructurePortal },
{ name: 'StructurePowerBank', val: StructurePowerBank },
{ name: 'StructurePowerSpawn', val: StructurePowerSpawn },
{ name: 'StructureRampart', val: StructureRampart },
{ name: 'StructureRoad', val: StructureRoad },
{ name: 'StructureSpawn', val: StructureSpawn },
{ name: 'StructureStorage', val: StructureStorage },
{ name: 'StructureTerminal', val: StructureTerminal },
{ name: 'StructureTower', val: StructureTower },
{ name: 'StructureWall', val: StructureWall },
{ name: 'Tombstone', val: Tombstone },
],
checkMapItem(functionName, map = Memory.profiler.map) {
if (!map[functionName]) {
// eslint-disable-next-line no-param-reassign
map[functionName] = {
time: 0,
calls: 0,
subs: {},
};
}
},
record(functionName, time, parent) {
this.checkMapItem(functionName);
Memory.profiler.map[functionName].calls++;
Memory.profiler.map[functionName].time += time;
if (parent) {
this.checkMapItem(parent);
this.checkMapItem(functionName, Memory.profiler.map[parent].subs);
Memory.profiler.map[parent].subs[functionName].calls++;
Memory.profiler.map[parent].subs[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();
} else if (Profiler.shouldCallgrind()) {
Profiler.downloadCallgrind();
}
},
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;
},
shouldCallgrind() {
return (
Profiler.type() === 'callgrind' &&
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,
callgrind: Profiler.callgrind,
registerObject: profileObjectFunctions,
registerFN: profileFunction,
registerClass: profileObjectFunctions,
Error: ProfilerError,
};