Class: LoadRunner::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/load_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Queue



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/load_runner.rb', line 8

def initialize(logger = nil)

  # logger
  if logger!=nil
    @logger = logger
  end

  # setup
  @threads = []
  @transactions = 0
  @num_threads = 1
  @failures = 0
end

Instance Attribute Details

#failuresObject

Returns the value of attribute failures.



6
7
8
# File 'lib/load_runner.rb', line 6

def failures
  @failures
end

Instance Method Details

#load(num_threads = 1, &work) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/load_runner.rb', line 22

def load(num_threads=1, &work)
  @num_threads = num_threads
  @work = work  # save work for new threads

  # create threads
  num_threads.times do ||

    # do work
    create_thread

    # log
    if @logger!=nil
        @logger.debug "action=create|name=thread"
      end
    end
end

#runObject



39
40
41
42
43
# File 'lib/load_runner.rb', line 39

def run()
  @start_time = Time.now
  start_all
  wait_until_finished
end

#run_and_stagger(max_sleep = 10) ⇒ Object



45
46
47
48
49
# File 'lib/load_runner.rb', line 45

def run_and_stagger(max_sleep=10)
  @start_time = Time.now
  start_all { rand_sleep(max_sleep) }
  wait_until_finished
end

#run_for_duration(num_seconds, max_sleep = 0) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/load_runner.rb', line 51

def run_for_duration(num_seconds, max_sleep=0)
  @start_time = Time.now 

  # keep them running
  to_time = Time.now + num_seconds

  # log
  if @logger!=nil
    @logger.debug "action=run_for_durration|until=#{to_time}"
  end

  # duration
  while (Time.now <= to_time)
    @threads.each do |t| 

      # restart threads
      if (t.status=="sleep")
        rand_sleep(max_sleep)
        t.run
      end  

      if (t.status==false or t.status == nil)
        @transactions += 1
        @threads.delete t
        thread = create_thread
        rand_sleep(max_sleep) 
        thread.run

        # log
        if @logger!=nil
          @logger.debug "thread_count=#{@threads.count}"
        end
      end
    end
  end
  wait_until_finished()
end