def pollStats(connection, sessionUrl, watchedStatsDict, pollingInterval=4): ''' This method is used to poll the stats. Polling stats is per request but this method does a continuous poll. Args: - connection is the connection object that manages the HTTP data transfers between the client and the REST API - sessionUrl is the address of the session that should run the test - watchedStatsDict these are the stats that are being monitored - pollingInterval the polling interval is 4 by default but can be overridden. ''' statSourceList = list(watchedStatsDict) # retrieve stats for a given stat dict # all the stats will be saved in the dictionary below #statsDict format: # { # statSourceName: { # timestamp: { # statCaption : value # } # } # } statsDict = {} # remember the timstamps that were already collected - will be ignored in future collectedTimestamps = {} # format { statSource : [2000, 4000, ...] } testIsRunning = True # check stat sources for statSource in statSourceList[:]: statSourceUrl = "%s/ixload/stats/%s/values" % (sessionUrl, statSource) statSourceReply = connection.httpRequest("GET", statSourceUrl) if statSourceReply.status_code != 200: log("Warning - Stat source '%s' does not exist. Will ignore it." % (statSource)) statSourceList.remove(statSource) # check the test state, and poll stats while the test is still running while testIsRunning: # the polling interval is configurable. by default, it's set to 4 seconds time.sleep(pollingInterval) for statSource in statSourceList: valuesUrl = "%s/ixload/stats/%s/values" % (sessionUrl, statSource) valuesObj = connection.httpGet(valuesUrl) valuesDict = valuesObj.getOptions() # get just the new timestamps - that were not previously retrieved in another stats polling iteration newTimestamps = [int(timestamp) for timestamp in list(valuesDict) if timestamp not in collectedTimestamps.get(statSource, [])] newTimestamps.sort() for timestamp in newTimestamps: timeStampStr = str(timestamp) collectedTimestamps.setdefault(statSource, []).append(timeStampStr) timestampDict = statsDict.setdefault(statSource, {}).setdefault(timestamp, {}) # save the values for the current timestamp, and later print them for caption, value in iteritems(valuesDict[timeStampStr].getOptions()): if caption in watchedStatsDict[statSource]: log("Timestamp %s - %s -> %s" % (timeStampStr, caption, value)) timestampDict[caption] = value testIsRunning = getTestCurrentState(connection, sessionUrl) == "Running" log("Stopped receiving stats.")