# Hello World
#
# A minimal script that tests The Grinder logging facility.
#
# This script shows the recommended style for scripts, with a
# TestRunner class. The script is executed just once by each worker
# process and defines the TestRunner class. The Grinder creates an
# instance of TestRunner for each worker thread, and repeatedly calls
# the instance for each run of that thread.

from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from HTTPClient import NVPair

# A shorter alias for the grinder.logger.info() method.
log = grinder.logger.info

# Create a Test with a test number and a description. The test will be
# automatically registered with The Grinder console if you are using
# it.
test1 = Test(1, "ClientTime")

headers = []
headers.append(NVPair("Content-type", "application/json"))
headers.append(NVPair("BC_ENABLED", "TRUE"))
convertRequest = HTTPRequest(headers=headers)
url = "http://localhost:8080/service/loadtest/fast/convert3"
#grinder.statistics.registerDataLogExpression("Start time at Server", "userLong0")
#grinder.statistics.registerDataLogExpression("Server time - Total", "userLong1")
#grinder.statistics.registerDataLogExpression("Server time - Web", "userLong2")
#grinder.statistics.registerDataLogExpression("Server time - Busi", "userLong3")
#grinder.statistics.registerDataLogExpression("Server time - External", "userLong4")
#grinder.statistics.registerSummaryExpression("Start time at Server", "userLong0")
#grinder.statistics.registerSummaryExpression("Server time - Total", "userLong1")
#grinder.statistics.registerSummaryExpression("Server time - Web", "userLong2")
#grinder.statistics.registerSummaryExpression("Server time - Busi", "userLong3")
#grinder.statistics.registerSummaryExpression("Server time - External", "userLong4")


# Instrument the info() method with our Test.
test1.record(convertRequest)

#test2 = Test(2, "ServerTime_Total")

class RequestData:
    def __init__(self):
        userFile = open("requests.txt", "r")
        self.datas = []
        try:
            for line in userFile:
                self.datas.append(line),
        finally:
            userFile.close()
#         self.datas = ['{"fromTimeZone": "Asia/Bangkok", "toTimeZone": "GMT", "inputDate": "2015-12-30 18:30:33"}', '{"fromTimeZone": "Asia/Bangkok", "toTimeZone": "GMT", "inputDate": "2013-01-15 02:30:33"}']

    def get(self, index):
        i = index % len(self.datas)
        return self.datas[i]

reqData = RequestData()
# A TestRunner instance is created for each thread. It can be used to
# store thread-specific data.
class TestRunner:

    # This method is called for every run.
    def __call__(self):
        jsonData = reqData.get(grinder.runNumber * grinder.threadNumber)
        response = convertRequest.POST(url, jsonData)
#        for s in response.listHeaders():
#            print s
#        startTimeAtServer = response.getHeader("StartTime")
#        if(startTimeAtServer is not None):
#            grinder.statistics.forLastTest.setLong("userLong0", int(startTimeAtServer))
#        totalTime = response.getHeader("TotalTime")
#        if(totalTime is not None):
#            grinder.statistics.forLastTest.setLong("userLong1", int(totalTime))
#        webserviceTime = response.getHeader("WebserviceTime")
#        if(webserviceTime is not None):
#            grinder.statistics.forLastTest.setLong("userLong2", int(webserviceTime))
#        businessTime = response.getHeader("Busi_InternalTime")
#        if(businessTime is not None):
#            grinder.statistics.forLastTest.setLong("userLong3", int(businessTime))
#        externalTime = response.getHeader("Busi_ExternalTime")
#        if(externalTime is not None):
#            grinder.statistics.forLastTest.setLong("userLong4", int(externalTime))
