Windows压力测试TCP优化篇(搭建环境+Socket脚本)

java.net.BindException: Address already in use 相信这个错在win上进行压力测试的小伙伴们一定都看到过吧~那么为何会出现这样的错误呢?如何避免呢?让我们来瞧瞧~压力测试之前,我们一定要知道http是网络上层协议,底层还是Socket。Socket可以支持不同的传输层协议(TCP或UDP),当使用TCP协议进行连接时,该Socket连接就是一个TCP连接,那么如何确定一个TCP链接呢?嘻嘻~那就是:源IP+源端口+目标IP+目标端口,由此可见一次http请求是需要占用一个端口,那么就让我们实践一下~

  • 首先我们先来搭一个供我们测试的环境~  咋们先用flask编写一个简单的接口~:
    @app.route('/time',methods=['get'])
    def get_time():
        now = str(datetime.datetime.now()) 
        ip1 = request.headers.get('X-Real-IP')
        ip2 = request.headers.get('X-Forwarded-For')
        return "当前的时间是:%s ,你的ip是:%s,你的ip组是:%s   "%(now,str(ip1),str(ip2))
  • 然后咋们使用nginx作为httpserver ,uwsgi 作为webserver,gevent异步io搭建一个环境,uwsgi 配置如下:
    [uwsgi]
    http = 0.0.0.0:8081 
    wsgi-file =/www/flask_app/flask_test.py
    callable = app 
    listen=1024
    processes = 6 
    threads = 20 
    pidfile = /www/flask_app/uwsgi.pid
    daemonize = /www/flask_app/mysite.log
    buffer-size = 32678 
    max-requests = 10000
    master = true
    gevent = 100        
    

    并在原来的代码上引入异步猴子补丁~(至于猴子补丁能为代码提升多大作用那就没测试啦~感兴趣的可以自行测试一下呀)

    import gevent.monkey
    gevent.monkey.patch_all()  # 可选内容,是否加载猴子补丁
  • 考虑到并发较大,最后我们用nginx 实现多端口的负载均衡,部分设置内容如下:
    upstream mycom{
    	least_conn;
        server 0.0.0.0:8081;
        server 0.0.0.0:8082;    
    }
        
    server
        {
            listen 60;
            #server_name 127.0.0.1;  
            location / {   
                include  uwsgi_params;
                proxy_pass  http://mycom; #连接负载均衡服务器
                uwsgi_param UWSGI_CHDIR /www/flask_app;
    			uwsgi_param UWSGI_SCRIPT flask_test:app;
                proxy_set_header X-Real-IP $remote_addr;
        		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        		proxy_set_header Host $http_host;
        		proxy_set_header X-NginX-Proxy true;
            }
  • 补充端口为8081,暂且称为1号机的代码,让负载均衡肉眼看的见~
    @app.route('/time',methods=['get'])
    def get_time():
        now = str(datetime.datetime.now()) #把当前时间转换成字符串
        #n = request.headers
        ip1 = request.headers.get('X-Real-IP')
        ip2 = request.headers.get('X-Forwarded-For')
        return "1号机:当前的时间是:%s ,你的ip是:%s,你的ip组是:%s   "%(now,str(ip1),str(ip2))
  • 同理~新建端口为8082,暂且称为2号机的代码,这样我们就可以区分啦~
    @app.route('/time',methods=['get'])
    def get_time():
        now = str(datetime.datetime.now()) #把当前时间转换成字符串
        #n = request.headers
        ip1 = request.headers.get('X-Real-IP')
        ip2 = request.headers.get('X-Forwarded-For')
        return "2号机:当前的时间是:%s ,你的ip是:%s,你的ip组是:%s   "%(now,str(ip1),str(ip2))

    二号机uwsgi配置如下:

    [uwsgi]
    http = 0.0.0.0:8082
    wsgi-file =/www/flask_app1/flask_test.py
    #plugin=python
    callable = app 
    listen=1024
    processes = 6 
    threads = 20 
    pidfile = /www/flask_app1/uwsgi.pid
    daemonize = /www/flask_app1/mysite.log
    buffer-size = 32678 
    max-requests = 10000
    master = true
    gevent = 100        
  • 哈哈这样配置代码已经都准备好啦~打开服务器远程链接cd到项目路径下执行(分别到1,2号机的路径下):
    uwsgi --ini uwsgi.ini

    执行成功后我们输入http://ayoc.top:60/time,多刷新几次瞧瞧看~哈哈成功咯~到这里我们的环境也就搭好啦~

  • 接下来我们用win默认环境进行测试~ 打开jmeter,并发400,forever,并设置遇到error 自动停止,执行~ 大概请求了16000左右时,报错如图:

    此时使用netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’查询服务器TCP链接,发现time_wait仅为692,总端口占用一千不到~排除服务器端口问题~

    哈哈因为再次之前我在服务器/etc/sysctl.conf中添加了如下配置信息,配置原文转自:点我~

    # 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用Cookie来处理,可防范少量的SYN攻击。该参数默认为0,表示关闭。
    net.ipv4.tcp_syncookies=1
    
    # 表示开启重用,即允许将TIME-WAIT套接字重新用于新的TCP连接。该参数默认为0,表示关闭。
    net.ipv4.tcp_tw_reuse=1
    
    # 表示开启TCP连接中TIME-WAIT套接字的快速回收,该参数默认为0,表示关闭。
    net.ipv4.tcp_tw_recycle=1
    
    # 表示如果套接字由本端要求关闭,那么这个参数将决定它保持在FIN-WAIT-2状态的时间。
    net.ipv4.tcp_fin_timeout=30
    
    # 表示当Keepalived启用时,TCP发送Keepalived消息的频度改为20分钟,默认值是2小时。
    net.ipv4.tcp_keepalive_time=1200
    
    # 表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息,默认值为180 000,此处改为5000。对于Apache、Nginx等服务器,前面介绍的几个参数已经可以很好地减少TIME_WAIT套接字的数量,但是对于Squid来说,效果却不大,有了此参数就可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。
    net.ipv4.tcp_max_tw_buckets=5000
  • 那么我们接着来排查win的端口问题,为了更明显的看到win端口使用信息,我们使用python 编写一个socket~
  • 服务端为:
    import socket
    import threading
    import sys
    
    port_list=[] # 存放使用过的地址
    
    def service():
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind(('127.0.0.1', 5555))
            s.listen(1000)
        except socket.error as msg:
            print(msg)
            sys.exit(1)
        print('Waiting connection...')
        while True:
            conn, addr = s.accept()
            t = threading.Thread(target=data, args=(conn, addr))
            t.start()
    
    def data(conn, addr):
        port_list.append(addr)
        print('新的连接来自{0}'.format(addr))
        conn.send(('Hi~').encode())
        while 1:
            data = conn.recv(1024)
            if data == 'exit' or not data:
                conn.send(('Connection closed!').encode())
                break
            conn.send(bytes('Hello, {0}'.format(data), "UTF-8"))  # TypeError: a bytes-like object is required, not 'str'
            setPort = len(list(set(port_list)))
            print(f'到此为止去重后的占用端口为{setPort}个')
        conn.close()
  • 客户端为:
    import socket
    import sys
    from socketTest import port_list
    
    def client(i):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('127.0.0.1', 5555))
        except socket.error as msg:
            print(msg)
            sys.exit(1)
        print(s.recv(1024))  # 目的在于接受:Accept new connection from (...
        data = (f'hi+{i}').encode()
        s.send(data)
        print('aa', s.recv(1024))
        s.close()
    
    
    if __name__ == '__main__':
        for i in range(65000):
            client(i)
        print(set(port_list))
        setPort = len(list(set(port_list)))
        print(f'此次占用端口{setPort}个')
    

    在win默认配置下启动服务端与客户端~执行15秒后~console显示:

    到此为止去重后的占用端口为16343个
    新的连接来自('127.0.0.1', 65531)
    到此为止去重后的占用端口为16343个
    新的连接来自('127.0.0.1', 65532)
    到此为止去重后的占用端口为16343个
    新的连接来自('127.0.0.1', 65533)
    到此为止去重后的占用端口为16343个
    新的连接来自('127.0.0.1', 65534)
    到此为止去重后的占用端口为16343个
    新的连接来自('127.0.0.1', 49152)
    到此为止去重后的占用端口为16343个
    新的连接来自('127.0.0.1', 49153)
    到此为止去重后的占用端口为16343个
    新的连接来自('127.0.0.1', 49154)

    由此可见socket绑定的端口是按照顺序分配的,且在win默认配置下只能用16343左右个端口,经查询端口49153~65535 为动态和/或私有端口。由此得出结论在默认配置下(win默认释放tcp端口为240s),单台win并发最大为每秒66个请求。

  • 接下来我们win+R打开运行 ,输入regedit,并进入[HKEY_LOCAL_MACHINE \System \CurrentControlSet \Services \Tcpip \Parameters] 下,新建如下配置DWORD(10进制),配置详情:点我~站外链接~
    TCPWindowSize   62420
    TcpTimedWaitDelay  30
    MaxUserPort  65534
  • 我们将客户机的测试脚本改为range(70000),为了让他循环使用端口,测试端口可用最大值,接着继续执行测试脚本,最后结果为70000个socket ,用了64256个端口(总0~65535)哈哈哈优化完成~ 所以用win作为施压机时切记一定要改注册表哟~  (测试接口已经被关闭啦~调皮的人不要来压我了!!!)
    新的连接来自('127.0.0.1', 34129)
    到此为止去重后的占用端口为64256个
    新的连接来自('127.0.0.1', 34130)
    到此为止去重后的占用端口为64256个
    新的连接来自('127.0.0.1', 34131)
    到此为止去重后的占用端口为64256个
  •  每日一题~会输出什么呢~嘻嘻
    import copy
    a = [1, 2, 3, 4, ['a', 'b']]
    b = a
    c = copy.copy(a)
    d = copy.deepcopy(a)
    a.append(5)
    a[4].append('c') 
    print(a)
    print(b)
    print(c)
    print(d)
    

     

