Contraboy
Contraboy

Reputation: 129

Get average of multiple threads running in a loop

Is there a way to get the average of all the requests. I am able to get the response time per execution as below(running as a loop for a set duration). But is there a way to sum all of these and get the average?

Output: Thread 0 Thread 1 request: 1 ResponseTime: 0.003963258999999941 request: 0 ResponseTime: 0.005142219999999975 Thread 0

I basically want to get the get the average of all the 4 entries above. NOTE: i am using start = time.process_time() & request_time = time.process_time() - start to get the response time. Thanks!!

Upvotes: 1

Views: 304

Answers (1)

naivecoder
naivecoder

Reputation: 116

If the thread termination leads to process termination then you need to store all the results to a file a create a separate code to calculate the average using the values in the file.

If the process keeps running even after the threads die then you can can do it in the following manner.

# Moving variable to keep track of average - global variable
average = 0

# Moving variable to keep track of number of threads that ran so far - global variable
n = 0

# On thread completion
def request(host, url, req_id):
    global average
    global n
    # Your code here 

    average = (average*n + new_thread_time) / (n+1)
    n += 1

Note that these variables need to be synchronized among threads.

Upvotes: 1

Related Questions