activerecord-import 2.2.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +124 -0
  3. data/README.markdown +99 -32
  4. data/lib/activerecord-import/active_record/adapters/mysql2_proxy_adapter.rb +8 -0
  5. data/lib/activerecord-import/active_record/adapters/postgresql_proxy_adapter.rb +8 -0
  6. data/lib/activerecord-import/active_record/adapters/sqlite3_proxy_adapter.rb +8 -0
  7. data/lib/activerecord-import/active_record/adapters/trilogy_proxy_adapter.rb +8 -0
  8. data/lib/activerecord-import/adapters/abstract_adapter.rb +58 -0
  9. data/lib/activerecord-import/adapters/active_record_proxy_adapter.rb +19 -0
  10. data/lib/activerecord-import/adapters/mysql2_proxy_adapter.rb +9 -0
  11. data/lib/activerecord-import/adapters/mysql_adapter.rb +70 -13
  12. data/lib/activerecord-import/adapters/postgresql_adapter.rb +10 -52
  13. data/lib/activerecord-import/adapters/postgresql_proxy_adapter.rb +9 -0
  14. data/lib/activerecord-import/adapters/sqlite3_adapter.rb +7 -3
  15. data/lib/activerecord-import/adapters/sqlite3_proxy_adapter.rb +9 -0
  16. data/lib/activerecord-import/adapters/trilogy_proxy_adapter.rb +9 -0
  17. data/lib/activerecord-import/aliases.rb +42 -0
  18. data/lib/activerecord-import/base.rb +2 -46
  19. data/lib/activerecord-import/core.rb +62 -0
  20. data/lib/activerecord-import/import.rb +612 -369
  21. data/lib/activerecord-import/synchronize.rb +28 -16
  22. data/lib/activerecord-import/version.rb +1 -1
  23. data/lib/activerecord-import/without_aliases.rb +17 -0
  24. metadata +19 -211
  25. data/.github/workflows/test.yaml +0 -165
  26. data/.gitignore +0 -37
  27. data/.rubocop.yml +0 -118
  28. data/.rubocop_todo.yml +0 -30
  29. data/Brewfile +0 -5
  30. data/Dockerfile +0 -23
  31. data/Gemfile +0 -63
  32. data/Rakefile +0 -75
  33. data/activerecord-import.gemspec +0 -28
  34. data/benchmarks/README +0 -32
  35. data/benchmarks/benchmark.rb +0 -72
  36. data/benchmarks/lib/base.rb +0 -143
  37. data/benchmarks/lib/cli_parser.rb +0 -111
  38. data/benchmarks/lib/float.rb +0 -17
  39. data/benchmarks/lib/mysql2_benchmark.rb +0 -21
  40. data/benchmarks/lib/output_to_csv.rb +0 -21
  41. data/benchmarks/lib/output_to_html.rb +0 -66
  42. data/benchmarks/models/test_innodb.rb +0 -5
  43. data/benchmarks/models/test_memory.rb +0 -5
  44. data/benchmarks/models/test_myisam.rb +0 -5
  45. data/benchmarks/schema/mysql2_schema.rb +0 -18
  46. data/docker-compose.yml +0 -34
  47. data/gemfiles/5.2.gemfile +0 -4
  48. data/gemfiles/6.0.gemfile +0 -4
  49. data/gemfiles/6.1.gemfile +0 -4
  50. data/gemfiles/7.0.gemfile +0 -4
  51. data/gemfiles/7.1.gemfile +0 -3
  52. data/gemfiles/7.2.gemfile +0 -3
  53. data/gemfiles/8.0.gemfile +0 -3
  54. data/test/adapters/janus_mysql2.rb +0 -3
  55. data/test/adapters/janus_trilogy.rb +0 -11
  56. data/test/adapters/jdbcmysql.rb +0 -3
  57. data/test/adapters/jdbcpostgresql.rb +0 -3
  58. data/test/adapters/jdbcsqlite3.rb +0 -3
  59. data/test/adapters/makara_postgis.rb +0 -3
  60. data/test/adapters/mysql2.rb +0 -3
  61. data/test/adapters/mysql2_makara.rb +0 -3
  62. data/test/adapters/mysql2_proxy.rb +0 -3
  63. data/test/adapters/mysql2spatial.rb +0 -3
  64. data/test/adapters/postgis.rb +0 -3
  65. data/test/adapters/postgresql.rb +0 -3
  66. data/test/adapters/postgresql_makara.rb +0 -3
  67. data/test/adapters/postgresql_proxy.rb +0 -3
  68. data/test/adapters/seamless_database_pool.rb +0 -3
  69. data/test/adapters/spatialite.rb +0 -3
  70. data/test/adapters/sqlite3.rb +0 -3
  71. data/test/adapters/trilogy.rb +0 -11
  72. data/test/database.yml.sample +0 -73
  73. data/test/github/database.yml +0 -85
  74. data/test/import_test.rb +0 -970
  75. data/test/janus_mysql2/import_test.rb +0 -8
  76. data/test/janus_trilogy/import_test.rb +0 -7
  77. data/test/jdbcmysql/import_test.rb +0 -7
  78. data/test/jdbcpostgresql/import_test.rb +0 -6
  79. data/test/jdbcsqlite3/import_test.rb +0 -6
  80. data/test/makara_postgis/import_test.rb +0 -10
  81. data/test/models/account.rb +0 -5
  82. data/test/models/alarm.rb +0 -4
  83. data/test/models/animal.rb +0 -8
  84. data/test/models/author.rb +0 -9
  85. data/test/models/bike_maker.rb +0 -10
  86. data/test/models/book.rb +0 -18
  87. data/test/models/car.rb +0 -5
  88. data/test/models/card.rb +0 -5
  89. data/test/models/chapter.rb +0 -6
  90. data/test/models/composite_book.rb +0 -19
  91. data/test/models/composite_chapter.rb +0 -12
  92. data/test/models/customer.rb +0 -18
  93. data/test/models/deck.rb +0 -8
  94. data/test/models/dictionary.rb +0 -6
  95. data/test/models/discount.rb +0 -5
  96. data/test/models/end_note.rb +0 -6
  97. data/test/models/group.rb +0 -5
  98. data/test/models/order.rb +0 -17
  99. data/test/models/playing_card.rb +0 -4
  100. data/test/models/promotion.rb +0 -5
  101. data/test/models/question.rb +0 -5
  102. data/test/models/rule.rb +0 -5
  103. data/test/models/tag.rb +0 -12
  104. data/test/models/tag_alias.rb +0 -11
  105. data/test/models/topic.rb +0 -31
  106. data/test/models/user.rb +0 -5
  107. data/test/models/user_token.rb +0 -6
  108. data/test/models/vendor.rb +0 -9
  109. data/test/models/widget.rb +0 -33
  110. data/test/mysql2/import_test.rb +0 -7
  111. data/test/mysql2_makara/import_test.rb +0 -8
  112. data/test/mysql2_proxy/import_test.rb +0 -6
  113. data/test/mysqlspatial2/import_test.rb +0 -8
  114. data/test/postgis/import_test.rb +0 -10
  115. data/test/postgresql/import_test.rb +0 -6
  116. data/test/postgresql_proxy/import_test.rb +0 -6
  117. data/test/schema/generic_schema.rb +0 -230
  118. data/test/schema/jdbcpostgresql_schema.rb +0 -3
  119. data/test/schema/mysql2_schema.rb +0 -21
  120. data/test/schema/postgis_schema.rb +0 -3
  121. data/test/schema/postgresql_schema.rb +0 -94
  122. data/test/schema/sqlite3_schema.rb +0 -15
  123. data/test/schema/version.rb +0 -12
  124. data/test/sqlite3/import_test.rb +0 -6
  125. data/test/support/active_support/test_case_extensions.rb +0 -73
  126. data/test/support/assertions.rb +0 -75
  127. data/test/support/factories.rb +0 -66
  128. data/test/support/generate.rb +0 -31
  129. data/test/support/mysql/import_examples.rb +0 -97
  130. data/test/support/postgresql/import_examples.rb +0 -642
  131. data/test/support/shared_examples/on_duplicate_key_ignore.rb +0 -45
  132. data/test/support/shared_examples/on_duplicate_key_update.rb +0 -437
  133. data/test/support/shared_examples/recursive_import.rb +0 -352
  134. data/test/support/sqlite3/import_examples.rb +0 -232
  135. data/test/synchronize_test.rb +0 -43
  136. data/test/test_helper.rb +0 -91
  137. data/test/trilogy/import_test.rb +0 -7
  138. data/test/value_sets_bytes_parser_test.rb +0 -106
  139. data/test/value_sets_records_parser_test.rb +0 -34
