问题
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