我有一个跟踪参与者,当被调用时,它会启动一个新线程来完成跟踪工作。跟踪工作最终会这样做:
private static readonly ConcurrentDictionary<string, DateTime> TimingsDictionary = new ConcurrentDictionary<string, DateTime>();
private static readonly ConcurrentDictionary<Guid, DateTime> WorkflowTimingsDictionary = new ConcurrentDictionary<Guid, DateTime>();
private void TimeActivityRecord(TrackingRecord record)
{
var activityStateRecord = record as ActivityStateRecord;
if (activityStateRecord != null)
{
if (activityStateRecord.State == ActivityStates.Executing)
{
if (!TimingsDictionary.TryAdd(GetActivityId(activityStateRecord), activityStateRecord.EventTime))
{}
}
if (activityStateRecord.State == ActivityStates.Closed
|| activityStateRecord.State == ActivityStates.Faulted
|| activityStateRecord.State == ActivityStates.Canceled)
{
DateTime startTime;
if (TimingsDictionary.TryRemove(GetActivityId(activityStateRecord), out startTime))
{
timer.TimeAction("Executing workflow step " + activityStateRecord.Activity.Name, activityStateRecord.InstanceId, activityStateRecord.EventTime.Subtract(startTime));
}
}
}
}
private void TimeWorkflowRecords(TrackingRecord record)
{
var workflowInstanceRecord = record as WorkflowInstanceRecord;
if (workflowInstanceRecord != null)
{
if (workflowInstanceRecord.State == WorkflowInstanceStates.Started)
{
if (!WorkflowTimingsDictionary.TryAdd(workflowInstanceRecord.InstanceId, workflowInstanceRecord.EventTime))
{}
}
if (workflowInstanceRecord.State == WorkflowInstanceStates.Completed
|| workflowInstanceRecord.State == WorkflowInstanceStates.Aborted
|| workflowInstanceRecord.State == WorkflowInstanceStates.UnhandledException
|| workflowInstanceRecord.State == WorkflowInstanceStates.Terminated
|| workflowInstanceRecord.State == WorkflowInstanceStates.Canceled)
{
DateTime startTime;
if (WorkflowTimingsDictionary.TryRemove(workflowInstanceRecord.InstanceId, out startTime))
{
timer.TimeAction("Executing workflow", workflowInstanceRecord.InstanceId, workflowInstanceRecord.EventTime.Subtract(startTime));
}
}
}
}
跟踪整个工作流程时间和已配置活动的时间。但是,我得到了奇怪的结果,工作流实例本身似乎总是比最长的活动花费更少的时间。有时我的工作流程需要 16 毫秒,其中的 6 个活动需要 625 毫秒、150 毫秒、125 毫秒、93 毫秒、17 毫秒、78 毫秒。
我做错了什么吗?我觉得我一定错过了一些非常明显的东西,但我一生都看不到它是什么。