加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何从性能测试开始

发布时间:2020-12-17 02:02:09 所属栏目:百科 来源:网络整理
导读:我正在使用rails 3.1.1并使用socery 0.7.11进行身份验证,我需要实现性能测试,我可以测试以下内容 模拟100个用户同时登录和注销 模拟100个用户同时添加新兴趣. 我已经通过以下链接: http://rubylearning.com/blog/2011/08/14/performance-testing-rails-appl
我正在使用rails 3.1.1并使用socery 0.7.11进行身份验证,我需要实现性能测试,我可以测试以下内容

>模拟100个用户同时登录和注销
>模拟100个用户同时添加新兴趣.

我已经通过以下链接:

http://rubylearning.com/blog/2011/08/14/performance-testing-rails-applications-how-to/

http://guides.rubyonrails.org/performance_testing.html

并且能够做以下事情

>使用test_helper.rb创建测试文件夹
>创建了test / performance / login_test.rb
>创建fixtures / users.yml

test_helper.rb中

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment',__FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

/performance/login_test.rb

require 'test_helper'
require 'rails/performance_test_help'

class LoginTest < ActionDispatch::PerformanceTest

  fixtures :users
  # Refer to the documentation for all available options
   self.profile_options = { :runs => 5,:metrics => [:wall_time,:memory],:output => 'tmp/performance',:formats => [:flat] }

  def test_login
   post_via_redirect "http://bc.lvh.me/",:username => users(:youruser).username,:password => users(:youruser).password
#using lvh.me coz login page is on my subdomain
  end
end

灯具/ users.yml里

one:
  username: eagleschasar12
  password: chaitanyA1#

two:
  username: eaglesmarktell12
  password: chaitanyA1#

当我运行rake test:profile时,我得到了以下结果

E
===============================================================================
Error: test_login(LoginTest)
ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named password: INSERT INTO "users" ("username","password","created_at","updated_at","id") VALUES ('eagleschasar12','chaitanyA1#','2012-06-15 06:43:01',980190962)

所以我的问题是

>为什么测试命令触发插入查询?
>我是否需要在夹具文件中编写100条用户记录来测试功能?
>如何检查会话登录/注销?
>此外,当我从db中选择兴趣类型时,我的用户应该登录系统,那么我如何在性能测试中测试这种情况?
>此测试是否在webrick上运行,webrick是我的本地服务器.

解决方法

我会尽力回答你的问题.我相信其他人可能会为每个人提供更完整的答案,我们欢迎他们,但我希望能让你开始,这样你就可以知道要寻找什么:

1.为什么测试命令触发插入查询?

您的插入查询是从您的灯具运行的.我个人使用FactoryGirl,但逻辑是一样的.运行fixture时,它会将该对象插入到数据库中.这样你就可以做一些像fixture:user这样的东西,它会为你创建一个用户,而不必每次都提供所有的User对象细节.如果您的文件中有5个灯具,运行类似灯具的东西:用户将创建5个条目(如果有人可以确认这个灯具我会感激,因为我没有用户灯具).

2.我是否需要在夹具文件中写入100条用户记录来测试功能?

不,你没有.您可以使用Faker gem根据需要使用随机名称,字符串等自动创建任意数量的用户(或其他任何内容).文档为here.

3.如何检查会话登录/注销?

在创建会话时,您应该设置session [user_id]变量,以便进行测试.不用说,在注销(即会话销毁)时,您应该将其设置为nil.在任何情况下,当通过设置session [:user_id]变量创建会话时,您可以使用request.session_options [:id]访问会话ID.

4.此外,那么我如何在性能测试中测试这种情况呢?

在每次测试之前,您应该运行“test_log_in”函数,该函数登录您创建的测试用户.然后,需要登录用户的测试应该通过.您还应该在不登录测试用户的情况下创建测试,并且他们不应执行需要登录用户的更改(或不应显示页面).

5.这个测试是在我的本地服务器webrick上运行的吗?

如果那是你在Gemfile中安装的服务器,那么它将被使用.它适用于您安装的任何服务器.

您在一个问题中提出了很多问题,因此如果您需要更多关于答案的详细信息,我建议您在不同的帖子中询问每个问题,并详细说明您要查找的内容.这样你的答案就不会像这个那样广泛,但更具体.

希望我至少给你一个开始的好地方.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读