In redux-observable, how can I measure the epics duration time when running complete?

送分小仙女□ 提交于 2019-12-11 05:35:33

问题


In the majority situation, epics listen an action and emit an action, many async task in it. I want to measure the duration of epics between the action in and action out. How can I do this?


回答1:


You can measure the time between tasks using Performance.now():

var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");

In the context of your actions and epics, you could include the timestamp in the initial action and then later call Performance.now(), subtracting the first measurement from the second. That's your delta between.

As an example, this measures from before an ajax request to after the ajax response comes back and the new action is created:

action$.pipe(
  ofType("PING"),
  switchMap(action => {
    const before = Performance.now();

    return ajax.getJSON("/ping").pipe(
      map(pong => {
        const after = Performance.now();
        return { type: "PONG", delta: after - before };
      })
    );
  })
);


来源:https://stackoverflow.com/questions/48183011/in-redux-observable-how-can-i-measure-the-epics-duration-time-when-running-comp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!