#!/usr/bin/env python

__author__ = 'Miroslav Bauer'

# ...............................................
# Testing script for sending various request sets
# to datamorph application. This tries to emulate
# the web GUI user front-end.
# ...............................................

import sys
import zmq
import time
import json
import atexit
from templates import *
from random import randint
sys.path.append('..')
from datamorph.constants import CTRL_PROTO, CTRL_ENDPOINT

# Create and setup socket for sending requests
cx = zmq.Context()
request = cx.socket(zmq.DEALER)
request.identity = 'TESTSUITE'
request.connect('%s://%s' % (CTRL_PROTO, CTRL_ENDPOINT))

def terminate():
	request.close()

atexit.register(terminate)

requests = []

def cr(template):
    req = []
    req += COMPUTE
    req.append(str(randint(1,100)))
    req.append(template)
    return req

def runJobs():
    requests.append(cr(BIGDATA))
    requests.append(cr(CPUHEAVY))
    requests.append(cr(CPUHEAVY))
    requests.append(cr(CPUHEAVY))
    go()

def runLocalJobs():
    for i in range(10):
        requests.append(cr(CPUHEAVY_L))
    go()

def runGridJobs():
    for i in range(10):
        requests.append(cr(CPUHEAVY_G))
    go()

def runCmds():
    requests.append(PING)
    requests.append(RELAGS)
    requests.append(GETAGS)
    requests.append(ECHO)
    go()

def go():
    for r in requests:
            request.send_multipart(r)
    repnum = 0
    while repnum < len(requests):
            r = request.recv_multipart()
            if r[0] == 'RESULTS':
                print r
                repnum += 1
                print "DONE %d of %d" % (repnum, len(requests))

lst = time.time()
runLocalJobs()
le = time.time()
gst = lse
runGridJobs()
ge = time.time()

print le - lst
print ge - gst
