Last active
July 24, 2017 02:22
-
-
Save IskenHuang/a85743b831f091b577c64bcb7f2bdd34 to your computer and use it in GitHub Desktop.
Performance monitor in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var __LAST_CALLED_TIME__ = 0; | |
function requestAnimFrame() { | |
if(!__LAST_CALLED_TIME__) { | |
__LAST_CALLED_TIME__ = Date.now() | |
return 0 | |
} | |
var delta = (Date.now() - __LAST_CALLED_TIME__)/1000 | |
__LAST_CALLED_TIME__ = Date.now() | |
console.log('fps = ', 1/delta) | |
return 1/delta | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @example | |
* var perf = new DevicePerf() | |
* | |
* // When you init this Function will auto added a record named 'init'. You can change it on construtor. For example: new DevicePerf('MY_CUSTOM_NAME') | |
* perf.add('MY_FIRST_RECORD') | |
* perf.add('MY_SECUND_RECORD') | |
* | |
* // print record history and total time. | |
* perf.report() | |
* | |
* // clear records | |
* perf.clear() // clear all records and add init | |
* perf.clear(true) // clear all records | |
*/ | |
class DevicePerf { | |
constructor(firstRecordName) { | |
this.history = [] | |
this.firstRecordName = typeof(firstRecordName) === 'string' ? firstRecordName : 'init' | |
} | |
add (name = '') { | |
name = String(name) | |
this.history.push({ | |
name: name, | |
ts: Date.now() | |
}) | |
} | |
clear(isEmpty = null) { | |
this.history = [] | |
if(!isEmpty) this.add(this.firstRecordName) | |
} | |
report(isMute = null) { | |
let len = history.length | |
if(len < 2) return 'Record is less than 2' | |
let result = { histroy: [] } | |
let firstPoint = history[0] | |
let lastPoint = history[len-1] | |
let platform = null | |
try { | |
platform = WXEnvironment.platform | |
}catch(e){} | |
for(let i = 0; i < len-1; i++) { | |
let p1 = history[i] | |
let p2 = history[i+1] | |
let obj = { | |
from: p1.name, | |
to: p2.name, | |
time: p2.ts - p1.ts | |
} | |
this.log(obj, false, isMute) | |
result.histroy.push(obj) | |
} | |
result.total = { | |
from: firstPoint.name, | |
to: lastPoint.name, | |
time: lastPoint.ts - firstPoint.ts | |
} | |
this.log(result.total, true, isMute) | |
return result | |
} | |
log (obj = {}, isTotal = false, isMute = false) { | |
const logger = platform && platform === 'android' ? nativeLog : console.log | |
const prefix = !!isTotal ? 'Total' : ' ' | |
!isMute && logger(prefix, 'from [', obj.from, '] to [', obj.to, ']:', obj.time) | |
} | |
get toString() { | |
return this.report(true) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @example | |
* var perf = new DevicePerf() | |
* | |
* // When you init this Function will auto added a record named 'init'. You can change it on construtor. For example: new DevicePerf('MY_CUSTOM_NAME') | |
* perf.add('MY_FIRST_RECORD') | |
* perf.add('MY_SECUND_RECORD') | |
* | |
* // print record history and total time. | |
* perf.report() | |
* | |
* // clear records | |
* perf.clear() // clear all records and add init | |
* perf.clear(true) // clear all records | |
*/ | |
function DevicePerf(firstRecordName) { | |
var history = [] | |
firstRecordName = typeof(firstRecordName) === 'string' ? firstRecordName : 'init' | |
Object.defineProperty(this, 'add', { | |
value: function(name) { | |
name = String(name) | |
var obj = { | |
name: name, | |
ts: Date.now() | |
} | |
history.push(obj) | |
} | |
}) | |
Object.defineProperty(this, 'clear', { | |
value: function(isEmpty) { | |
history = [] | |
if(!isEmpty) this.add(firstRecordName) | |
} | |
}) | |
Object.defineProperty(this, 'report', { | |
value: function(isMute) { | |
var len = history.length | |
if(len < 2) return 'Record is less than 2' | |
var result = { histroy: [] } | |
var firstPoint = history[0] | |
var lastPoint = history[len-1] | |
var platform = null | |
try { | |
platform = WXEnvironment.platform | |
}catch(e){} | |
var logformat = function(obj, isTotal) { | |
var log = platform && platform === 'android' ? nativeLog : console.log | |
var prefix = !!isTotal ? 'Total' : ' ' | |
!isMute && log(prefix, 'from [', obj.from, '] to [', obj.to, ']:', obj.time) | |
} | |
for(var i = 0; i < len-1; i++) { | |
var p1 = history[i] | |
var p2 = history[i+1] | |
var obj = { | |
from: p1.name, | |
to: p2.name, | |
time: p2.ts - p1.ts | |
} | |
logformat(obj) | |
result.histroy.push(obj) | |
} | |
result.total = { | |
from: firstPoint.name, | |
to: lastPoint.name, | |
time: lastPoint.ts - firstPoint.ts | |
} | |
logformat(result.total, true) | |
return result | |
} | |
}) | |
Object.defineProperty(this, 'toString', { | |
value: function() { | |
return this.report(true) | |
} | |
}) | |
this.add(firstRecordName) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment