test_rpc_proxy.py 2.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17
import tvm
18
from tvm import te
19 20 21 22
import logging
import numpy as np
import time
import multiprocessing
23
from tvm import rpc
24

25
def rpc_proxy_check():
26 27
    """This is a simple test function for RPC Proxy

28
    It is not included as pytests, because:
29 30 31 32 33 34 35 36
    - 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:
37
        from tvm.rpc import proxy
38
        web_port = 8888
39
        prox = proxy.Proxy("localhost", web_port=web_port)
40
        def check():
41
            if not tvm.runtime.enabled("rpc"):
42 43 44 45 46 47 48 49
                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(
50
                target=proxy.websocket_proxy_server,
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
                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)
67
    rpc_proxy_check()