Step 1: Move vm scripts to the right place
[integration/test.git] / tools / clustering / cluster-debugging / transaction-tracking / process.py
1 #!/usr/bin/env python
2
3 from datetime import datetime
4 import collections
5
6
7 class Transaction:
8     def __init__(self, txnId, startTime, operations):
9         self.txnId = txnId
10         self.operations = operations
11         self.startTime = datetime.strptime(startTime,
12                                            '%Y-%m-%d,%H:%M:%S,%f')
13         self.reachedTime = None
14         self.completeTime = None
15
16     def setReachedTime(self, reachedTime):
17         self.reachedTime = datetime.strptime(reachedTime,
18                                              '%Y-%m-%d,%H:%M:%S,%f')
19
20     def setCompleteTime(self, completeTime):
21         self.completeTime = datetime.strptime(completeTime,
22                                               '%Y-%m-%d,%H:%M:%S,%f')
23
24     def totalTime(self):
25         return Transaction.diffInMicros(self.startTime, self.completeTime)
26
27     def transferTime(self):
28         return Transaction.diffInMicros(self.startTime, self.reachedTime)
29
30     @staticmethod
31     def diffInMicros(start, end):
32         if end is not None and start is not None:
33             delta = end - start
34             seconds = delta.seconds
35             microseconds = delta.microseconds
36             return (seconds * 1000000 + microseconds) / 1000
37         return -1
38
39     def __str__(self):
40         return "transactionId = " + self.txnId + ", " \
41                + "operations = " + unicode(self.operations) + ", " \
42                + "startTime = " + unicode(self.startTime) + ", " \
43                + "reachedTime = " + unicode(self.reachedTime) + ", " \
44                + "completeTime = " + unicode(self.completeTime) + ", " \
45                + "transferTime = " + unicode(self.transferTime()) + ", " \
46                + "totalTime = " + unicode(self.totalTime())
47
48     def csv(self):
49         return unicode(self.startTime) + "," \
50             + self.txnId + "," \
51             + unicode(self.operations) + "," \
52             + unicode(self.transferTime()) + "," \
53             + unicode(self.totalTime())
54
55     @staticmethod
56     def csv_header():
57         return "Start Time,Transaction Id,Operations,Transfer Time," \
58                "Complete Time"
59
60
61 def processFiles():
62     txns = collections.OrderedDict()
63     txnBegin = open("txnbegin.txt", "r")
64     for line in txnBegin:
65         arr = line.split(",")
66         txns[arr[3]] = Transaction(arr[3],
67                                    arr[0] + "," + arr[1] + "," + arr[2],
68                                    int(arr[4]))
69
70     txnReached = open("txnreached.txt", "r")
71     for line in txnReached:
72         arr = line.split(",")
73         txnId = arr[3].strip()
74         if txnId in txns:
75             txn = txns[txnId]
76             txn.setReachedTime(arr[0] + "," + arr[1] + "," + arr[2])
77
78     txnComplete = open("txnend.txt", "r")
79     for line in txnComplete:
80         arr = line.split(",")
81         txnId = arr[3].strip()
82         if txnId in txns:
83             txn = txns[txnId]
84             txn.setCompleteTime(arr[0] + "," + arr[1] + "," + arr[2])
85
86     return txns
87
88
89 def filterTransactionsByTimeToComplete(timeToComplete):
90     txns = processFiles()
91     totalTime = 0
92     for txn in txns:
93         if txns[txn].totalTime() > timeToComplete:
94             print txns[txn]
95             totalTime += txns[txn].totalTime()
96
97     print "Total time for these transactions = " + unicode(totalTime)
98
99
100 def csv():
101     txns = processFiles()
102     print Transaction.csv_header()
103     for txn in txns:
104         print txns[txn].csv()