data/.rubocop.yml DELETED
@@ -1,118 +0,0 @@
1
- inherit_from: .rubocop_todo.yml
2
-
3
- AllCops:
4
- TargetRubyVersion: 3.0.x
5
-
6
- Metrics/AbcSize:
7
- Enabled: false
8
-
9
- Metrics/BlockLength:
10
- Enabled: false
11
-
12
- Metrics/ClassLength:
13
- Enabled: false
14
-
15
- Metrics/CyclomaticComplexity:
16
- Enabled: false
17
-
18
- Metrics/MethodLength:
19
- Enabled: false
20
-
21
- Metrics/ModuleLength:
22
- Enabled: false
23
-
24
- Metrics/PerceivedComplexity:
25
- Enabled: false
26
-
27
- Style/CommentedKeyword:
28
- Enabled: false
29
-
30
- Style/ClassAndModuleChildren:
31
- Enabled: false
32
-
33
- Style/Documentation:
34
- Enabled: false
35
-
36
- Style/EvalWithLocation:
37
- Enabled: false
38
-
39
- Style/ExpandPathArguments:
40
- Enabled: false
41
-
42
- Style/GuardClause:
43
- Enabled: false
44
-
45
- Style/IfUnlessModifier:
46
- Enabled: false
47
-
48
- Style/NumericPredicate:
49
- Enabled: false
50
-
51
- Style/PercentLiteralDelimiters:
52
- Enabled: false
53
-
54
- Style/RedundantBegin:
55
- Enabled: false
56
-
57
- Style/SpecialGlobalVars:
58
- Enabled: false
59
-
60
- Style/StringLiterals:
61
- Enabled: false
62
-
63
- Style/SymbolArray:
64
- Enabled: false
65
-
66
- Style/TrailingCommaInArrayLiteral:
67
- Enabled: false
68
-
69
- Layout/ArgumentAlignment:
70
- Enabled: false
71
-
72
- Layout/ParameterAlignment:
73
- EnforcedStyle: with_fixed_indentation
74
-
75
- Layout/EndAlignment:
76
- EnforcedStyleAlignWith: variable
77
-
78
- Layout/ElseAlignment:
79
- Enabled: false
80
-
81
- Layout/EmptyLineAfterGuardClause:
82
- Enabled: false
83
-
84
- Layout/HeredocIndentation:
85
- Enabled: false
86
-
87
- Layout/SpaceInsideParens:
88
- Enabled: false
89
-
90
- Layout/SpaceInsidePercentLiteralDelimiters:
91
- Enabled: false
92
-
93
- Layout/LineLength:
94
- Enabled: false
95
-
96
- Lint/ErbNewArguments:
97
- Enabled: false
98
-
99
- Lint/MissingCopEnableDirective:
100
- Enabled: false
101
-
102
- Lint/PercentStringArray:
103
- Enabled: false
104
-
105
- Naming/HeredocDelimiterNaming:
106
- Enabled: false
107
-
108
- Naming/MethodParameterName:
109
- Enabled: false
110
-
111
- Security/YAMLLoad:
112
- Enabled: false
113
-
114
- Gemspec/RequiredRubyVersion:
115
- Enabled: false
116
-
117
- Bundler/OrderedGems:
118
- Enabled: false
data/.rubocop_todo.yml DELETED
@@ -1,30 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2023-02-15 00:58:14 UTC using RuboCop version 1.45.1.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 2
10
- Lint/RescueException:
11
- Exclude:
12
- - 'benchmarks/lib/cli_parser.rb'
13
- - 'test/import_test.rb'
14
-
15
- # Offense count: 3
16
- # This cop supports safe autocorrection (--autocorrect).
17
- # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods.
18
- Lint/UnusedMethodArgument:
19
- Exclude:
20
- - 'lib/activerecord-import/adapters/postgresql_adapter.rb'
21
- - 'lib/activerecord-import/import.rb'
22
-
23
- # Offense count: 2
24
- Style/CombinableLoops:
25
- Exclude:
26
- - 'test/support/shared_examples/recursive_import.rb'
27
-
28
- Naming/FileName:
29
- Exclude:
30
- - 'lib/activerecord-import.rb'
data/Brewfile DELETED
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- brew "mysql"
4
- brew "postgresql"
5
- brew "sqlite"
data/Dockerfile DELETED
@@ -1,23 +0,0 @@
1
- # Use the official Ruby 3.2 image as a base image
2
- ARG RUBY_VERSION=3.2
3
- FROM ruby:${RUBY_VERSION}-bullseye
4
-
5
- # Set the working directory
6
- WORKDIR /usr/src/app
7
-
8
- # Install system packages
9
- RUN apt-get update -qq && \
10
- apt-get install -y default-mysql-client postgresql postgresql-contrib vim && \
11
- apt-get clean
12
-
13
- # Set environment variables
14
- ENV AR_VERSION=7.0
15
-
16
- # Copy all files
17
- COPY . .
18
-
19
- # Move sample database.yml and install gems
20
- RUN mv test/database.yml.sample test/database.yml && \
21
- bundle install
22
-
23
- CMD ["irb"]
data/Gemfile DELETED
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- gemspec
6
-
7
- version = ENV['AR_VERSION'].to_f
8
-
9
- mysql2_version = '0.4.0'
10
- mysql2_version = '0.5.0' if version >= 6.1
11
- mysql2_version = '0.5.6' if version >= 8.0
12
- sqlite3_version = '1.3.0'
13
- sqlite3_version = '1.4.0' if version >= 6.0
14
- sqlite3_version = '2.2.0' if version >= 8.0
15
- pg_version = '0.9'
16
- pg_version = '1.1' if version >= 6.1
17
- pg_version = '1.5' if version >= 8.0
18
-
19
- group :development, :test do
20
- gem 'rubocop'
21
- gem 'rake'
22
- end
23
-
24
- # Database Adapters
25
- platforms :ruby do
26
- gem "mysql2", "~> #{mysql2_version}"
27
- gem "pg", "~> #{pg_version}"
28
- gem "sqlite3", "~> #{sqlite3_version}"
29
- # seamless_database_pool requires Ruby ~> 2.0
30
- gem "seamless_database_pool", "~> 1.0.20" if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
31
- gem "trilogy" if version >= 6.0
32
- if version >= 6.0 && version <= 7.0
33
- gem "activerecord-trilogy-adapter"
34
- end
35
- end
36
-
37
- platforms :jruby do
38
- gem "jdbc-mysql"
39
- gem "jdbc-postgres"
40
- gem "activerecord-jdbcsqlite3-adapter"
41
- gem "activerecord-jdbcmysql-adapter"
42
- gem "activerecord-jdbcpostgresql-adapter"
43
- end
44
-
45
- # Support libs
46
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0.0")
47
- gem "factory_bot"
48
- else
49
- gem "factory_bot", "~> 5", "< 6.4.5"
50
- end
51
- gem "timecop"
52
- gem "chronic"
53
- gem "mocha", "~> 2.1.0"
54
-
55
- # Debugging
56
- platforms :ruby do
57
- gem "pry-byebug"
58
- gem "pry", "~> 0.14.0"
59
- end
60
-
61
- gem "minitest"
62
-
63
- eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__)
data/Rakefile DELETED
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler"
4
- Bundler.setup
5
-
6
- require 'rake'
7
- require 'rake/testtask'
8
-
9
- namespace :display do
10
- task :notice do
11
- puts
12
- puts "To run tests you must supply the adapter, see rake -T for more information."
13
- puts
14
- end
15
- end
16
- task default: ["display:notice"]
17
-
18
- ADAPTERS = %w(
19
- mysql2
20
- mysql2_makara
21
- mysql2spatial
22
- mysql2_proxy
23
- janus_mysql2
24
- jdbcmysql
25
- jdbcsqlite3
26
- jdbcpostgresql
27
- postgresql
28
- postgresql_makara
29
- postgresql_proxy
30
- postgis
31
- makara_postgis
32
- sqlite3
33
- spatialite
34
- seamless_database_pool
35
- trilogy
36
- janus_trilogy
37
- ).freeze
38
- ADAPTERS.each do |adapter|
39
- namespace :test do
40
- desc "Runs #{adapter} database tests."
41
- Rake::TestTask.new(adapter) do |t|
42
- # FactoryBot has an issue with warnings, so turn off, so noisy
43
- # t.warning = true
44
- t.test_files = FileList["test/adapters/#{adapter}.rb", "test/*_test.rb", "test/active_record/*_test.rb", "test/#{adapter}/**/*_test.rb"]
45
- end
46
- task adapter
47
- end
48
- end
49
-
50
- begin
51
- require 'rcov/rcovtask'
52
- adapter = ENV['ARE_DB']
53
- Rcov::RcovTask.new do |test|
54
- test.libs << 'test'
55
- test.pattern = ["test/adapters/#{adapter}.rb", "test/*_test.rb", "test/#{adapter}/**/*_test.rb"]
56
- test.verbose = true
57
- end
58
- rescue LoadError
59
- task :rcov do
60
- abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov"
61
- end
62
- end
63
-
64
- require 'rdoc/task'
65
- Rake::RDocTask.new do |rdoc|
66
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
67
-
68
- rdoc.rdoc_dir = 'rdoc'
69
- rdoc.title = "activerecord-import #{version}"
70
- rdoc.rdoc_files.include('README*')
71
- rdoc.rdoc_files.include('lib/**/*.rb')
72
- end
73
-
74
- require 'rubocop/rake_task'
75
- RuboCop::RakeTask.new
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path('../lib/activerecord-import/version', __FILE__)
4
-
5
- Gem::Specification.new do |gem|
6
- gem.authors = ["Zach Dennis"]
7
- gem.email = ["zach.dennis@gmail.com"]
8
- gem.summary = "Bulk insert extension for ActiveRecord"
9
- gem.description = "A library for bulk inserting data using ActiveRecord."
10
- gem.homepage = "https://github.com/zdennis/activerecord-import"
11
- gem.license = "MIT"
12
-
13
- gem.metadata = {
14
- "changelog_uri" => "https://github.com/zdennis/activerecord-import/blob/master/CHANGELOG.md"
15
- }
16
-
17
- gem.files = `git ls-files`.split($\)
18
- gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
19
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
- gem.name = "activerecord-import"
21
- gem.require_paths = ["lib"]
22
- gem.version = ActiveRecord::Import::VERSION
23
-
24
- gem.required_ruby_version = ">= 2.4.0"
25
-
26
- gem.add_runtime_dependency "activerecord", ">= 4.2"
27
- gem.add_development_dependency "rake"
28
- end
data/benchmarks/README DELETED
@@ -1,32 +0,0 @@
1
- To run the benchmarks, from within the benchmarks run:
2
- ruby benchmark.rb [options]
3
-
4
- The following options are supported:
5
- --adapter [String] The database adapter to use. IE: mysql, postgresql, oracle
6
-
7
- --do-not-delete By default all records in the benchmark tables will be deleted at the end of the benchmark. This flag indicates not to delete the benchmark data.
8
- --num [Integer] The number of objects to benchmark. (Required!)
9
- --table-type [String] The table type to test. This can be used multiple times. By default it is all table types.
10
- --to-csv [String] Print results in a CSV file format
11
- --to-html [String] Print results in HTML format (String filename must be supplied)
12
-
13
- See "ruby benchmark.rb -h" for the complete listing of options.
14
-
15
- EXAMPLES
16
- --------
17
- To output to html format:
18
- ruby benchmark.rb --adapter=mysql2 --to-html=results.html
19
-
20
- To output to csv format:
21
- ruby benchmark.rb --adapter=mysql2 --to-csv=results.csv
22
-
23
- LIMITATIONS
24
- -----------
25
- Currently MySQL is the only supported adapter to benchmark.
26
-
27
- AUTHOR
28
- ------
29
- Zach Dennis
30
- zach.dennis@gmail.com
31
- http://www.continuousthinking.com
32
-
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
- require "fileutils"
5
- require "active_record"
6
- require "active_record/base"
7
-
8
- benchmark_dir = File.dirname(__FILE__)
9
-
10
- $LOAD_PATH.unshift('.')
11
-
12
- # Get the gem into the load path
13
- $LOAD_PATH.unshift(File.join(benchmark_dir, '..', 'lib'))
14
-
15
- # Load the benchmark files
16
- Dir[File.join( benchmark_dir, 'lib', '*.rb' )].sort.each { |f| require f }
17
-
18
- # Parse the options passed in via the command line
19
- options = BenchmarkOptionParser.parse( ARGV )
20
-
21
- FileUtils.mkdir_p 'log'
22
- ActiveRecord::Base.configurations["test"] = YAML.load_file(File.join(benchmark_dir, "../test/database.yml"))[options.adapter]
23
- ActiveRecord::Base.logger = Logger.new("log/test.log")
24
- ActiveRecord::Base.logger.level = Logger::DEBUG
25
- if ActiveRecord.respond_to?(:default_timezone)
26
- ActiveRecord.default_timezone = :utc
27
- else
28
- ActiveRecord::Base.default_timezone = :utc
29
- end
30
-
31
- require "activerecord-import"
32
- ActiveRecord::Base.establish_connection(:test)
33
-
34
- ActiveSupport::Notifications.subscribe(/active_record.sql/) do |_, _, _, _, hsh|
35
- ActiveRecord::Base.logger.info hsh[:sql]
36
- end
37
-
38
- # Load base/generic schema
39
- require File.join(benchmark_dir, "../test/schema/version")
40
- require File.join(benchmark_dir, "../test/schema/generic_schema")
41
- adapter_schema = File.join(benchmark_dir, "schema/#{options.adapter}_schema.rb")
42
- require adapter_schema if File.exist?(adapter_schema)
43
-
44
- Dir["#{File.dirname(__FILE__)}/models/*.rb"].sort.each { |file| require file }
45
-
46
- require File.join( benchmark_dir, 'lib', "#{options.adapter}_benchmark" )
47
- table_types = if options.benchmark_all_types
48
- ["all"]
49
- else
50
- options.table_types.keys
51
- end
52
-
53
- letter = options.adapter[0].chr
54
- clazz_str = letter.upcase + options.adapter[1..].downcase
55
- clazz = Object.const_get( "#{clazz_str}Benchmark" )
56
-
57
- benchmarks = []
58
- options.number_of_objects.each do |num|
59
- benchmarks << (benchmark = clazz.new)
60
- benchmark.send( "benchmark", table_types, num )
61
- end
62
-
63
- options.outputs.each do |output|
64
- format = output.format.downcase
65
- output_module = Object.const_get( "OutputTo#{format.upcase}" )
66
- benchmarks.each do |benchmark|
67
- output_module.output_results( output.filename, benchmark.results )
68
- end
69
- end
70
-
71
- puts
72
- puts "Done with benchmark!"
@@ -1,143 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class BenchmarkBase
4
- attr_reader :results
5
-
6
- # The main benchmark method dispatcher. This dispatches the benchmarks
7
- # to actual benchmark_xxxx methods.
8
- #
9
- # == PARAMETERS
10
- # * table_types - an array of table types to benchmark
11
- # * num - the number of record insertions to test
12
- def benchmark( table_types, num )
13
- array_of_cols_and_vals = build_array_of_cols_and_vals( num )
14
- table_types.each do |table_type|
15
- send( "benchmark_#{table_type}", array_of_cols_and_vals )
16
- end
17
- end
18
-
19
- # Returns a struct which contains two attritues, +description+ and +tms+ after performing an
20
- # actual benchmark.
21
- #
22
- # == PARAMETERS
23
- # * description - the description of the block that is getting benchmarked
24
- # * blk - the block of code to benchmark
25
- #
26
- # == RETURNS
27
- # A struct object with the following attributes:
28
- # * description - the description of the benchmark ran
29
- # * tms - a Benchmark::Tms containing the results of the benchmark
30
-
31
- BmStruct = Struct.new( :description, :tms, :failed, keyword_init: true )
32
-
33
- def bm( description, &block )
34
- tms = nil
35
- puts "Benchmarking #{description}"
36
-
37
- Benchmark.bm { |x| tms = x.report(&block) }
38
- delete_all
39
- failed = false
40
-
41
- BmStruct.new( description: description, tms: tms, failed: failed )
42
- end
43
-
44
- # Given a model class (ie: Topic), and an array of columns and value sets
45
- # this will perform all of the benchmarks necessary for this library.
46
- #
47
- # == PARAMETERS
48
- # * model_clazz - the model class to benchmark (ie: Topic)
49
- # * array_of_cols_and_vals - an array of column identifiers and value sets
50
- #
51
- # == RETURNS
52
- # returns true
53
- def bm_model( model_clazz, array_of_cols_and_vals )
54
- puts
55
- puts "------ Benchmarking #{model_clazz.name} -------"
56
-
57
- cols, vals = array_of_cols_and_vals
58
- num_inserts = vals.size
59
-
60
- # add a new result group for this particular benchmark
61
- group = []
62
- @results << group
63
-
64
- description = "#{model_clazz.name}.create (#{num_inserts} records)"
65
- group << bm( description ) do
66
- vals.each do |values|
67
- model_clazz.create create_hash_for_cols_and_vals( cols, values )
68
- end
69
- end
70
-
71
- description = "#{model_clazz.name}.import(column, values) for #{num_inserts} records with validations"
72
- group << bm( description ) { model_clazz.import cols, vals, validate: true }
73
-
74
- description = "#{model_clazz.name}.import(columns, values) for #{num_inserts} records without validations"
75
- group << bm( description ) { model_clazz.import cols, vals, validate: false }
76
-
77
- models = []
78
- array_of_attrs = []
79
-
80
- vals.each do |arr|
81
- array_of_attrs << (attrs = {})
82
- arr.each_with_index { |value, i| attrs[cols[i]] = value }
83
- end
84
- array_of_attrs.each { |attrs| models << model_clazz.new(attrs) }
85
-
86
- description = "#{model_clazz.name}.import(models) for #{num_inserts} records with validations"
87
- group << bm( description ) { model_clazz.import models, validate: true }
88
-
89
- description = "#{model_clazz.name}.import(models) for #{num_inserts} records without validations"
90
- group << bm( description ) { model_clazz.import models, validate: false }
91
-
92
- true
93
- end
94
-
95
- # Returns a two element array composing of an array of columns and an array of
96
- # value sets given the passed +num+.
97
- #
98
- # === What is a value set?
99
- # A value set is an array of arrays. Each child array represents an array of value sets
100
- # for a given row of data.
101
- #
102
- # For example, say we wanted to represent an insertion of two records:
103
- # column_names = [ 'id', 'name', 'description' ]
104
- # record1 = [ 1, 'John Doe', 'A plumber' ]
105
- # record2 = [ 2, 'John Smith', 'A painter' ]
106
- # value_set [ record1, record2 ]
107
- #
108
- # == PARAMETER
109
- # * num - the number of records to create
110
- def build_array_of_cols_and_vals( num )
111
- cols = [:my_name, :description]
112
- value_sets = []
113
- num.times { |i| value_sets << ["My Name #{i}", "My Description #{i}"] }
114
- [cols, value_sets]
115
- end
116
-
117
- # Returns a hash of column identifier to value mappings giving the passed in
118
- # value array.
119
- #
120
- # Example:
121
- # cols = [ 'id', 'name', 'description' ]
122
- # values = [ 1, 'John Doe', 'A plumber' ]
123
- # hsh = create_hash_for_cols_and_vals( cols, values )
124
- # # hsh => { 'id'=>1, 'name'=>'John Doe', 'description'=>'A plumber' }
125
- def create_hash_for_cols_and_vals( cols, vals )
126
- h = {}
127
- cols.zip( vals ) { |col, val| h[col] = val }
128
- h
129
- end
130
-
131
- # Deletes all records from all ActiveRecord subclasses
132
- def delete_all
133
- ActiveRecord::Base.send( :subclasses ).each do |subclass|
134
- if subclass.table_exists? && subclass.respond_to?(:delete_all)
135
- subclass.delete_all
136
- end
137
- end
138
- end
139
-
140
- def initialize # :nodoc:
141
- @results = []
142
- end
143
- end