Permalink
Browse files

begin big refactor

  • Loading branch information...
1 parent cafe141 commit 47eb4f6c168fc7a0deaf3ef2bf50818f0e35010d @ghendry committed Mar 27, 2011
View
8 lib/index_tester/generate_tests.rb → lib/index_tester/query_tests.rb
@@ -1,8 +1,14 @@
require 'mustache'
module IndexTester
- class GenerateTests
+ class QueryTests
+ class << self
+ def generate(log, db)
+ queries = log.unique_selects
+ #new(queries,db)
+ end
+ end
def initialize(harvester)
@harvester = harvester
end
View
24 lib/index_tester/read_log.rb
@@ -4,19 +4,29 @@ module IndexTester
class ReadLog
def initialize(file_name)
@file_name = file_name
- @content = load_csv
- end
-
- def load_csv
- CSV.read(@file_name)
+ #puts content.inspect
end
def unique_log_entries
- @content.inject({}) {|list, line| list[line[1]] = line; list}.values
+ #content.inject({}) {|list, line| list[line[1]] = line; list}.values
+ content
end
def unique_selects
- unique_log_entries.inject([]) {|list, entry| list << entry[0]; list}
+ #unique_log_entries.inject([]) {|list, entry| list << entry[0]; list}
+ content
+ end
+
+ private
+ def content
+ @content ||= File.new(@file_name, 'r').readlines.map {|line| line.split('|')}
+ @content.map do |line|
+ h = {}
+ [:type,:begin_time,:end_time,:elapsed,:location,:sql].each_with_index do |key, i|
+ h[key] = line[i].chomp
+ end
+ h
+ end
end
end
end
View
6 script/index_tester/write_index_tests.rb
@@ -0,0 +1,6 @@
+require_relative '../../lib/index_tester/read_log'
+
+if $0 == __FILE__
+ return "Please provide log file name and database" unless ARGV.size > 1
+ puts IndexTester::QueryTests.generate(IndexTester::ReadLog.new(ARGV[0]), ARGV[1])
+end
View
15 template/index_tester/tests_view.mustache
@@ -0,0 +1,15 @@
+require_relative '../../lib/index_tester/query_rating'
+
+class QueryRatingTests < MiniTest::Unit::TestCase
+ include IndexTester::QueryRating
+
+ def test_queries
+ {{#looper}}
+ # For query: {{query}}
+ assert_operator 50, :<, query_rating(total_count: {{total_count}},
+ no_indexes: {{no_indexes}},
+ scanned_count: {{scanned_count}},
+ returned_count: {{returned_count}})
+ {{/looper}}
+ end
+end
View
17 test/index_tester/fixture/sql.log
@@ -0,0 +1,17 @@
+sql.active_record|2011-03-26 23:17:51|2011-03-26 23:17:51|0.000343|SQL|explain select * from people where usn = 'gh'
+sql.active_record|2011-03-26 23:17:51|2011-03-26 23:17:51|0.000336|PeopleConnect::Person Load|SELECT `people`.* FROM `people` ORDER BY people.usn
+sql.active_record|2011-03-26 23:18:00|2011-03-26 23:18:00|0.001385|SQL|SHOW TABLES
+sql.active_record|2011-03-26 23:18:00|2011-03-26 23:18:00|0.000391|PeopleConnect::Person Load|SELECT `people`.* FROM `people` WHERE `people`.`usn` = 'gh'
+sql.active_record|2011-03-26 23:18:08|2011-03-26 23:18:08|0.000411|PeopleConnect::Person Load|SELECT `people`.* FROM `people` WHERE `people`.`first_name` = 'Ginny' AND `people`.`last_name` = 'Hendry'
+sql.active_record|2011-03-26 23:18:28|2011-03-26 23:18:28|0.000149|SQL|BEGIN
+sql.active_record|2011-03-26 23:18:28|2011-03-26 23:18:28|0.00035|PeopleConnect::Person Load|SELECT `people`.`id` FROM `people` WHERE (`people`.`usn` = BINARY 'jh') LIMIT 1
+sql.active_record|2011-03-26 23:18:28|2011-03-26 23:18:28|0.001003|SQL|describe `people`
+sql.active_record|2011-03-26 23:18:28|2011-03-26 23:18:28|0.000285|AREL|INSERT INTO `people` (`first_name`, `last_name`, `usn`, `created_at`, `updated_at`) VALUES ('Jamie', 'Hendry', 'jh', '2011-03-26 23:18:28', '2011-03-26 23:18:28')
+sql.active_record|2011-03-26 23:18:28|2011-03-26 23:18:28|0.000398|SQL|COMMIT
+sql.active_record|2011-03-26 23:18:28|2011-03-26 23:18:28|0.000221|PeopleConnect::Person Load|SELECT `people`.* FROM `people`
+sql.active_record|2011-03-26 23:18:40|2011-03-26 23:18:40|0.000134|SQL|BEGIN
+sql.active_record|2011-03-26 23:18:40|2011-03-26 23:18:40|0.002733|PeopleConnect::Person Load|SELECT `people`.`id` FROM `people` WHERE (`people`.`usn` = BINARY 'lh') LIMIT 1
+sql.active_record|2011-03-26 23:18:40|2011-03-26 23:18:40|0.002166|AREL|INSERT INTO `people` (`first_name`, `last_name`, `usn`, `created_at`, `updated_at`) VALUES ('Lori', 'Hendry', 'lh', '2011-03-26 23:18:40', '2011-03-26 23:18:40')
+sql.active_record|2011-03-26 23:18:40|2011-03-26 23:18:40|0.000416|SQL|COMMIT
+sql.active_record|2011-03-26 23:18:40|2011-03-26 23:18:40|0.000258|PeopleConnect::Person Load|SELECT `people`.* FROM `people`
+sql.active_record|2011-03-26 23:18:50|2011-03-26 23:18:50|0.000485|PeopleConnect::Person Load|SELECT `people`.* FROM `people` WHERE `people`.`first_name` = 'Ginny' AND `people`.`last_name` = 'Hendry'
View
8 test/index_tester/log_test.rb
@@ -4,12 +4,12 @@
class TestReadLog < MiniTest::Unit::TestCase
def setup
- @reader = IndexTester::ReadLog.new(File.dirname(__FILE__) + '/fixture/query.log')
+ @reader = IndexTester::ReadLog.new(File.dirname(__FILE__) + '/fixture/sql.log')
end
def test_select
- assert_equal 3, @reader.unique_log_entries.size
- assert_equal 3, @reader.unique_selects.size
+ assert_equal 6, @reader.unique_log_entries.size
+ assert_equal 6, @reader.unique_selects.size
refute_equal @reader.unique_selects[0], @reader.unique_selects[1]
end
end
@@ -22,7 +22,7 @@ def setup
IndexTester::HarvestData.any_instance.stubs(:missing_indexes).returns([true,true,false])
IndexTester::HarvestData.any_instance.stubs(:scanned_counts).returns([2,2,1])
IndexTester::HarvestData.any_instance.stubs(:returned_counts).returns([2,2,1])
- fixture_log = File.dirname(__FILE__) + '/fixture/query.log'
+ fixture_log = File.dirname(__FILE__) + '/fixture/sql.log'
@queries = IndexTester::ReadLog.new(fixture_log).unique_selects
@harvester = IndexTester::HarvestData.new(IndexTester::DataBase.new, @queries)
end
View
10 test/index_tester/query_test.rb
@@ -2,7 +2,7 @@
require_relative '../../lib/index_tester/query_rating'
require_relative '../../lib/index_tester/harvest_data'
require_relative '../../lib/index_tester/data_base'
-require_relative '../../lib/index_tester/generate_tests'
+require_relative '../../lib/index_tester/query_tests'
class TestCreateTests < MiniTest::Unit::TestCase
def setup
@@ -12,13 +12,14 @@ def setup
IndexTester::HarvestData.any_instance.stubs(:missing_indexes).returns([true,true,false])
IndexTester::HarvestData.any_instance.stubs(:scanned_counts).returns([2,2,1])
IndexTester::HarvestData.any_instance.stubs(:returned_counts).returns([2,2,1])
- fixture_log = File.dirname(__FILE__) + '/fixture/query.log'
+ fixture_log = File.dirname(__FILE__) + '/fixture/sql.log'
@queries = IndexTester::ReadLog.new(fixture_log).unique_selects
@harvester = IndexTester::HarvestData.new(IndexTester::DataBase.new, @queries)
- @tests = IndexTester::GenerateTests.new(@harvester)
+ #@tests = IndexTester::GenerateTests.new(@harvester)
end
- def test_queries
+=begin
+ def test_harvester
assert_equal 2, @harvester.record_counts[@harvester.tables[0]]
assert_equal false, @harvester.missing_indexes[2]
assert_equal 1, @harvester.scanned_counts[2]
@@ -30,4 +31,5 @@ def test_making_tests
File.open(File.dirname(__FILE__) + '/generated_test.rb', 'w') {|f| f.puts test_code}
refute_equal '', test_code
end
+=end
end

0 comments on commit 47eb4f6

Please sign in to comment.