模拟多socket客户端连接server进行代码压力测试

项目中遇到需要对C程序代码进行压力测试,需要模拟大量并发相关报文上报server,与服务端进行交互,测试服务端并发处理能力,本文使用Python模拟报文并进行相关并发会话建立


目标:1、按照接口协议模拟socket报文;2、建立多socket会话向C语言编写的服务端进行连接,进行压力测试;

遇到主要的问题:1、系统不支持大于1000的并发数,需要调整系统参数,ulimit -n 1024,具体设置此处不详细介绍


代码如下:


#/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: MengJie Qian

import datetime,time,os
import socket, struct,random

msg_head = 'AA766200010000'
status = '01'
gyro = '808A00000000'
other = '00' * 5 + '00' + '52'
#data='aa76b12f900064110407172239001a13070e3c00662600dcbe02b554010001800180060a901b6e00f1be5f5d00000000000000003436303031383033323734393933370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006144'

def int_hex(x):
    if type(x) == str:
        x_hex = hex(int(x))
    elif type(x) == int:
        x_hex = hex(x)
    else:
        print("Input a right number or string ...")
    return  x_hex[2:].zfill(2)

def get_date():
    #tm_year=2017, tm_mon=8, tm_mday=4, tm_hour=9, tm_min=4, tm_sec=49, tm_wday=4, tm_yday=216, tm_isdst=0)
    now = time.gmtime()
    year = str(now.tm_year)[2:]
    month = now.tm_mon
    day = now.tm_mday
    hour = now.tm_hour
    min = now.tm_min
    sec = now.tm_sec
    y = int_hex(year)

    mon= int_hex(month)
    d = int_hex(day)
    h = int_hex(hour)
    min = int_hex(min)
    s = int_hex(sec)
    return  '{0}{1}{2}{3}{4}{5}'.format(y,mon,d,h,min,s)

def long_lat_convert(*lat_long,high='300.05'):

    Dec_hex =''
    for dec in lat_long:
        Dec = float(dec)
        high = float(high)

        deg = int((Dec))
        Deg=hex(deg)[2:].zfill(2)
        Minutes = int((Dec - deg) * 60)
        Min=hex(Minutes)[2:].zfill(2)
        Seconds = int(str((Dec - deg) * 60).split('.')[1][:5])
        Sec=hex(Seconds)[2:].zfill(6)
        dec='01'+Deg+Min+Sec
        Dec_hex += dec

    h_int = int(high)
    h_deg = hex(h_int)[2:].zfill(4)
    h_float = int(str(high).split('.')[1])
    h_f= hex(h_float)[2:].zfill(2)
    h_hex = h_deg+h_f
    Dec_hex = Dec_hex + h_hex
    return  Dec_hex

def cell_convert(lac,ci):
    lac = int(lac)
    ci = int(ci)
    return hex(ci)[2:].zfill(8)+hex(lac)[2:].zfill(6)+'48'+'47'

def device_convert(phone_num,imsi):
    imsi = imsi.ljust(20,'0') 
    imsi_str = ''
    for s in imsi:
        if s.isalpha(): 
            imsi_str += hex(ord(s))[2:].zfill(2)
        else:
            imsi_str += hex(int(s))[2:].zfill(2)
    return  phone_num.zfill(16)+imsi_str

def mac_convert(mac):
    mac = mac.replace(":","")+'00' *2
    return mac +'00' * 8 * 7

def get_odd_even(data):
    sum = 0
    for index in range(14,len(data),2):
        sum += int(data[index:index+2],16)
    return hex(sum)[-2:].zfill(2)

def file_read(file):
    if os.path.exists(file) and os.path.getsize(file):
        f = open(file,"r")
        list_arr = []
        for line in f:
            if line.strip():
                list_arr.append(line)
        f.close()
        return  list_arr

def str_join(imsi_post_list):
    #long|lat|high|lac|ci|phone_num|imsi|mac
    l_str = []
    for msg_index in imsi_post_list:
        long = msg_index.strip().split("|")[0]
        # lg = round((float(long) + random.uniform(0.0001, 0.0010)), 6)
        # long_str = str(lg)
        lat = msg_index.strip().split("|")[1]
        # la = round((float(lat) + random.uniform(0.0001, 0.0010)), 6)
        # lat_str = str(la)
        h = msg_index.strip().split("|")[2]
        lac = msg_index.strip().split("|")[3]
        ci = msg_index.strip().split("|")[4]
        phone_num = msg_index.strip().split("|")[5]
        imsi = msg_index.strip().split("|")[6]
        macaddr = msg_index.strip().split("|")[7]

        date = get_date()
        long_lat_high = long_lat_convert(lat, long, high=h)
        lac_ci = cell_convert(lac,ci)
        device = device_convert(phone_num, imsi)
        mac = mac_convert(macaddr)


        suffix = msg_head + date + long_lat_high + status + gyro + lac_ci + \
            device + mac + other

        odd_even = get_odd_even(suffix)
        result = suffix + odd_even
        l_str.append(result)
    return  l_str

def dataSwitch(data):
    str1 = b''
    str2 = b''
    while data:
        str1 = data[0:2]
        s = int(str1,16)
        str2 += struct.pack('B',s)
        data = data[2:]
    return str2


def create_conn(ADDR,send_data):
    cli_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    reuse = True
    cli_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, reuse)
    # connect
    cli_socket.connect(ADDR)
    cli_socket.send(send_data)



if __name__ == "__main__":
    HOST = '192.168.2.223'
    PORT = 12212
    ADDR = (HOST,PORT)
    # long|lat|high|lac|ci|phone_num|imsi|mac
    msg_list = file_read("msg_chuandai.txt")
    concu = len(msg_list)
    data_list = str_join(msg_list)

    while True:
        print("msg send loop to start...")
        for data in data_list:
            send_data = dataSwitch(data)
            create_conn(ADDR,send_data)
            time.sleep(1)
        print("msg send loop finished...")
        time.sleep(5)


配置文件示例:

102.610006|26.326207|699|33251|33569|8613502111060|460018032738617|00:00:00:00:00:00
102.601343|26.342289|699|33251|33569|8613502111060|460018032738617|00:00:00:00:00:00
102.638794|26.311119|700|33251|35872|8613502111061|460018032738617|00:00:00:00:00:00


你可能感兴趣的:(LINUX技术,Python学习)