raptor_timing.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #
  2. # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
  3. # All rights reserved.
  4. # This component and the accompanying materials are made available
  5. # under the terms of the License "Eclipse Public License v1.0"
  6. # which accompanies this distribution, and is available
  7. # at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. #
  9. # Initial Contributors:
  10. # Nokia Corporation - initial contribution.
  11. #
  12. # Contributors:
  13. #
  14. # Description:
  15. # timings API
  16. # This API can be used to start and stop timings in order to measure performance
  17. #
  18. import time
  19. class Timing(object):
  20. @classmethod
  21. def discovery_string(cls, object_type, count):
  22. """
  23. Returns a tag that can be used to show what is about to be
  24. "processed"
  25. Parameters:
  26. object_type - string
  27. Type of object that is about to be "processed" in this task
  28. count - int
  29. Number of objects of input "object_type" are about to be
  30. "processed"
  31. Returns:
  32. string
  33. XML tag in the format that can be printed directly to a
  34. Raptor log
  35. """
  36. return "<progress:discovery object_type='" + str(object_type) + \
  37. "' count='" + str(count) + "' />\n"
  38. @classmethod
  39. def start_string(cls, object_type, task, key):
  40. """
  41. Returns a tag that can be used to show what is being "processed"
  42. and the time it started
  43. Parameters:
  44. object_type - string
  45. Type of object that is being "processed" in this task
  46. task - string
  47. What is being done with the object being "processed"
  48. key - string
  49. Unique identifier for the object being "processed"
  50. Returns:
  51. string
  52. XML tag in the format that can be printed directly to a
  53. Raptor log
  54. """
  55. return "<progress:start object_type='" + str(object_type) + \
  56. "' task='" + str(task) + "' key='" + str(key) + \
  57. "' time='" + str(time.time()) + "' />\n"
  58. @classmethod
  59. def end_string(cls, object_type, task, key):
  60. """
  61. Returns a tag that can be used to show what was being "processed"
  62. and the time it finished
  63. Parameters:
  64. object_type - string
  65. Type of object that was being "processed" in this task
  66. task - string
  67. What was being done with the object being "processed"
  68. key - string
  69. Unique identifier for the object that was "processed"
  70. Returns:
  71. string
  72. XML tag in the format that can be printed directly to a
  73. Raptor log
  74. """
  75. return "<progress:end object_type='" + str(object_type) + \
  76. "' task='" + str(task) + "' key='" + str(key) + \
  77. "' time='" + str(time.time()) + "' />\n"
  78. @classmethod
  79. def custom_string(cls, tag = "duration", object_type = "all", task = "all",
  80. key = "all", time = 0.0):
  81. """
  82. Returns a custom tag in the 'progress' tag format
  83. Parameters:
  84. tag - string
  85. String to be used for the tag
  86. object_type - string
  87. Type of object that was being "processed" in this task
  88. task - string
  89. What was being done with the object being "processed"
  90. key - string
  91. Unique identifier for the object that was "processed"
  92. time - float
  93. The time to be included in the tag
  94. Returns:
  95. string
  96. XML tag in the format that can be printed directly to a
  97. Raptor log
  98. """
  99. time_string = "time"
  100. if tag == "duration":
  101. time_string = "duration"
  102. return "<progress:" + str(tag) + " object_type='" + str(object_type) + \
  103. "' task='" + str(task) + "' key='" + str(key) + \
  104. "' " + time_string + "='" + str(time) + "' />\n"
  105. @classmethod
  106. def extract_values(cls, source):
  107. """
  108. Takes, as input, a single tag of the format returned by one of the
  109. above progress functions. Will extract the attributes and
  110. return them as a dictionary. Returns an empty dictionary {}
  111. if the tag name is not recognised or there is a parse error
  112. Parameters:
  113. source - string
  114. The input string from which extracted attributes are
  115. required
  116. Returns:
  117. dictionary
  118. Dictionary containing the attributes extracted from the
  119. input string. Returns an empty dictionary {} if the
  120. tag name is not recognised or there is a parse error
  121. NB: This function will not work correctly if the 'source' variable
  122. contains multiple tags
  123. """
  124. import re
  125. attributes = {}
  126. try:
  127. match = re.match(re.compile(".*object_type='(?P<object_type>.*?)'"),
  128. source)
  129. attributes["object_type"] = match.group("object_type")
  130. except AttributeError, e:
  131. print e
  132. attributes["object_type"] = ""
  133. try:
  134. match = re.match(re.compile(".*task='(?P<task>.*?)'"), source)
  135. attributes["task"] = match.group("task")
  136. except AttributeError, e:
  137. print e
  138. attributes["task"] = ""
  139. try:
  140. match = re.match(re.compile(".*key='(?P<key>.*?)'"), source)
  141. attributes["key"] = match.group("key")
  142. except AttributeError:
  143. attributes["key"] = ""
  144. try:
  145. match = re.match(re.compile(".*time='(?P<time>.*?)'"), source)
  146. attributes["time"] = match.group("time")
  147. except AttributeError:
  148. attributes["time"] = ""
  149. try:
  150. match = re.match(re.compile(".*count='(?P<count>.*?)'"), source)
  151. attributes["count"] = match.group("count")
  152. except AttributeError:
  153. attributes["count"] = ""
  154. return attributes