import random
import subprocess
import time

TEST_TIME = 10

proc = subprocess.Popen(['disorderCook.exe', "SELLEX", "CATS"], shell = False, stdin = subprocess.PIPE, stdout = subprocess.PIPE)

starttime = time.clock()

def get_response_from_process(proc, message):       # MUST MATCH THE REAL THING IN THE FRONTEND, ELSE DEADLOCK
    assert(isinstance(message, str))

    if not message.endswith("\n"):
        message += "\n"
    
    b_message = bytes(message, encoding = "ascii")
    
    proc.stdin.write(b_message)
    proc.stdin.flush()
    
    result = ""
    
    while 1:
        line = str(proc.stdout.readline(), encoding = "ascii")
        if line.strip() == ("END"):         # line ending with \r\n has been observed to happen
            return result
        else:
            result += line

n = 0

all_account_ids = dict()

while 1:
    # print(n)
    n += 1
    price = random.randint(1, 5000)
    qty = random.randint(1, 100)
    direction = random.choice([1, 2])
    orderType = random.choice([1, 1, 1, 1, 2, 3, 4])

    account = "account" + str(n // 1000)
    if account not in all_account_ids:
        all_account_ids[account] = len(all_account_ids)
    acc_id = all_account_ids[account]
    
    message = "ORDER {} {} {} {} {} {}".format(account, acc_id, qty, price, direction, orderType)
    raw_response = get_response_from_process(proc, message)
    # print(raw_response)
    
    # time.sleep(0.01)
    if time.clock() - starttime > TEST_TIME:
        break

print("{} orders placed in {} seconds".format(n, TEST_TIME))
print("= {} per second".format(n // TEST_TIME))

debug_info = get_response_from_process(proc, "__DEBUG_MEMORY__")
print(debug_info)

input()

@@ -189,34 +189,8 @@
 E)%0A%0A
-starttime = time.clock()%0A%0A
 def 
@@ -779,40 +779,169 @@
 ne%0A%0A
-n = 0%0A%0Aall_account_ids = dict()%0A
+# ----------------------------------------------------------------------%0A%0Aprint(%22Placing many orders...%22)%0Aall_account_ids = dict()%0Astarttime = time.clock()%0An = 0
 %0Awhi
@@ -1778,16 +1778,386 @@
 _info)%0A%0A
+%0Aprint(%22Getting many quotes...%22)%0Astarttime = time.clock()%0An = 0%0Awhile 1:%0A    n += 1%0A    raw_response = get_response_from_process(proc, %22QUOTE%22)%0A    # print(raw_response)%0A    %0A    # time.sleep(0.01)%0A    if time.clock() - starttime %3E TEST_TIME:%0A        break%0A%0Aprint(%22%7B%7D quotes received in %7B%7D seconds%22.format(n, TEST_TIME))%0Aprint(%22= %7B%7D per second%22.format(n // TEST_TIME))%0A%0A
 input()%0A

add mass quotes test