private ReportContext ResetContext(ReportContext context)
        {
            if (context == null)
            {
                // builds an aggregate of virtual user counts by status.
                Func<IList<VirtualUserDto>> getUsers = () => statuses
                    .Select(s => new VirtualUserDto
                    {
                        Status = s,
                        Count = users.Count(u => u.State == s)
                    })
                    .ToList();

                // extracts all profiled results for the current period.
                Func<IList<ProfileItem>> snapshot = () =>
                {
                    ProfileItem item;
                    IList<ProfileItem> items = new List<ProfileItem>();
                    while (period.TryTake(out item))
                    {
                        items.Add(item);
                    }
                    return items;
                };

                // extracts all profile pending.
                Func<IList<RequestItem>> pending = () =>
                                                              {
                                                                  return periodPending.Values.ToList();
                                                              };

                context = new ReportContext
                {
                    RequestFactory = this,
                    Total = totalRequests,
                    Users = getUsers,
                    Snapshot = snapshot,
                    Pending = pending
                };
            }

            context.InternalId++;
            context.LastReportedPosition = context.Position;
            context.Duration = TimeSpan.Zero;
            context.Started = DateTime.UtcNow;
            context.Stopwatch = Stopwatch.StartNew();

            return context;
        }
 private void UpdateContext(ReportContext context)
 {
     context.Position = period.Count;
     context.Duration = context.Stopwatch.Elapsed;
 }
        private void ReportingLoop(ReportContext context)
        {
            if (!report.Enabled) // sanity.
            {
                log.Debug(Debugging.ProfiledVirtualUserNetwork_ReportingDisabled.FormatWith(this));
                return;
            }
            log.Debug(Debugging.ProfiledVirtualUserNetwork_ReportingLoop.FormatWith(this));

            DateTime lastReportStarted = context.Started;
            do
            {
                lastReportStarted = WaitBeforeReporting(lastReportStarted, report.Interval);

                UpdateContext(context);

                var reported = report.TryReport(context);
                if (reported)
                {
                    ResetContext(context);
                }
            } while (context.Position < context.Total && !isComplete && !Aborted);

            log.Debug(Debugging.ProfiledVirtualUserNetwork_ReportingLoopComplete.FormatWith(this));
        }