test_rpc_proxy.py 1.64 KB
Newer Older
1 2 3 4 5
import tvm
import logging
import numpy as np
import time
import multiprocessing
6
from tvm import rpc
7

8
def rpc_proxy_check():
9 10 11 12 13 14 15 16 17 18 19
    """This is a simple test function for RPC Proxy

    It is not included as nosetests, because:
    - It depends on tornado
    - It relies on the fact that Proxy starts before client and server connects,
      which is often the case but not always

    User can directly run this script to verify correctness.
    """

    try:
20
        from tvm.rpc import proxy
21
        web_port = 8888
22
        prox = proxy.Proxy("localhost", web_port=web_port)
23 24 25 26 27 28 29 30 31 32
        def check():
            if not tvm.module.enabled("rpc"):
                return
            @tvm.register_func("rpc.test2.addone")
            def addone(x):
                return x + 1
            @tvm.register_func("rpc.test2.strcat")
            def addone(name, x):
                return "%s:%d" % (name, x)
            server = multiprocessing.Process(
33
                target=proxy.websocket_proxy_server,
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
                args=("ws://localhost:%d/ws" % web_port,"x1"))
            # Need to make sure that the connection start after proxy comes up
            time.sleep(0.1)
            server.deamon = True
            server.start()
            client = rpc.connect(prox.host, prox.port, key="x1")
            f1 = client.get_function("rpc.test2.addone")
            assert f1(10) == 11
            f2 = client.get_function("rpc.test2.strcat")
            assert f2("abc", 11) == "abc:11"
        check()
    except ImportError:
        print("Skipping because tornado is not avaliable...")

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
50
    rpc_proxy_check()