13,718 次浏览

“Windows压力测试TCP优化篇(搭建环境+Socket脚本)”的2,002个回复

  1. [1, 2, 3, 4, [‘a’, ‘b’, ‘c’], 5]
    [1, 2, 3, 4, [‘a’, ‘b’, ‘c’], 5]
    [1, 2, 3, 4, [‘a’, ‘b’, ‘c’], 5]
    [1, 2, 3, 4, [‘a’, ‘b’]]

  2. Absolutely NEW update of captcha solution package “XRumer 19.0 + XEvil”:
    captchas solution of Google (ReCaptcha-2 and ReCaptcha-3), Facebook, BitFinex, Bing, Hotmail, SolveMedia, Yandex,
    and more than 12000 another subtypes of captchas,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can use XEvil 4.0 with any most popular SEO/SMM software: iMacros, XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

    Interested? You can find a lot of impessive videos about XEvil in YouTube.

    FREE DEMO AVAILABLE!

    See you later 😉

    http://XEvil.net

  3. Appreciating the dedication you put into your site and in depth information you provide.

    It’s nice to come across a blog every once in a while that isn’t the same outdated rehashed
    information. Wonderful read! I’ve saved your site and I’m including your RSS feeds to my Google account.

    P.S. If you have a minute, would love your feedback on my new website
    re-design. You can find it by searching for «royal cbd» — no sweat
    if you can’t.

    Keep up the good work!

  4. My brother suggested I would possibly like this
    website. He was once totally right. This submit actually
    made my day. You can not consider just how much time I had spent for this info!
    Thanks!

  5. I’m really impressed with your writing skills and also with the layout on your blog.
    Is this a paid theme or did you customize it yourself?

    Anyway keep up the excellent quality writing, it’s rare to see a nice blog like this
    one nowadays.

  6. Excellent post. I was checking constantly this blog and I’m impressed!
    Very helpful info specifically the last part 🙂 I care for such info a lot.
    I was seeking this particular information for a long time.

    Thank you and good luck.

  7. I feel that is among the most significant info for me.
    And i’m satisfied studying your article. But wanna statement
    on few general things, The website taste is perfect, the
    articles is really excellent : D. Good process, cheers

  8. This is the perfect website for everyone who really wants to understand
    this topic. You realize a whole lot its almost tough to argue with you
    (not that I actually will need to…HaHa). You definitely
    put a brand new spin on a topic which has been discussed for a long
    time. Wonderful stuff, just excellent!

  9. Fantastic items from you, man. I have be mindful your stuff
    prior to and you’re just extremely fantastic. I actually like what you have got right here, certainly like what you’re stating and the way in which you assert it.
    You’re making it enjoyable and you still take care of
    to stay it sensible. I can not wait to learn far more from you.
    That is really a terrific site.

  10. Hi there just wanted to give you a brief heads up and let you know a few of the images aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different web browsers and both show the same results.

  11. Знаете ли вы?
    Возможно, что американцы уже в 1872 году вмешались в канадские выборы.
    Ливийский диктатор пытался спасти от казни премьер-министра Пакистана.
    Врач на карантине спел созданную для фильма песню Высоцкого «Давно смолкли залпы орудий».
    Первый в мире короткоствольный револьвер (англ.)русск. с откидным барабаном стал символом кинонуара.
    Рассказ английского писателя был экранизирован в СССР раньше, чем опубликован его английский оригинал.

    arbeca

  12. I apologize for off-topic, I’m thinking about building an enlightening internet site for individuals. May probably start with posting interesting information just like”Grapes explode when you put them in the microwave. Go on, try it then”Please let me know if you know where I can find some related facts like here

    bahsegel 172

  13. Wonderful items from you, man. I have be aware your stuff previous to and you’re
    simply extremely great. I really like what you’ve got here, really like what you are saying and the
    way in which by which you are saying it. You’re making
    it entertaining and you continue to care for to keep it smart.

    I can not wait to read far more from you. That is actually a tremendous web site.

  14. It’s appropriate time to make some plans for the future and it’s time to
    be happy. I’ve read this post and if I could I wish to suggest you some interesting things or advice.
    Perhaps you can write next articles referring to this article.

    I desire to read more things about it!

  15. Great post. I was checking constantly this weblog and I am impressed!
    Very helpful information specially the closing part 🙂 I handle such information much.
    I used to be looking for this particular information for a long time.
    Thanks and best of luck.

  16. This is the right web site for anybody who wants to find out about this
    topic. You realize a whole lot its almost tough to argue with you (not
    that I actually will need to…HaHa). You
    definitely put a fresh spin on a topic which has been written about for a long time.
    Excellent stuff, just excellent!

  17. I am extremely impressed with your writing skills as well as with the layout on your blog.
    Is this a paid theme or did you customize it yourself?
    Either way keep up the nice quality writing, it is
    rare to see a nice blog like this one these days.

  18. I like to get my inspiration from really meaningful phrases said by truly great people like “Good and evil, reward and punishment, are the only motives to a rational creature”, do you know where I can find thematic compiltaions of those?

  19. Hello, There’s no doubt that your website may be having web
    browser compatibility problems. Whenever I take a look at
    your site in Safari, it looks fine however, if opening in IE, it’s got some overlapping issues.

    I just wanted to provide you with a quick heads up!
    Other than that, great site!

  20. Commonly, it was previously empiric that required malar only most qualified rank to buy cialis online reviews in wider fluctuations, but strange sortie symptoms that uncountable youngРІ One is an frenzied Reaction Harding ED mobilization; I purple this workings last wishes as most you to build compensate fresh whatРІs insideРІ Lems For the benefit of ED While Are Digital To Lymphocyte Shagging Acuity And Tonsillar Hypertrophy. discount viagra cheapest viagra

  21. I’ve been surfing online more than 3 hours nowadays, yet I never found any fascinating article like yours.

    It is lovely price sufficient for me. In my opinion, if all website owners and bloggers made excellent content as you did, the internet
    will probably be much more helpful than ever before.

  22. I’m amazed, I must say. Seldom do I come across a blog that’s both educative and engaging, and let
    me tell you, you’ve hit the nail on the head. The problem is something which too few folks are speaking
    intelligently about. I’m very happy that
    I found this during my search for something concerning this.

  23. hi!,I like your writing very so much! proportion we be in contact extra about your post on AOL?

    I require an expert on this space to resolve
    my problem. May be that’s you! Taking a look forward to peer you.

  24. I’m really impressed with your writing skills and also with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?
    Anyway keep up the nice quality writing, it is rare to see a nice
    blog like this one nowadays. cheap flights 31muvXS

  25. We’re a bunch of volunteers and starting a brand new scheme in our community.
    Your website provided us with helpful information to work on. You have performed
    an impressive task and our whole community shall be grateful to
    you. cheap flights 34pIoq5

  26. I do not know if it’s just me or if perhaps everybody else experiencing issues with your website.
    It seems like some of the written text in your posts are running off the screen. Can someone else please comment and let me know
    if this is happening to them too? This could be a problem with my web browser because
    I’ve had this happen before. Kudos

  27. It’s the best time to make a few plans for the longer term and it is time
    to be happy. I have learn this submit and if I may I desire to suggest you some interesting issues or suggestions.
    Perhaps you could write subsequent articles regarding this article.
    I wish to learn more things about it!

  28. Pingback: viagra for sale
  29. Tactile stimulation Tool nasal Regurgitation Asymptomatic testing GP Chemical harm Might Assist gadget I Rem Behavior Diagnosis Hypertension Top brass Nutrition General Remedial programme Other Inhibitors Autoantibodies firstly subsidize Healing Other side Blocking Anticonvulsant Group therapy less. cialis tadalafil Frpbss qpzzkn

  30. Pingback: viagra prescription
  31. Hey! This is my 1st comment here so I just wanted to give
    a quick shout out and tell you I genuinely enjoy reading your articles.
    Can you suggest any other blogs/websites/forums that
    go over the same subjects? Thank you so much!

  32. I will right away seize your rss feed as I can not find your e-mail subscription link or newsletter service.

    Do you’ve any? Kindly permit me realize in order that I may just subscribe.
    Thanks.

  33. Pingback: buy viagra online usa
  34. Greetings! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in exchanging links
    or maybe guest writing a blog article or vice-versa?
    My site addresses a lot of the same topics as yours and I think we could greatly
    benefit from each other. If you happen to be interested
    feel free to send me an e-mail. I look forward to hearing
    from you! Excellent blog by the way!

  35. Pingback: sildenafil