Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 自动测量功能时间_Javascript_Jquery_Performance - Fatal编程技术网

Javascript 自动测量功能时间

Javascript 自动测量功能时间,javascript,jquery,performance,Javascript,Jquery,Performance,我有500多个运行客户端的JavaScript函数。我想测量客户端上每个函数的执行时间和相对频率,并将统计数据发送到服务器,以便找出哪些函数需要首先优化 所有函数都是全局对象的一部分(如果有帮助的话) 如何进行自动测量?我需要一个全局函数来监视和测量所有其他函数。这可能吗?像这样的东西应该可以解决问题(还没有测试过) 用法 这应该起作用: 函数testFunc(测试){ 对于(var i=0;i基于@Yury Tarabanko(这对我不起作用,但给了我灵感),以下是我得到的: //将在其中存储

我有500多个运行客户端的JavaScript函数。我想测量客户端上每个函数的执行时间和相对频率,并将统计数据发送到服务器,以便找出哪些函数需要首先优化

所有函数都是全局对象的一部分(如果有帮助的话)


如何进行自动测量?我需要一个全局函数来监视和测量所有其他函数。这可能吗?

像这样的东西应该可以解决问题(还没有测试过)

用法

这应该起作用:

函数testFunc(测试){
对于(var i=0;i基于@Yury Tarabanko(这对我不起作用,但给了我灵感),以下是我得到的:

//将在其中存储性能重用的全局对象

perf = {};
然后,您需要一个封装所有其他函数并跟踪性能的函数

//Function that actually tracks the perfomance, wrapping all other functions
function trackPerfomance() {
    var name, fn;
    for (name in jNTRender) { //jNTRender - is the namespace that I was analysing. Use yours or window
        fn = jNTRender[name];
        if (typeof fn === 'function') {
            jNTRender[name] = (function(name, fn) {
                var args = arguments;
                return function() {
                    if (!perf[name]) perf[name] = {
                        timesCalled: 0, 
                        timeTaken: 0,
                        averageTime: 0
                    }

                    var start = performance.now(),
                    out = fn.apply(this, arguments),
                    end = performance.now();
                    perf[name].timesCalled ++; //how many times function was called
                    perf[name].timeTaken += (end - start);  //time taken for execution
                    perf[name].averageTime = perf[name].timeTaken/perf[name].timesCalled; //average execution time
                    return out;


                }
            })(name, fn);
        }
    }
}
你需要分析结果

//Function that analyzes results - jQuery used for simplicity
function analyzePerfomance(){

    functionsAverageTime = [];

    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        functionsAverageTime.push([functionName, functionPerfomance.averageTime]);
    });

    functionsAverageTime.sort(function(a, b) { return b[1]-a[1] });

    console.log('Slowest in average functions, msec');
    $.each(functionsAverageTime, function(index, value){
        console.log(index+1, value[0], value[1]);
    });

    functionsTimesCalled = [];
    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        functionsTimesCalled.push([functionName, functionPerfomance.timesCalled]);
    });

    console.log('Most used functions, times');
    $.each(functionsTimesCalled, function(index, value){
        console.log(index+1, value[0], value[1]);
    });

    functionsTotalTimeSpent = [];
    totalTime = 0;
    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        totalTime += functionPerfomance.timeTaken;
    });


    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        functionsTotalTimeSpent.push([functionName, functionPerfomance.timeTaken, 100*functionPerfomance.timeTaken/totalTime]);
    });

    functionsTotalTimeSpent.sort(function(a, b) { return b[1]-a[1] });

    console.log('Time taken by functions, msec, % of total time taken');
    $.each(functionsTotalTimeSpent, function(index, value){
        console.log(index+1, value[0], Math.round(value[1]), value[2].toFixed(2) + '%');
    });

}
从控制台运行跟踪器

trackPerfomance();
等待一些时间-分钟,小时。。。 并分析性能:

analyzePerfomance();
这是我在控制台中得到的。非常有用,易于阅读。当然,可以通过ajax将perf对象发送到服务器


迭代您的命名空间对象,并用收集调用统计信息的函数包装每个函数。在
窗口上将数据发送回服务器。卸载
。将这两个建议结合起来,应该会起作用:)@YuryTarabanko,这是我的想法,但我不知道如何“包装每个函数”-请您解释一下好吗?可能会有帮助。请注意,我会为您的日志设置一个缓冲区,并每x次调用提交/刷新一次,以窗口结束。卸载以在会话结束时提交/刷新其余部分。同时请注意,您可能无法访问private(已关闭)函数。您只需确保在记录所有要执行的函数之后,但在实际调用它们之前初始化
getPerfResults()
。这项任务应该不会太难完成。发送数据的逻辑和数据的发送方式由您决定。顺便说一句
getPerfResults()
检索每个全局函数相对于
性能的平均执行时间。now()
//Function that analyzes results - jQuery used for simplicity
function analyzePerfomance(){

    functionsAverageTime = [];

    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        functionsAverageTime.push([functionName, functionPerfomance.averageTime]);
    });

    functionsAverageTime.sort(function(a, b) { return b[1]-a[1] });

    console.log('Slowest in average functions, msec');
    $.each(functionsAverageTime, function(index, value){
        console.log(index+1, value[0], value[1]);
    });

    functionsTimesCalled = [];
    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        functionsTimesCalled.push([functionName, functionPerfomance.timesCalled]);
    });

    console.log('Most used functions, times');
    $.each(functionsTimesCalled, function(index, value){
        console.log(index+1, value[0], value[1]);
    });

    functionsTotalTimeSpent = [];
    totalTime = 0;
    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        totalTime += functionPerfomance.timeTaken;
    });


    $.each(jNTPerfomance, function(functionName, functionPerfomance){
        functionsTotalTimeSpent.push([functionName, functionPerfomance.timeTaken, 100*functionPerfomance.timeTaken/totalTime]);
    });

    functionsTotalTimeSpent.sort(function(a, b) { return b[1]-a[1] });

    console.log('Time taken by functions, msec, % of total time taken');
    $.each(functionsTotalTimeSpent, function(index, value){
        console.log(index+1, value[0], Math.round(value[1]), value[2].toFixed(2) + '%');
    });

}
trackPerfomance();
analyzePerfomance();