cirandas.net

ref: master

test/integration/performance_test.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require_relative "../test_helper"
require 'benchmark'

class PerformanceTest < ActionDispatch::IntegrationTest

  all_fixtures

  should 'not have a linear increase in time to display a blog page' do
    person = create_profile('clueless')

    person0 = create_profile('person0')
    person1 = create_profile('person1')
    create_posts(person1, 50)
    person2 = create_profile('person2')
    create_posts(person2, 100)

    get '/person0/blog'
    get '/person1/blog'
    get '/person2/blog'

    # no posts
    time0 = (Benchmark.measure { 10.times { get '/person0/blog' } })

    # first 50
    time1 = (Benchmark.measure { 10.times { get '/person1/blog' } })

    # another 50
    time2 = (Benchmark.measure { 10.times { get '/person2/blog' } })

    # should not scale linearly, i.e. the inclination of the first segment must
    # be a lot higher than the one of the segment segment. To compensate for
    # small variations due to hardware and/or execution environment, we are
    # satisfied if the the inclination of the first segment is at least twice
    # the inclination of the second segment.
    a1 = (time1.total - time0.total)/50.0
    a2 = (time2.total - time1.total)/50.0
    assert a1 > a2*NON_LINEAR_FACTOR, "#{a1} should be larger than #{a2} by at least a factor of #{NON_LINEAR_FACTOR}"
  end
  protected

  def create_profile(name)
    person = create_user(name).person
    Blog.create(:name => "Blog", :profile => person)
    person
  end

  def create_posts(profile, n)
    postnumber = profile.articles.count
    blog = profile.blog
    n.times do |i|
      postnumber += 1
      TextArticle.create!(:profile => profile, :parent => blog, :name => "post number #{postnumber}")
    end
  end

end