+ # Merge (started_at, finished_at) time range into the list of time ranges in
+ # timestamps (timestamps must be sorted and non-overlapping).
+ # return the updated timestamps list.
+ def merge_range timestamps, started_at, finished_at
+ # in the comments below, 'i' is the entry in the timestamps array and 'j'
+ # is the started_at, finished_at range which is passed in.
+ timestamps.each_index do |i|
+ if started_at
+ if started_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
+ # 'j' started and ended during 'i'
+ return timestamps
+ end
+
+ if started_at < timestamps[i][0] and finished_at >= timestamps[i][0] and finished_at <= timestamps[i][1]
+ # 'j' started before 'i' and finished during 'i'
+ # re-merge range between when 'j' started and 'i' finished
+ finished_at = timestamps[i][1]
+ timestamps.delete_at i
+ return merge_range timestamps, started_at, finished_at
+ end
+
+ if started_at >= timestamps[i][0] and started_at <= timestamps[i][1]
+ # 'j' started during 'i' and finished sometime after
+ # move end time of 'i' back
+ # re-merge range between when 'i' started and 'j' finished
+ started_at = timestamps[i][0]
+ timestamps.delete_at i
+ return merge_range timestamps, started_at, finished_at
+ end
+
+ if finished_at < timestamps[i][0]
+ # 'j' finished before 'i' started, so insert before 'i'
+ timestamps.insert i, [started_at, finished_at]
+ return timestamps
+ end
+ end
+ end
+
+ timestamps << [started_at, finished_at]
+ end
+
+ # Accept a list of objects with [:started_at] and [:finshed_at] keys and
+ # merge overlapping ranges to compute the time spent running after periods of
+ # overlapping execution are factored out.
+ def determine_wallclock_runtime jobs
+ timestamps = []
+ jobs.each do |j|
+ insert_at = 0
+ started_at = j[:started_at]
+ finished_at = (if j[:finished_at] then j[:finished_at] else Time.now end)
+ if started_at
+ timestamps = merge_range timestamps, started_at, finished_at
+ end
+ end
+ timestamps.map { |t| t[1] - t[0] }.reduce(:+) || 0
+ end
+