11

我正在尝试使用 apache bench 在我的 rails 应用程序中加载测试创建操作,但 ab 似乎没有发送 POST 数据 - 尽管它确实正确提交了 POST 而不是 GET 请求。

这是我运行的命令:

ab -n 1 -p post -v 4 "http://oz01.zappos.net/registrations"

这是发布文件的内容:

authenticity_token=M18KXwSOuIVbDPZOVQy5h8aSGoU159V9S5uV2lpsAI0

rails 日志显示了一个 POST 请求,但没有显示任何正在发布的参数:

Started POST "/registrations" for 10.66.210.70 at Thu Sep 09 17:48:06 -0700 2010
  Processing by RegistrationsController#create as */*
Rendered registrations/new.html.erb within layouts/application (14.0ms)
Completed 200 OK in 24ms (Views: 14.6ms | ActiveRecord: 0.1ms)

而来自浏览器的 POST 请求会导致以下日志条目:

Started POST "/registrations" for 192.168.66.20 at Thu Sep 09 17:49:47 -0700 2010
  Processing by RegistrationsController#create as HTML
  Parameters: {"submit"=>"true", "authenticity_token"=>"AfNG0UoTbJXnxke2725efhYAoi3ogddMC7Uqu5mAui0=", "utf8"=>"\342\234\223", "registration"=>{"city"=>"", "address"=>"", "name"=>"", "zip"=>"", "optin"=>"0", "state"=>"", "email"=>""}}
Rendered registrations/new.html.erb within layouts/application (13.7ms)
Completed 200 OK in 24ms (Views: 14.3ms | ActiveRecord: 0.1ms)

最后,这是 ab 为请求记录的内容:

---
POST /registrations HTTP/1.0
User-Agent: ApacheBench/2.0.40-dev
Host: oz01.zappos.net
Accept: */*
Content-length: 63
Content-type: text/plain


---

为什么它不接收帖子数据?

如果“post”文件不存在,那么我会收到一条错误消息,说它找不到文件,所以我至少知道它正在找到文件......

4

2 回答 2

11

也许您需要 man ab 中所述的 -T 选项:-

ab -n 1 -p post -v 4 -T application/x-www-form-urlencoded "http://oz01.zappos.net/registrations"

我用 Django 进行了测试,似乎 Django 并不真正关心内容类型标题(无论我是否使用 -T,它都会显示 POSTed 内容)但 Rails 可能想要它。

于 2011-01-24T08:02:34.433 回答
7

老问题,但为了其他搜索这个的人,这就是我如何让它工作的。

确保您的帖子文件经过正确的 URL 编码,最后没有额外的非打印字符或任何内容。最没有错误的方法就是用代码创建它。我用一些 python 来创建我的:

>>> import urllib
>>> outfile = open('post.data', 'w')
>>> params = ({ 'auth_token': 'somelongstringthatendswithanequalssign=' })
>>> encoded = urllib.urlencode(params)
>>> outfile.write(encoded)
>>> outfile.close()

示例输出:

auth_token=somelongstringthatendswithanequalssign%3D
于 2012-03-08T20:19:51.487 回答