benchmark.py 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed 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.

W
wangjiawei04 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
import sys
import os
import base64
import yaml
import requests
import time
import json
try:
    from paddle_serving_server_gpu.pipeline import PipelineClient
except ImportError:
    from paddle_serving_server.pipeline import PipelineClient
import numpy as np
from paddle_serving_client.utils import MultiThreadRunner
from paddle_serving_client.utils import benchmark_args, show_latency
29 30


W
wangjiawei04 已提交
31 32 33 34 35 36 37 38 39 40 41 42
def parse_benchmark(filein, fileout):
    with open(filein, "r") as fin:
        res = yaml.load(fin)
        del_list = []
        for key in res["DAG"].keys():
            if "call" in key:
                del_list.append(key)
        for key in del_list:
            del res["DAG"][key]
    with open(fileout, "w") as fout:
        yaml.dump(res, fout, default_flow_style=False)

43

W
wangjiawei04 已提交
44 45 46 47 48 49 50 51 52
def gen_yml(device):
    fin = open("config.yml", "r")
    config = yaml.load(fin)
    fin.close()
    config["dag"]["tracer"] = {"interval_s": 10}
    if device == "gpu":
        config["op"]["det"]["local_service_conf"]["device_type"] = 1
        config["op"]["det"]["local_service_conf"]["devices"] = "2"
        config["op"]["rec"]["local_service_conf"]["device_type"] = 1
53 54
        config["op"]["rec"]["local_service_conf"]["devices"] = "2"
    with open("config2.yml", "w") as fout:
W
wangjiawei04 已提交
55 56
        yaml.dump(config, fout, default_flow_style=False)

57

W
wangjiawei04 已提交
58 59 60
def cv2_to_base64(image):
    return base64.b64encode(image).decode('utf8')

61

W
wangjiawei04 已提交
62 63
def run_http(idx, batch_size):
    print("start thread ({})".format(idx))
64
    url = "http://127.0.0.1:9999/ocr/prediction"
W
wangjiawei04 已提交
65
    start = time.time()
66 67
    test_img_dir = "imgs/"
    #test_img_dir = "rctw_test/images/"
68 69
    latency_list = []
    total_number = 0
W
wangjiawei04 已提交
70
    for img_file in os.listdir(test_img_dir):
71
        l_start = time.time()
W
wangjiawei04 已提交
72 73 74 75
        with open(os.path.join(test_img_dir, img_file), 'rb') as file:
            image_data1 = file.read()
        image = cv2_to_base64(image_data1)
        data = {"key": ["image"], "value": [image]}
76 77 78
        #for i in range(100):
        r = requests.post(url=url, data=json.dumps(data))
        print(r.json())
W
wangjiawei04 已提交
79
        end = time.time()
80 81 82 83 84
        l_end = time.time()
        latency_list.append(l_end * 1000 - l_start * 1000)
        total_number = total_number + 1
    return [[end - start], latency_list, [total_number]]

W
wangjiawei04 已提交
85 86 87

def multithread_http(thread, batch_size):
    multi_thread_runner = MultiThreadRunner()
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    start = time.time()
    result = multi_thread_runner.run(run_http, thread, batch_size)
    end = time.time()
    total_cost = end - start
    avg_cost = 0
    total_number = 0
    for i in range(thread):
        avg_cost += result[0][i]
        total_number += result[2][i]
    avg_cost = avg_cost / thread
    print("Total cost: {}s".format(total_cost))
    print("Each thread cost: {}s. ".format(avg_cost))
    print("Total count: {}. ".format(total_number))
    print("AVG QPS: {} samples/s".format(batch_size * total_number /
                                         total_cost))
    show_latency(result[1])

W
wangjiawei04 已提交
105 106 107 108 109

def run_rpc(thread, batch_size):
    client = PipelineClient()
    client.connect(['127.0.0.1:18090'])
    start = time.time()
110 111
    test_img_dir = "imgs/"
    #test_img_dir = "rctw_test/images/"
112 113
    latency_list = []
    total_number = 0
W
wangjiawei04 已提交
114
    for img_file in os.listdir(test_img_dir):
115
        l_start = time.time()
W
wangjiawei04 已提交
116 117 118
        with open(os.path.join(test_img_dir, img_file), 'rb') as file:
            image_data = file.read()
        image = cv2_to_base64(image_data)
119 120 121 122 123
        ret = client.predict(feed_dict={"image": image}, fetch=["res"])
        print(ret)
        l_end = time.time()
        latency_list.append(l_end * 1000 - l_start * 1000)
        total_number = total_number + 1
W
wangjiawei04 已提交
124
    end = time.time()
125
    return [[end - start], latency_list, [total_number]]
W
wangjiawei04 已提交
126 127 128 129


def multithread_rpc(thraed, batch_size):
    multi_thread_runner = MultiThreadRunner()
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    start = time.time()
    result = multi_thread_runner.run(run_rpc, thread, batch_size)
    end = time.time()
    total_cost = end - start
    avg_cost = 0
    total_number = 0
    for i in range(thread):
        avg_cost += result[0][i]
        total_number += result[2][i]
    avg_cost = avg_cost / thread
    print("Total cost: {}s".format(total_cost))
    print("Each thread cost: {}s. ".format(avg_cost))
    print("Total count: {}. ".format(total_number))
    print("AVG QPS: {} samples/s".format(batch_size * total_number /
                                         total_cost))
    show_latency(result[1])

W
wangjiawei04 已提交
147 148 149

if __name__ == "__main__":
    if sys.argv[1] == "yaml":
150
        mode = sys.argv[2]  # brpc/  local predictor
W
wangjiawei04 已提交
151 152 153 154
        thread = int(sys.argv[3])
        device = sys.argv[4]
        gen_yml(device)
    elif sys.argv[1] == "run":
155
        mode = sys.argv[2]  # http/ rpc
W
wangjiawei04 已提交
156 157 158 159 160 161 162 163 164 165
        thread = int(sys.argv[3])
        batch_size = int(sys.argv[4])
        if mode == "http":
            multithread_http(thread, batch_size)
        elif mode == "rpc":
            multithread_rpc(thread, batch_size)
    elif sys.argv[1] == "dump":
        filein = sys.argv[2]
        fileout = sys.argv[3]
        parse_benchmark(filein, fileout)