golang测试TCP最大连接数

前端之家收集整理的这篇文章主要介绍了golang测试TCP最大连接数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

受限于本地端口数的分配,我想看看本地能起到多少个TCP client连接

本测试用例client和server都部署在同一台linux虚拟机上

查看进程打开的描述符数:102400(10万)

[root@bogon~]#ulimit-a
corefilesize(blocks,-c)0
datasegsize(kbytes,-d)unlimited
schedulingpriority(-e)0
filesize(blocks,-f)unlimited
pendingsignals(-i)15042
maxlockedmemory(kbytes,-l)64
maxmemorysize(kbytes,-m)unlimited
openfiles(-n)102400
pipesize(512bytes,-p)8
POSIXmessagequeues(bytes,-q)819200
real-timepriority(-r)0
stacksize(kbytes,-s)8192
cputime(seconds,-t)unlimited
maxuserprocesses(-u)15042
virtualmemory(kbytes,-v)unlimited
filelocks(-x)unlimited

再看看测试代码,client端:

packagemain

import(
	_"bytes"
	"fmt"
	"net"
	"net/url"
	_"os"
	"strconv"
	"time"
)

var(
	DefalutTimeout=25*time.Second
	MaxClient=70000
	clientNum=0
	msg_nums=0
)

funcsockConn(daemonstring,timeouttime.Duration)(net.Conn,error){
	daemonURL,err:=url.Parse(daemon)
	//fmt.Printf("daemonurl%v%v\n",daemonURL,daemonURL.Scheme)
	iferr!=nil{
		returnnil,fmt.Errorf("couldnotparseurl%q:%v",daemon,err)
	}

	varcnet.Conn
	switchdaemonURL.Scheme{
	case"unix":
		returnnet.DialTimeout(daemonURL.Scheme,daemonURL.Path,timeout)
	case"tcp":
		returnnet.DialTimeout(daemonURL.Scheme,daemonURL.Host,timeout)
	default:
		returnc,fmt.Errorf("unknownscheme%v(%s)",daemonURL.Scheme,daemon)
	}
}

funcsendData(socketnet.Conn,nint,msg_num_chanchanint){
	buf:=make([]byte,10)
	for{
		_,err:=socket.Write([]byte(strconv.Itoa(n)))
		iferr!=nil{
			fmt.Printf("Errorreading:%s\n",err.Error())
			clientNum--
			return
		}
		//sendreply
		_,err=socket.Read(buf)
		fmt.Printf("client%v\n",string(buf))
		iferr!=nil{
			fmt.Printf("Errorsendreply:%s\n",err.Error())
			clientNum--
			return
		}
		//time.Sleep(1*time.Second)
		msg_num_chan<-1
		buf=buf[0:]
	}
}

funcconnectServer(msg_num_chanchanint){
	fori:=1;i<=MaxClient;i++{
		socket,err:=sockConn("tcp://127.0.0.1:8088",DefalutTimeout)
		iferr!=nil{
			fmt.Printf("connecterror:%s\n",err)
		}else{
			clientNum++
			gosendData(socket,i,msg_num_chan)
		}

	}
}

funcmain(){

	msg_num_chan:=make(chanint)
	connectServer(msg_num_chan)

	gofunc(){
		formsg_num:=rangemsg_num_chan{
			msg_nums+=msg_num
		}
	}()

	time.Sleep(10*time.Second)
	fmt.Printf("recmsg%d\n",msg_nums)
}

Server端:

packagemain

import(
	"fmt"
	"net"
	"os"
	"time"
)

const(
	MAX_CONN_NUM=100000
)

funcEchoFunc(connnet.Conn){
	deferconn.Close()
	buf:=make([]byte,err:=conn.Read(buf)
		iferr!=nil{
			//println("Errorreading:",err.Error())
			return
		}
		//fmt.Printf("server%s\n",string(buf))
		//sendreply
		_,err=conn.Write(buf)
		iferr!=nil{
			//println("Errorsendreply:",err.Error())
			return
		}
	}
}

funcmain(){
	listener,err:=net.Listen("tcp","127.0.0.1:8088")
	iferr!=nil{
		fmt.Println("errorlistening:",err.Error())
		os.Exit(1)
	}
	deferlistener.Close()

	fmt.Printf("running...\n")

	varcur_conn_numint=0
	conn_chan:=make(channet.Conn)
	ch_conn_change:=make(chanint)

	gofunc(){
		forconn_change:=rangech_conn_change{
			cur_conn_num+=conn_change
		}
	}()

	gofunc(){
		for_=rangetime.Tick(5e9){
			fmt.Printf("curconnnum:%d\n",cur_conn_num)
		}
	}()

	fori:=0;i<MAX_CONN_NUM;i++{
		gofunc(){
			forconn:=rangeconn_chan{
				ch_conn_change<-1
				EchoFunc(conn)
				ch_conn_change<--1
			}
		}()
	}

	for{
		conn,err:=listener.Accept()
		iferr!=nil{
			println("Erroraccept:",err.Error())
			return
		}
		conn_chan<-conn
	}
}

最后看看server端执行结果:

[root@bogonmpro]#gorunserver.go
running...
curconnnum:0
curconnnum:17452
curconnnum:28233
curconnnum:28233
curconnnum:28233
^C^Cexitstatus2

client端执行结果:

connecterror:dialtcp127.0.0.1:8088:cannotassignrequestedaddress
connecterror:dialtcp127.0.0.1:8088:cannotassignrequestedaddress
connecterror:dialtcp127.0.0.1:8088:cannotassignrequestedaddress
connecterror:dialtcp127.0.0.1:8088:cannotassignrequestedaddress
connecterror:dialtcp127.0.0.1:8088:cannotassignrequestedaddress
connecterror:dialtcp127.0.0.1:8088:cannotassignrequestedaddress

只能起到2万8个连接,最主要受限于端口数

本例只是测试client端的连接数,不是测试server的连接数

原文链接:https://www.f2er.com/go/190136.html

猜你在找的Go相关文章