【问题标题】:How to capture time consumed in a user action in Javascript?如何在 Javascript 中捕获用户操作所消耗的时间?
【发布时间】:2013-02-01 13:33:20
【问题描述】:

在使用 ExtJs 创建的 RIA 应用程序中,我们需要捕获用户操作所消耗的时间。

例如,用户点击打开一个窗口的按钮“打开”,则需要捕获打开窗口所消耗的时间。

我们尝试过使用 Selenium IDE,但它只记录操作并执行它们,而不记录命令中消耗的时间。

我们也尝试过使用浏览器的“Profiler”功能(在操作之前开始分析,在操作结束后停止分析),但那是手动的。

因此,有人可以指导 - 如何以自动方式捕获用户在 Javascript 中的操作所花费的时间?

提前感谢您的帮助。

【问题讨论】:

标签: javascript performance-testing


【解决方案1】:

这是一个全局事件跟踪功能的粗略草图:

(function() {

var pairs = {};

eventLogger = function(e) {
    var ts = new Date().getTime();

    if (pairs[e]) {
        var taken = ts - pairs[e],
        img = new Image();
        img.onload = function() { };
        img.src = '/path/to/logger?evt=' + encodeURIComponent(e) + '&t=' + encodeURIComponent(taken);

        delete pairs[e];
    } else {
        pairs[e] = ts;
    }
}

}());

匹配成对的事件名称;第一个启动计时器,第二个发出服务器请求以记录该事件所用的时间。

然后,您将记录如下事件:

eventLogger('loadContacts');
// start code that loads contacts
// ...
// later
eventLogger('loadContacts'); // done loading

它不区分开始和停止事件,所以如果你可能有重叠的开始和停止时间,你可能需要做一些调整。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-15
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多