Protobuf

Protobuf数据结构分析如下:

 mac安装protobuf:

protobuf的github主页:https://github.com/protocolbuffers/protobuf

release页面的tag标签下。找到对应的protobuf版本。

 

 下载下来后进入解压包目录下

 1、 ./configure

 2、  直接make

 3、 make check

 4、 make install在Mac上非root安装没有指定路径,执行步骤5报错,mkdirs denied

 5、 vim ~/.bash_profile添加

         export PROTOBUF=${安装dir}

         export PATH=$PROTOBUF/bin:$PATH

 6、protoc --version测试是否安装成功

 

实例如下:

proto文件如下:

syntax = "proto3";
package School.Student;


//电话号码类型说明
enum PhoneType {
MOBILE                                          =   0;
HOME                                            =   1;
WORK                                            =   2;
  }

//学生信息
message StudentInfo {
  string name                                   =   1;
  string id                                     =   2;
  int32 age                                     =   3;
  string email                                  =   4;
  }
  
//电话号码
message PhoneNumber {
    string number                               =   1;
    PhoneType type                               =  2;
  }

//采集父/母信息
message SampleParentInfo {
    oneof mother_or_father {
    string mother_name                          =   1;
    string father_name                          =   2;
    }
}

message StdMagRequest
{
//    map<string , string> class_info                 =   1;  //班级-代号说明
    repeated StudentInfo student_info            =   2;
    repeated PhoneNumber phone_info              =   3;
    repeated SampleParentInfo parent_info        =   4;
}

message StdMagResponse{
    int32 result                                   =    1;
//    string err_msg                                 =    2;
    repeated StudentInfo student_info              =    2;
}


service StdMagService {
    rpc Search (StdMagRequest) returns (StdMagResponse);
}

grpc客户端:

# -*- coding:utf-8 -*-

from protoFile.studentMagement_pb2 import *
from protoFile.studentMagement_pb2_grpc import *
from google.protobuf.json_format import MessageToDict, ParseDict
import json


class GrpcClient(object):
    def __init__(self):
        self.__HOST = 'localhost'
        self.__PORT = '50051'
        self.PhoneType = 0
        self.student_info = [
            {
                "name": "Nacy",
                "id": "80251111",
                "age": 17,
                "email": "80251111@139.com"
            }
        ]
        self.phone_number = [
            {
                "number": "18910214265",
                "type": 0
            }
        ]
        self.sample_parent_info = [
            {
                "mother_name": "abd"
            }
        ]
        self.stdmag_request = {
            # "class_info": {
            #     'key': "gaosan",
            #     'value': "1008"
            # },
            "student_info": self.student_info,
            "phone_info": self.phone_number,
            "parent_info": self.sample_parent_info
        }

    def run(self):
        with grpc.insecure_channel(self.__HOST + ":" + self.__PORT) as channel:   #
            client = StdMagServiceStub(channel)
            pbmsg = StdMagRequest()
            ParseDict(js_dict=self.stdmag_request, message=pbmsg)
            req_body = json.dumps(self.stdmag_request)
            print '请求Body:' + req_body
            response = MessageToDict(client.Search(pbmsg))
            return response

if __name__=="__main__":
    req_object = GrpcClient()
    response = req_object.run()
    print(response)

grpc服务端:

# -*- coding:utf-8 -*-
from protoFile.studentMagement_pb2 import *
from protoFile.studentMagement_pb2_grpc import *
import multiprocessing as mp
from multiprocessing import Pool, Queue, Lock
from concurrent import futures
import time


class StdMagService(StdMagServiceServicer):
    def Search(self, request, context):
        std_mag_response = {
            "result": 200,
            "student_info": [
                {
                    "name": "Nacy",
                    "id": "80251111",
                    "age": 17,
                    "email": "80251111@139.com"
                }
            ]
        }
        return StdMagResponse(std_mag_response)

def main():
    # 多线程服务器
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    servicer = StdMagService()
    add_StdMagServiceServicer_to_server(servicer, server)
    # 监听端口
    server.add_insecure_port('localhost:50051')
    # 开始接收请求进行服务
    server.start()
    try:
        print("running...")
        time.sleep(1000)
    except KeyboardInterrupt:
        print("stopping...")
        server.stop(0)


if __name__ == '__main__':
    main()

 

 protobuf与json性能对比实战:

 

# -*- coding:utf-8 -*-

import json
from protoFiles import personInfomation_pb2
from line_profiler import LineProfiler
from time import time
import sys


# def timeCount(func):
#     """函数执行时间统计"""
#     def intime(input_data):
#         starttime = time()
#         func(input_data)
#         curtime = time()
#         usetime = curtime - starttime
#         print usetime
#         return usetime
#     return intime

def parse_json():
    # 构造json格式数据
    data = {
        "name": "xiaowang",
        "id": 1,
        "email": "123@qq.com",
        "phones": {
            "number": "156888888"
        }
    }
    for number in range(1000):
        # json序列化
        data_json = json.dumps(data)
        # json反序列化
        result = json.loads(data_json)


def parse_protobuf():
    # 构造protobuf格式数据
    person = personInfomation_pb2.Person()
    person.name = "xiaowang"
    person.id = 1
    person.email = "123@qq.com"
    person.phones.number = "156888888"
    person.phones.type = 0
    for number in range(1000):
        # protobuf序列化
        data_protobuf = person.SerializeToString()
        # protobuf反序列化
        result_protobuf.ParseFromString(data_protobuf)

if __name__ == "__main__":
    result_protobuf = personInfomation_pb2.Person()
    prof01 = LineProfiler(parse_json)
    prof01.enable()
    parse_json()
    prof01.disable()
    prof01.print_stats(sys.stdout)

    prof02 = LineProfiler(parse_protobuf)
    prof02.enable()
    parse_protobuf()
    prof02.disable()
    prof02.print_stats(sys.stdout)

 

 

 

 

 

 

 

 

posted @ 2019-08-16 16:03  *陌上花开*  阅读(310)  评论(0编辑  收藏  举报