-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemSnap.coffee
56 lines (51 loc) · 1.83 KB
/
memSnap.coffee
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
###*
@file MemSnap is a 0.2KB analytics micro-library written in both CoffeeScript and JavaScript that logs browser memory statistic snapshots over time.
@author Daniel Lamb <[email protected]>
###
###*
Logs browser memory statistic snapshots over time.
@param {Window} global - the global context (aka the Window object).
@param {string} url - the url including parameters for where to send the memory snapshots.
@param {Object.<string, number>} intervals - a key value pair list of when to send the memory snapshots.
@param {Function} notify - an optional callback when a memory snapshot is taken.
###
memSnap = (global, url, intervals, notify) ->
perf = global.performance
# check browser support
if perf and perf.memory
lblRegx = /\{lbl\}/
lmtRegx = /\{lmt\}/
totRegx = /\{tot\}/
useRegx = /\{use\}/
# use default intervals if not set
intervals = intervals or
# label: delay
'0s': 0
'5m': 3e5
'10m': 6e5
'15m': 9e5
'30m': 18e5
'1h': 36e5
'3h': 108e5
'6h': 216e5
'12h': 432e5
'24h': 864e5
'48h': 1728e5
###*
Schedules sending a memory snapshot in the future.
@param {string} label - human readable label of when the snapshot was taken
@param {number} delay - how long to wait in milliseconds from the time memSnap is called.
###
sendAt = (label, delay) ->
global.setTimeout (->
if notify
notify label, perf.memory
global.document.createElement('img').src =
url.replace(lblRegx, global.encodeURIComponent label)
.replace(lmtRegx, perf.memory.jsHeapSizeLimit)
.replace(totRegx, perf.memory.totalJSHeapSize)
.replace(useRegx, perf.memory.usedJSHeapSize)
), delay
# schedule sending memory stats
for own key, value of intervals
sendAt key, value