본문 바로가기
IT

gRPC 테스트 python

by Dblog 2021. 6. 1.
728x90

가이드를 따라서 pingpong 테스트를 해보려 합니다.

 

https://grpc.io/docs/languages/cpp/basics/

 

Basics tutorial

A basic tutorial introduction to gRPC in C++.

grpc.io

 

개발환경

 

Server

  • ubuntu 18.04
  • python3

 

Client

  • ubuntu 20.04
  • python3

 

 

먼저 proto3 IDL을 사용하기 때문에 가이드에 맞게 pingpong 코드를 작성합니다.

https://developers.google.com/protocol-buffers/docs/proto3

 

Language Guide (proto3)  |  Protocol Buffers  |  Google Developers

This guide describes how to use the protocol buffer language to structure your protocol buffer data, including .proto file syntax and how to generate data access classes from your .proto files. It covers the proto3 version of the protocol buffers language:

developers.google.com

 

syntax ="proto3";
package dy;

service PingPongService {
    rpc ping (Ping) returns (Pong) {}
}
message Ping
{
    uint32 count=1;    
}

message Pong
{
    uint32 count=1;    
}

 

작성이 완료되면 IDL 파일을 가지고 gRPC로 코드를 generate 합니다.

pingpong_pb2_grpc.py
pingpong_pb2.py

완료된 두개의 파일을 import 해서 서버/클라이언트 프로그램을 개발해 보겠습니다.

 

Server

from concurrent import futures
import grpc
import pingpong_pb2
import pingpong_pb2_grpc
import time
import threading


class Listener(pingpong_pb2_grpc.PingPongServiceServicer):
    def __init__(self):
        self.counter = 0
        self.lastPrintTime = time.time()

    def ping(self,request, context):
        self.counter += 1
        if (self.counter > 1000):
            print("10000 calls in %3f seconds" % (time.time() - self.lastPrintTime))
            self.lastPrintTime = time.time()
            self.counter = 0

        return pingpong_pb2.Pong(count=request.count + 1)


def server():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
    pingpong_pb2_grpc.add_PingPongServiceServicer_to_server(Listener(), server)
    server.add_insecure_port("[::]:9999")
    server.start()
    try:
        while True:
            print("server on: threads %i" % (threading.active_count()))
            time.sleep(10)
    except KeyboardInterrupt:
        print("KeyboardInterrupt")
        server.stop(0)


if __name__ == "__main__":
    server()

 

Client

import os
import pingpong_pb2
import pingpong_pb2_grpc
import time
import grpc


def run():
    counter = 0
    pid = os.getpid()
    with grpc.insecure_channel("localhost:9999") as channel:
        stub = pingpong_pb2_grpc.PingPongServiceStub(channel)
        while True:
            try:
                start = time.time()
                response = stub.ping(pingpong_pb2.Ping(count=counter))
                counter = response.count
                if counter % 1000 == 0:
                    print("%4f : resp=%s : procid=%i" % (time.time() - start, response.count, pid))

            except KeyboardInterrupt:
                print("KeyboardInterrupt")
                channel.unsubscribe(close)
                exit()

def close(channel):
    channel.close()


if __name__ == "__main__":
    run()

 

두 파일을 작성할때 주의할 점은 서버 코드에 ping()의 대소문자 까지 정확하게 작성해야 한다는 것 입니다.

이 확인 사항말고 다른 코드에서는 서버는 requset를 listen 상태에 두고 server/client 사이에 port 설정만 잘 해주면 됩니다.

 

테스트 결과

server console

 

client console

 

--Refer

https://www.youtube.com/watch?v=dQK0VLahrDk 

728x90

'IT' 카테고리의 다른 글

RPC, IPC 그리고 gRPC  (0) 2021.06.14
gRPC 테스트 C++  (0) 2021.06.07
gRPC 설치 - c++  (0) 2021.06.01
[LINUX] 리눅스 명령어,  (0) 2021.03.18
[Docker] 삽질의 역사(1) 명령어  (0) 2021.03.15

댓글