sbsv2/raptor/python/raptor_timing.py
branchwip
changeset 30 01c962c3f631
equal deleted inserted replaced
29:ee00c00df073 30:01c962c3f631
       
     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 
       
    20 class Timing(object):
       
    21 	
       
    22 	@classmethod
       
    23 	def discovery_string(cls, object_type, count):
       
    24 		"""
       
    25 			Returns a tag that can be used to show what is about to be
       
    26 					"processed"
       
    27 			Parameters:
       
    28 				object_type - string
       
    29 					Type of object that is about to be "processed" in this task
       
    30 				count - int
       
    31 					Number of objects of input "object_type" are about to be
       
    32 							"processed"
       
    33 			Returns:
       
    34 				string
       
    35 					XML tag in the format that can be printed directly to a
       
    36 							Raptor log
       
    37 		"""
       
    38 		return "<progress:discovery object_type='" + str(object_type) + \
       
    39 				"' count='" + str(count) + "' />\n"
       
    40 				
       
    41 	
       
    42 	@classmethod
       
    43 	def start_string(cls, object_type, task, key):
       
    44 		"""
       
    45 			Returns a tag that can be used to show what is being "processed"
       
    46 					and the time it started
       
    47 			Parameters:
       
    48 				object_type - string
       
    49 					Type of object that is being "processed" in this task
       
    50 				task - string
       
    51 					What is being done with the object being "processed"
       
    52 				key - string
       
    53 					Unique identifier for the object being "processed"
       
    54 			Returns:
       
    55 				string
       
    56 					XML tag in the format that can be printed directly to a
       
    57 							Raptor log
       
    58 		"""
       
    59 		return "<progress:start object_type='" + str(object_type) + \
       
    60 				"' task='" + str(task) + "' key='" + str(key) + \
       
    61 				"' time='" + str(time.time()) + "' />\n"
       
    62 	
       
    63 	
       
    64 	@classmethod
       
    65 	def end_string(cls, object_type, task, key):
       
    66 		"""
       
    67 			Returns a tag that can be used to show what was being "processed"
       
    68 					and the time it finished
       
    69 			Parameters:
       
    70 				object_type - string
       
    71 					Type of object that was being "processed" in this task
       
    72 				task - string
       
    73 					What was being done with the object being "processed"
       
    74 				key - string
       
    75 					Unique identifier for the object that was "processed"
       
    76 			Returns:
       
    77 				string
       
    78 					XML tag in the format that can be printed directly to a
       
    79 							Raptor log
       
    80 		"""
       
    81 		return "<progress:end object_type='" + str(object_type) + \
       
    82 				"' task='" + str(task) + "' key='" + str(key) + \
       
    83 				"' time='" + str(time.time()) + "' />\n"
       
    84 	
       
    85 	
       
    86 	@classmethod
       
    87 	def custom_string(cls, tag = "duration", object_type = "all", task = "all",
       
    88 			key = "all", time = 0.0):
       
    89 		"""
       
    90 			Returns a custom tag in the 'progress' tag format
       
    91 			
       
    92 			Parameters:
       
    93 				tag - string
       
    94 					String to be used for the tag 
       
    95 				object_type - string
       
    96 					Type of object that was being "processed" in this task
       
    97 				task - string
       
    98 					What was being done with the object being "processed"
       
    99 				key - string
       
   100 					Unique identifier for the object that was "processed"
       
   101 				time - float
       
   102 					The time to be included in the tag
       
   103 			Returns:
       
   104 				string
       
   105 					XML tag in the format that can be printed directly to a
       
   106 							Raptor log
       
   107 		"""		
       
   108 		time_string = "time"
       
   109 		if tag == "duration":
       
   110 			time_string = "duration" 
       
   111 		return "<progress:" + str(tag) + " object_type='" + str(object_type) + \
       
   112 				"' task='" + str(task) + "' key='" + str(key) + \
       
   113 				"' " + time_string + "='" + str(time) + "' />\n"
       
   114 	
       
   115 	
       
   116 	@classmethod
       
   117 	def extract_values(cls, source):
       
   118 		"""
       
   119 			Takes, as input, a single tag of the format returned by one of the
       
   120 					above progress functions. Will extract the attributes and
       
   121 					return them as a dictionary. Returns an empty dictionary {}
       
   122 					if the tag name is not recognised or there is a parse error
       
   123 			Parameters:
       
   124 				source - string
       
   125 					The input string from which extracted attributes are
       
   126 							required
       
   127 			Returns:
       
   128 				dictionary
       
   129 					Dictionary containing the attributes extracted from the
       
   130 							input string. Returns an empty dictionary {} if the
       
   131 							tag name is not recognised or there is a parse error
       
   132 			NB: This function will not work correctly if the 'source' variable
       
   133 					contains multiple tags
       
   134 		"""
       
   135 		import re
       
   136 		
       
   137 		attributes = {}
       
   138 					
       
   139 		try:
       
   140 			match = re.match(re.compile(".*object_type='(?P<object_type>.*?)'"),
       
   141 					source)
       
   142 			attributes["object_type"] = match.group("object_type")
       
   143 		except AttributeError, e:
       
   144 			print e
       
   145 			attributes["object_type"] = ""
       
   146 		try:
       
   147 			match = re.match(re.compile(".*task='(?P<task>.*?)'"), source)
       
   148 			attributes["task"] = match.group("task")
       
   149 		except AttributeError, e:
       
   150 			print e
       
   151 			attributes["task"] = ""
       
   152 		try:
       
   153 			match = re.match(re.compile(".*key='(?P<key>.*?)'"), source)
       
   154 			attributes["key"] = match.group("key")
       
   155 		except AttributeError:
       
   156 			attributes["key"] = ""
       
   157 		try:
       
   158 			match = re.match(re.compile(".*time='(?P<time>.*?)'"), source)
       
   159 			attributes["time"] = match.group("time")
       
   160 		except AttributeError:
       
   161 			attributes["time"] = ""
       
   162 		try:
       
   163 			match = re.match(re.compile(".*count='(?P<count>.*?)'"), source)
       
   164 			attributes["count"] = match.group("count")
       
   165 		except AttributeError:
       
   166 			attributes["count"] = ""
       
   167 			
       
   168 		return attributes