访问次数
我的另一个总结性博客: todototry 大米粒

ab测试出现error: connection reset by peer的解决方案

我们在使用一些开源程序之前,可能会使用ab工具在服务器或者本地进行一次性能评估,但是很多时候却总是会以失败告终,因为,服务器会拒绝你的ab工具发出的http请求, 出现 error: connection reset by peer, 这该怎么办呢?

首先,为了测试一个具有sql操作的页面,通常需要登录,这时候需要伪装成一个用户来实现自动登录,简单的方法就是:在浏览器端登录一个用户,打开chrome的developer tool,将其中的cookie复制,用-H option来实现带cookie的http请求,其余的并发任务交给ab工具,使得每个http都携带session对应的cookie,如果这样就ok了,基本上说明这个web app安全性有待考量,这也是初级使用者可能忽略的问题。

 

其次,如果伪装成登录用户,携带了cookie依然出现这个错误,那么通常是由于web app源码中开启了csrf防护,针对伪装的请求做了过滤,这个问题也相对好解决,因为是开放源码,我们可以打开源码,找到这个web app的框架级的config文件,或者是 application级别的启动器类,就可以方便快速地找到这段程序,将csrf防护关闭就可以了。以下就以rails应用, 和  sails.js应用为例,讲述如何关闭csrf防护。

rails应用:代码在controller目录下的ApplicationController.rb文件,将其中的csrf保护函数protect_from_forgery中cookie.delete(auto_cookie_name)行注释掉。或者将protect_from_forgery注释掉,代码如下:

 
class ApplicationController < ActionController::Base
  include Redmine::I18n
  include Redmine::Pagination
  include RoutesHelper
  helper :routes
 
  class_attribute :accept_api_auth_actions
  class_attribute :accept_rss_auth_actions
  class_attribute :model_object
 
  layout 'base'
 
  protect_from_forgery
  def handle_unverified_request
    super
    #cookies.delete(autologin_cookie_name)
  end
 
  before_filter :session_expiration, :user_setup, :check_if_login_required, :set_localization
 
  rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token
  rescue_from ::Unauthorized, :with => :deny_access
  rescue_from ::ActionView::MissingTemplate, :with => :missing_template
 
  include Redmine::Search::Controller
  include Redmine::MenuManager::MenuController
  helper Redmine::MenuManager::MenuHelper
 
  def session_expiration
    if session[:user_id]
      if session_expired? && !try_to_autologin
        reset_session
        flash[:error] = l(:error_session_expired)
        redirect_to signin_url
      else
        #session[:atime] = Time.now.utc.to_i
      end
    end
  end

 

如果是sails.js web app, 那么就更加简单了,打开config目录下,有一个csrf文件,将csrf设置为false就可以了。

在关闭了csrf之后,基本上ab测试就不会再出现error: connection reset by peer 了。

 

如果你对手头没有源码的web app的性能测试感兴趣,可以参考我的其它文章。

 

参考:

检查丢包利器dropwatch: http://blog.yufeng.info/archives/2497

ulimit问题及其影响: http://blog.yufeng.info/archives/1380

 

posted @ 2015-04-20 18:58  fandyst  阅读(4387)  评论(0编辑  收藏  举报