| 14 | | class DrbSmokeTest |
|---|
| 15 | | def initialize(id) |
|---|
| 16 | | @id = id |
|---|
| 17 | | end |
|---|
| 18 | | def self.count_connections |
|---|
| 19 | | res = Content.connection.execute("show status where variable_name = 'Threads_connected'") |
|---|
| 20 | | if res |
|---|
| 21 | | res.fetch_row.last |
|---|
| 22 | | else |
|---|
| 23 | | "error getting connection count" |
|---|
| | 12 | RECORDS_PER_PROCESS = 1000 |
|---|
| | 13 | NUM_PROCESSES = 10 # should be an even number |
|---|
| | 14 | NUM_RECORDS_PER_LOGENTRY = 10 |
|---|
| | 15 | NUM_DOCS = 100 |
|---|
| | 16 | NUM_TERMS = 1000 |
|---|
| | 17 | |
|---|
| | 18 | TIME_FACTOR = 1000.to_f / NUM_RECORDS_PER_LOGENTRY |
|---|
| | 19 | |
|---|
| | 20 | class Words |
|---|
| | 21 | DICTIONARY = '/usr/share/dict/words' |
|---|
| | 22 | def initialize |
|---|
| | 23 | @words = [] |
|---|
| | 24 | File.open(DICTIONARY) do |file| |
|---|
| | 25 | file.each_line do |word| |
|---|
| | 26 | @words << word.strip unless word =~ /'/ |
|---|
| | 27 | end |
|---|
| | 28 | end |
|---|
| | 29 | end |
|---|
| | 30 | |
|---|
| | 31 | def to_s |
|---|
| | 32 | "#{@words.size} words" |
|---|
| | 33 | end |
|---|
| | 34 | |
|---|
| | 35 | def random_word |
|---|
| | 36 | @words[rand(@words.size)] |
|---|
| 26 | | def run |
|---|
| 27 | | @t1 = Time.now |
|---|
| 28 | | RECORDS_PER_PROCESS.times do |i| |
|---|
| 29 | | Content.create! :title => "process #{@id}", :description => "record #{i}\n#{'Lorem ipsum. ' * 100 }" |
|---|
| 30 | | if i % NUM_RECORDS_PER_LOGENTRY == 0 |
|---|
| 31 | | time = Time.now - @t1 |
|---|
| 32 | | puts "#{@id}: #{i} records indexed, last 100 in #{time}" |
|---|
| 33 | | Stats.create! :process_id => @id, :records => NUM_RECORDS_PER_LOGENTRY, :processing_time => time, :open_connections => self.class.count_connections |
|---|
| | 39 | |
|---|
| | 40 | puts "compiling sample documents..." |
|---|
| | 41 | WORDS = Words.new |
|---|
| | 42 | puts WORDS |
|---|
| | 43 | DOCUMENTS = [] |
|---|
| | 44 | |
|---|
| | 45 | NUM_DOCS.times do |
|---|
| | 46 | doc = '' |
|---|
| | 47 | NUM_TERMS.times { doc << WORDS.random_word << ' ' } |
|---|
| | 48 | DOCUMENTS << doc |
|---|
| | 49 | end |
|---|
| | 50 | |
|---|
| | 51 | def self.random_document |
|---|
| | 52 | DOCUMENTS[rand(DOCUMENTS.size)] |
|---|
| | 53 | end |
|---|
| | 54 | |
|---|
| | 55 | puts "built #{NUM_DOCS} documents with an avg. size of #{DOCUMENTS.join.size / NUM_DOCS} Byte." |
|---|
| | 56 | |
|---|
| | 57 | class Monitor |
|---|
| | 58 | class << self |
|---|
| | 59 | def count_connections |
|---|
| | 60 | res = Content.connection.execute("show status where variable_name = 'Threads_connected'") |
|---|
| | 61 | if res |
|---|
| | 62 | res.fetch_row.last |
|---|
| | 63 | else |
|---|
| | 64 | "error getting connection count" |
|---|
| | 65 | end |
|---|
| | 66 | end |
|---|
| | 67 | def running? |
|---|
| | 68 | Stats.count_by_sql("select count(*) from stats where kind='finished'") < (NUM_PROCESSES/2) |
|---|
| | 69 | end |
|---|
| | 70 | end |
|---|
| | 71 | end |
|---|
| | 72 | |
|---|
| | 73 | class TestBase |
|---|
| | 74 | def initialize(id) |
|---|
| | 75 | @id = id |
|---|
| | 76 | @t1 = Time.now |
|---|
| | 77 | end |
|---|
| | 78 | |
|---|
| | 79 | def get_time |
|---|
| | 80 | returning(Time.now - @t1) do |
|---|
| | 82 | end |
|---|
| | 83 | end |
|---|
| | 84 | |
|---|
| | 85 | end |
|---|
| | 86 | |
|---|
| | 87 | class Writer < TestBase |
|---|
| | 88 | def run |
|---|
| | 89 | RECORDS_PER_PROCESS.times do |i| |
|---|
| | 90 | Content.create! :title => "record #{@id} / #{i}", :description => DrbSmokeTest::random_document |
|---|
| | 91 | if i % NUM_RECORDS_PER_LOGENTRY == 0 |
|---|
| | 92 | # write stats |
|---|
| | 93 | time = get_time |
|---|
| | 94 | puts "#{@id}: #{i} records indexed, last #{NUM_RECORDS_PER_LOGENTRY} in #{time}" |
|---|
| | 95 | Stats.create! :process_id => @id, :kind => 'write', :info => i, |
|---|
| | 96 | :processing_time => time * TIME_FACTOR, |
|---|
| | 97 | :open_connections => Monitor::count_connections |
|---|
| | 98 | end |
|---|
| | 99 | end |
|---|
| | 100 | Stats.create! :process_id => @id, :kind => 'finished' |
|---|
| | 101 | end |
|---|
| | 102 | end |
|---|
| | 103 | |
|---|
| | 104 | class Searcher < TestBase |
|---|
| | 105 | def run |
|---|
| | 106 | while Monitor::running? |
|---|
| | 107 | t = Time.now |
|---|
| | 108 | result = Content.find_with_ferret 'findme', :lazy => true |
|---|
| | 109 | time = Time.now - t |
|---|
| | 110 | Stats.create! :process_id => @id, :kind => 'search', :info => "total_hits: #{result.total_hits} ; results: #{result.size}", |
|---|
| | 111 | :processing_time => time * 1000, |
|---|
| | 112 | :open_connections => Monitor::count_connections |
|---|
| | 113 | sleep 1 |
|---|
| | 114 | end |
|---|
| | 115 | end |
|---|
| | 116 | |
|---|
| | 117 | end |
|---|
| | 118 | |
|---|
| | 119 | def self.run |
|---|
| | 120 | @start = Time.now |
|---|
| | 121 | |
|---|
| | 122 | NUM_PROCESSES.times do |i| |
|---|
| | 123 | unless fork |
|---|
| | 124 | @id = i |
|---|
| | 125 | break |
|---|
| | 126 | end |
|---|
| | 127 | end |
|---|
| | 128 | |
|---|
| | 129 | if @id |
|---|
| | 130 | @id.even? ? Writer.new(@id).run : Searcher.new(@id).run |
|---|
| | 131 | else |
|---|
| | 132 | |
|---|
| | 133 | # create some records to search for |
|---|
| | 134 | 20.times do |i| |
|---|
| | 135 | Content.create! :title => "to find #{i}", :description => ("findme #{i} " << random_document) |
|---|
| | 136 | end |
|---|
| | 137 | |
|---|
| | 138 | while true |
|---|
| | 139 | puts "open connections: #{Monitor::count_connections}; time elapsed: #{Time.now - @start} seconds" |
|---|
| | 140 | sleep 10 |
|---|