您的位置:首页 > 理论基础 > 计算机网络

搭建TCP服务器环境和jmeter测试TCP协议

2016-03-20 10:10 796 查看
搭建服务器环境

演示的是linux服务器上搭建环境,window环境一样

1)使用python写好服务器端程序,程序是支持多线程并发的。单线程就不需要jmeter去进行性能测试了。

代码引用:

http://www.cnblogs.com/hazir/p/python_socket_programming.html

import socket
import sys
from thread import *

HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
#Sending message to connected client
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string

#infinite loop so that function do not terminate and thread do not end.
while True:

#Receiving from client
data = conn.recv(1024)
reply = 'OK...' + data
if not data:
break

conn.sendall(reply)
conn.close()
#came out of loop
conn.close()

#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])

#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))

s.close()


运行程序

[root@localhost Desktop]# python threadServer.py
Socket created
Socket bind complete
Socket now listening


使用telnet测试连接

[root@localhost dev]# telnet 192.168.1.107 8888
Trying 192.168.1.107...
Connected to 192.168.1.107 (192.168.1.107).
Escape character is '^]'.
Welcome to the server. Type something and hit enter
nihao
OK...nihao
Connection closed by foreign host.


至此tcp服务器环境已经搭建好了

使用jmeter编写简单脚本进行测试



运行结果

正常结果



这里出现两种异常情况,一种是没有收到返回结果



一种是 Software caused connection abort



出现问题的原因还不清楚,后面分析

增加并发进行压测的时候能够明显看到服务器CPU上升

到这,jmeter测试TCP协议基本过程讲完,重要的还是后面的问题分析和定位。

中途遇到的问题:本地使用telnet能够发送请求,使用jmeter却提示unknownhost 。这是我本地jmeter的问题,重启下jmeter或者重新打开jmeter自带的tcpsampler脚本,修改一下。就可以运行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  socket jmeter 性能测试