【问题标题】:How to start with performance testing如何开始性能测试
【发布时间】:2012-06-15 10:17:55
【问题描述】:

我正在使用 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

fixtures/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', '2012-06-15 06:43:01', 980190962)

所以我的问题是

  • 为什么要测试命令触发插入查询?
  • 我需要在fixture文件中写入100条用户记录来测试功能吗?
  • 如何查看会话登录/注销?
  • 另外,当我从 db 中选择兴趣类型时,我的用户应该登录系统,那么如何在性能测试中测试这个场景?
  • 此测试是否在我的本地服务器 webrick 上运行。

【问题讨论】:

    标签: ruby-on-rails performance-testing


    【解决方案1】:

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


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

    您的插入查询是从您的固定装置运行的。我个人使用FactoryGirl,但逻辑是一样的。当你运行你的fixture时,它会将该对象插入到你的数据库中。这样您就可以执行fixture :user 之类的操作,它会为您创建一个用户,而无需每次都提供所有用户对象的详细信息。如果您的文件中有 5 个固定装置,则运行 fixture :users 之类的内容将创建 5 个条目(如果有人可以为固定装置确认这一点,我将不胜感激,因为我不使用固定装置)。


    2.我需要在fixture文件中写入100条用户记录来测试功能吗?

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


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

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


    4.另外,当我从 db 中选择兴趣类型时,我的用户应该登录系统,那么如何在性能测试中测试这个场景?

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


    5.此测试是否在我的本地服务器 webrick 上运行?

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


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

    希望我至少给了你一个好的起点。

    【讨论】:

    • 是的,你是对的,下次我会处理这个(关于问一个问题),非常感谢你给我一个方向
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2010-09-07
    • 1970-01-01
    • 2016-07-27
    • 2010-11-20
    • 2015-08-26
    • 2020-07-14
    相关资源
    最近更新 更多