Using the following extension method:
public static class EnumerableExtensions { public static IEnumerable<Task<TimedResult<TReturn>>> TimedSelect<TSource, TReturn>( this IEnumerable<TSource> source, Func<TSource, Task<TReturn>> func ) { if (source == null) throw new ArgumentNullException("source"); if (func == null) throw new ArgumentNullException("func"); return source.Select(x => { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Task<TReturn> task = func(x); Task<TimedResult<TReturn>> timedResultTask = task .ContinueWith(y => { stopwatch.Stop(); return new TimedResult<TReturn>(task, stopwatch.Elapsed); }); return timedResultTask; }); } } public class TimedResult<T> { internal TimedResult(Task<T> task, TimeSpan duration) { Task = task; Duration = duration; } public readonly Task<T> Task; public readonly TimeSpan Duration; }
And callsite
var tasks = statements.TimedSelect(statement => _session.ExecuteAsync(statement)); var result = Task.WhenAll(tasks).Result;
You can extract the results you need.
Note that this includes the pool timeout (the timeout when the task thread becomes available) and therefore may not be accurate.
Not a general version of this extension is an exercise for the reader.
source share