Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # We have a log file with timestamps, named events, and a start/end indicator
- # We wrote some code that is intended to calculate the average duration of
- # each event, but it is producing some unexpected outputs. What's happening, and
- # how can it be fixed?
- # Test log file
- # 22000 get_foo start
- # 22050 get_foo end
- # 22200 get_bar start
- # 22250 get_bar end
- # 22400 get_foo end
- # 22500 get_bar start
- # 22550 get_bar end
- # 22580 get_foo start
- # 22600 get_bar end
- # 22610 get_foo end
- def main():
- # f = open("log.txt", "r") # open log file in read mode
- with open("log.txt", "r") as f:
- lines = f.readlines()
- event_start_times = {} # define empty dictionaries for event starts and total times
- total_times = {} # defines total time for each event type
- for line in lines: # Loop over each line in log file
- words = line.rstrip().split(" ") # Define each line in file as 'word' and strip characters to the right and split by space into a list
- timestamp = int(words[0]) # first element in 'words' and convert to int
- event = words[1] # second element in 'words' list
- event_type = words[2] # third element in 'words' list
- if event_type == "start":
- event_start_times[event] = timestamp # event_start dictionary stores the [event] as key and timestamp as value if 'start'
- elif event not in total_times: # if the event is not already stored in the total_times dict()
- start_timestamp = event_start_times[event] # Assign a value of the first timestamp that is encountered
- total_times[event] = ((timestamp - start_timestamp), 1) # find the total time and count of each independent event (get_foo / get_bar)
- else: # if event IS in total_times or type == 'end'
- time_count_pair = total_times[event] # variable that returns the total time difference of each event and number of times it has occured
- try:
- start_timestamp = event_start_times.pop(event) # Assign a value of the starting timestamp
- time = time_count_pair[0] + (timestamp - start_timestamp) # starting time + total time difference (THESE VARIABLES NEED TO BE SWAPPED 'star_timestamp' and 'timestamp'_
- count = time_count_pair[1] + 1
- total_times[event] = (time, count)
- except KeyError:
- print(f"Warning! Event {event} was not started. Skipping.")
- for event in total_times:
- time, count = total_times[event]
- average = time/count
- print(f"{event} took an average of {average} milliseconds.")
- main()
- # NamedTuple
- # One of the reasons for the problems - counting in events that are END but never started (like line 5)
- # exception handling
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement