I’ve been searching through this website and have seen multiple references to time deltas, but haven’t quite found what I’m looking for.
Basically, I have a list of messages that are received by a comms server and I want to calcuate the latency time between each message out and in. It looks like this:
161336.934072 - TMsg out: [O] enter order. RefID [123] OrdID [4568]
161336.934159 - TMsg in: [A] accepted. ordID [456] RefNumber [123]
Mixed in with these messages are other messages as well, however, I only want to capture the difference between the Out messages and in messages with the same RefID.
So far, to sort out from the main log which messages are Tmessages I’ve been doing this, but it’s really inefficient. I don’t need to be making new files everytime.:
big_file = open('C:/Users/kdalton/Documents/Minicomm.txt', 'r')
small_file1 = open('small_file1.txt', 'w')
for line in big_file:
if 'T' in line: small_file1.write(line)
big_file.close()
small_file1.close()
How do I calculate the time deltas between the two messages and sort out these messages from the main log?
This generator function returns a tuple containing the id and the difference in timestamps between the out and in messages. (If you want to do something more complex with the time difference, check out
datetime.timedelta). Note that this assumes out messages always appear before in messages.You can now get a list out of it:
Or write it to a file:
Or chain it into a more complex workflow.
Update:
(in response to looking at the actual data)
Try this instead:
Changes in this version:
ordIDforinmessages is the one that matchesrefIDin theoutmessagesOuchMsginstead ofTMsgUpdate 2
To get an average of the deltas:
Or, if you have previously generated a list containing all the data, we can reuse it instead of reparsing the file: