def test_pymode(self): args = set_frontend_parser().parse_args([]) p_args = set_preprocessor_parser().parse_args([ '--port_in', str(args.port_out), '--port_out', '5531', '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_BIND), '--yaml_path', '!UnaryPreprocessor {parameters: {doc_type: 1}}' ]) e_args = set_encoder_parser().parse_args([ '--port_in', str(p_args.port_out), '--port_out', str(args.port_in), '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_CONNECT), '--yaml_path', 'flair.yml', ]) with ServiceManager(EncoderService, e_args), \ ServiceManager(PreprocessorService, p_args), \ FrontendService(args), \ grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port), options=[('grpc.max_send_message_length', 70 * 1024 * 1024), ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel: stub = gnes_pb2_grpc.GnesRPCStub(channel) resp = stub.Call(list(RequestGenerator.index([b'hello world', b'goodbye!'], 1))[0]) self.assertEqual(resp.request_id, '0')
def test_pymode(self): args = set_frontend_parser().parse_args([]) p_args = set_preprocessor_parser().parse_args([ '--port_in', str(args.port_out), '--port_out', '5531', '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_BIND), '--yaml_path', 'SentSplitPreprocessor' ]) e_args = set_encoder_parser().parse_args([ '--port_in', str(p_args.port_out), '--port_out', str(args.port_in), '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_CONNECT), '--yaml_path', 'transformer.yml', '--py_path', 'distilbert.py' ]) with ServiceManager(EncoderService, e_args), \ ServiceManager(PreprocessorService, p_args), \ FrontendService(args), \ grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port), options=[('grpc.max_send_message_length', 70 * 1024 * 1024), ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel: stub = gnes_pb2_grpc.GnesRPCStub(channel) resp = stub.Call( list( RequestGenerator.index([ b'hello world, good to see you.', b'goodbye! wish to see you again!' ], 1))[0]) self.assertEqual(resp.request_id, 0)
def test_fasterrcnn_preprocessor(self): args = set_preprocessor_parser().parse_args( ['--yaml_path', self.fasterrcnn_yaml]) c_args = _set_client_parser().parse_args( ['--port_in', str(args.port_out), '--port_out', str(args.port_in)]) all_zips = zipfile.ZipFile(self.data_path) all_bytes = [all_zips.open(v).read() for v in all_zips.namelist()] with ServiceManager(PreprocessorService, args), ZmqClient(c_args) as client: for req in RequestGenerator.index(all_bytes): msg = gnes_pb2.Message() msg.request.index.CopyFrom(req.index) client.send_message(msg) r = client.recv_message() for d in r.request.index.docs: self.assertGreater(len(d.chunks), 0) for _ in range(len(d.chunks)): self.assertEqual( len(blob2array(d.chunks[_].blob).shape), 3) self.assertEqual( blob2array(d.chunks[_].blob).shape[-1], 3) self.assertEqual( blob2array(d.chunks[_].blob).shape[0], 224) self.assertEqual( blob2array(d.chunks[_].blob).shape[1], 224) print(blob2array(d.chunks[0].blob).dtype)
def test_health_check(self): a = set_router_parser().parse_args([ '--yaml_path', 'BaseRouter', ]) a1 = copy.deepcopy(a) b = set_healthcheck_parser().parse_args(['--port', str(a.port_ctrl)]) # before - fail with self.assertRaises(SystemExit) as cm: healthcheck(b) self.assertEqual(cm.exception.code, 1) # running - success with self.assertRaises(SystemExit) as cm: with RouterService(a): time.sleep(2) healthcheck(b) self.assertEqual(cm.exception.code, 0) # running - managerservice - success with self.assertRaises(SystemExit) as cm: with ServiceManager(RouterService, a1): time.sleep(2) healthcheck(b) self.assertEqual(cm.exception.code, 0) # after - fail with self.assertRaises(SystemExit) as cm: healthcheck(b) self.assertEqual(cm.exception.code, 1)
def _test_multiple_router(self, backend='thread', num_parallel=5): a = set_router_parser().parse_args([ '--yaml_path', 'BaseRouter', '--num_parallel', str(num_parallel), '--parallel_backend', backend ]) with ServiceManager(RouterService, a): pass
def _test_grpc_multiple_pub(self, backend='thread', num_parallel=5): args = set_frontend_parser().parse_args([ '--grpc_host', '127.0.0.1', ]) p_args = set_router_parser().parse_args([ '--port_in', str(args.port_out), '--port_out', str(args.port_in), '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_CONNECT), '--yaml_path', 'BaseRouter', '--num_parallel', str(num_parallel), '--parallel_backend', backend, '--parallel_type', str(ParallelType.PUB_BLOCK) ]) with ServiceManager( RouterService, p_args), FrontendService(args), grpc.insecure_channel( '%s:%d' % (args.grpc_host, args.grpc_port), options=[ ('grpc.max_send_message_length', 70 * 1024 * 1024), ('grpc.max_receive_message_length', 70 * 1024 * 1024) ]) as channel: stub = gnes_pb2_grpc.GnesRPCStub(channel) resp = stub.Call(list(RequestGenerator.query(b'abc', 1))[0]) self.assertEqual(resp.request_id, 0)
def test_pymode(self): args = set_frontend_parser().parse_args([ '--socket_in', str(SocketType.PULL_BIND), '--socket_out', str(SocketType.PUSH_BIND), ]) p_args = set_preprocessor_parser().parse_args([ '--port_in', str(args.port_out), '--port_out', str(args.port_in), '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_CONNECT), '--yaml_path', 'pipline.yml', '--py_path', 'mypreprocessor1.py', 'mypreprocessor2.py' ]) with ServiceManager(PreprocessorService, p_args), \ FrontendService(args), \ grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port), options=[('grpc.max_send_message_length', 70 * 1024 * 1024), ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel: stub = gnes_pb2_grpc.GnesRPCStub(channel) resp = stub.Call( list(RequestGenerator.index([b'doc1:', b'doc2:'], 1))[0]) self.assertEqual(resp.request_id, '0')
def test_pymode(self): os.unsetenv('http_proxy') os.unsetenv('https_proxy') args = set_frontend_parser().parse_args([]) p_args = set_preprocessor_parser().parse_args([ '--port_in', str(args.port_out), '--port_out', '5531', '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_BIND), '--yaml_path', 'SentSplitPreprocessor' ]) e_args = set_indexer_parser().parse_args([ '--port_in', str(p_args.port_out), '--port_out', str(args.port_in), '--socket_in', str(SocketType.PULL_CONNECT), '--socket_out', str(SocketType.PUSH_CONNECT), '--yaml_path', '!DictIndexer {gnes_config: {name: dummy_dict_indexer}}', ]) with ServiceManager(IndexerService, e_args), \ ServiceManager(PreprocessorService, p_args), \ FrontendService(args), \ grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port), options=[('grpc.max_send_message_length', 70 * 1024 * 1024), ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel: stub = gnes_pb2_grpc.GnesRPCStub(channel) all_bytes = [] with open(os.path.join(self.dirname, '26-doc-chinese.txt'), 'r', encoding='utf8') as fp: for v in fp: if v.strip(): all_bytes.append(v.encode()) for r in stub.StreamCall(RequestGenerator.index(all_bytes)): print(r) bi = BaseIndexer.load('dummy_dict_indexer.bin') self.assertEqual(bi.size, 26) print(bi.query([0]))
def test_override_module(self): args = set_indexer_parser().parse_args([ '--yaml_path', os.path.join(self.dir_path, 'contrib', 'fake_faiss.yml'), '--py_path', os.path.join(self.dir_path, 'contrib', 'fake_faiss.py'), ]) with ServiceManager(IndexerService, args): pass
def test_external_module(self): args = set_encoder_parser().parse_args([ '--yaml_path', os.path.join(self.dir_path, 'contrib', 'dummy.yml'), '--py_path', os.path.join(self.dir_path, 'contrib', 'dummy_contrib.py'), ]) with ServiceManager(RouterService, args): pass
def test_frontend_alone(self): args = set_frontend_parser().parse_args([ '--grpc_host', '127.0.0.1', ]) with FrontendService(args): pass with ServiceManager(FrontendService, args): pass
def test_override_module(self): args = set_indexer_parser().parse_args([ '--yaml_path', os.path.join(self.dir_path, 'contrib', 'fake_faiss.yml'), '--py_path', os.path.join(self.dir_path, 'contrib', 'fake_faiss.py'), ]) with ServiceManager(IndexerService, args): pass self.assertTrue(os.path.exists('foo_contrib_encoder.bin')) os.remove('foo_contrib_encoder.bin')
def test_external_module(self): args = set_encoder_parser().parse_args([ '--yaml_path', os.path.join(self.dir_path, 'contrib', 'dummy.yml'), '--py_path', os.path.join(self.dir_path, 'contrib', 'dummy_contrib.py'), ]) with ServiceManager(RouterService, args): pass self.assertTrue(os.path.exists('foo_contrib_encoder.bin')) os.remove('foo_contrib_encoder.bin')
def test_empty_service(self): args = set_encoder_parser().parse_args(['--yaml_path', '!TestEncoder {gnes_config: {name: EncoderService, is_trained: true}}']) c_args = _set_client_parser().parse_args([ '--port_in', str(args.port_out), '--port_out', str(args.port_in)]) with ServiceManager(EncoderService, args), ZmqClient(c_args) as client: msg = gnes_pb2.Message() d = msg.request.index.docs.add() d.doc_type = gnes_pb2.Document.IMAGE c = d.chunks.add() c.blob.CopyFrom(array2blob(self.test_numeric)) client.send_message(msg) r = client.recv_message() self.assertEqual(len(r.request.index.docs), 1) self.assertEqual(r.response.index.status, gnes_pb2.Response.SUCCESS)
def test_video_decode_preprocessor(self): args = set_preprocessor_parser().parse_args(['--yaml_path', self.yml_path]) c_args = _set_client_parser().parse_args([ '--port_in', str(args.port_out), '--port_out', str(args.port_in)]) video_bytes = [ open(os.path.join(self.video_path, _), 'rb').read() for _ in os.listdir(self.video_path) ] with ServiceManager(PreprocessorService, args), ZmqClient(c_args) as client: for req in RequestGenerator.index(video_bytes): msg = gnes_pb2.Message() msg.request.index.CopyFrom(req.index) client.send_message(msg) r = client.recv_message() for d in r.request.index.docs: self.assertGreater(len(d.chunks), 0) for _ in range(len(d.chunks)): shape = blob2array(d.chunks[_].blob).shape self.assertEqual(shape[1:], (299, 299, 3))
def test_hc_os_env(self): os.environ['GNES_CONTROL_PORT'] = str(56789) a = set_router_parser().parse_args([ '--yaml_path', 'BaseRouter', ]) a1 = copy.deepcopy(a) b = set_healthcheck_parser().parse_args( ['--port', os.environ.get('GNES_CONTROL_PORT')]) # before - fail with self.assertRaises(SystemExit) as cm: healthcheck(b) self.assertEqual(cm.exception.code, 1) # running - success with self.assertRaises(SystemExit) as cm: with RouterService(a): time.sleep(2) healthcheck(b) self.assertEqual(cm.exception.code, 0) # running - managerservice - success with self.assertRaises(SystemExit) as cm: with ServiceManager(RouterService, a1): time.sleep(2) healthcheck(b) self.assertEqual(cm.exception.code, 0) # after - fail with self.assertRaises(SystemExit) as cm: healthcheck(b) self.assertEqual(cm.exception.code, 1) os.unsetenv('GNES_CONTROL_PORT')
def test_empty_service(self): args = set_indexer_parser().parse_args([ '--yaml_path', '!BaseChunkIndexer {gnes_config: {name: IndexerService}}' ]) c_args = _set_client_parser().parse_args( ['--port_in', str(args.port_out), '--port_out', str(args.port_in)]) with ServiceManager(IndexerService, args), ZmqClient(c_args) as client: msg = gnes_pb2.Message() d = msg.request.index.docs.add() c = d.chunks.add() c.doc_id = 0 c.embedding.CopyFrom(array2blob(self.test_numeric)) c.offset = 0 c.weight = 1.0 client.send_message(msg) r = client.recv_message() self.assertEqual(r.response.index.status, gnes_pb2.Response.SUCCESS)
def test_empty_service(self): args = set_encoder_parser().parse_args([ '--yaml_path', self.yaml_path ]) with ServiceManager(EncoderService, args): pass
def test_empty_service(self): args = set_preprocessor_parser().parse_args([ '--yaml_path', self.yml_path ]) with ServiceManager(PreprocessorService, args): pass