mysql2 0.2.24 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.gitignore +12 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +1 -0
  4. data/CHANGELOG.md +148 -0
  5. data/Gemfile +3 -0
  6. data/README.rdoc +257 -0
  7. data/Rakefile +5 -0
  8. data/benchmark/active_record.rb +51 -0
  9. data/benchmark/active_record_threaded.rb +42 -0
  10. data/benchmark/allocations.rb +33 -0
  11. data/benchmark/escape.rb +36 -0
  12. data/benchmark/query_with_mysql_casting.rb +80 -0
  13. data/benchmark/query_without_mysql_casting.rb +47 -0
  14. data/benchmark/sequel.rb +37 -0
  15. data/benchmark/setup_db.rb +119 -0
  16. data/benchmark/threaded.rb +44 -0
  17. data/ext/mysql2/client.c +272 -849
  18. data/ext/mysql2/client.h +12 -27
  19. data/ext/mysql2/extconf.rb +14 -72
  20. data/ext/mysql2/mysql2_ext.h +4 -7
  21. data/ext/mysql2/result.c +123 -319
  22. data/ext/mysql2/result.h +1 -4
  23. data/lib/active_record/connection_adapters/em_mysql2_adapter.rb +64 -0
  24. data/lib/active_record/fiber_patches.rb +104 -0
  25. data/lib/mysql2.rb +5 -20
  26. data/lib/mysql2/client.rb +200 -50
  27. data/lib/mysql2/em.rb +3 -13
  28. data/lib/mysql2/em_fiber.rb +31 -0
  29. data/lib/mysql2/error.rb +6 -71
  30. data/lib/mysql2/version.rb +2 -2
  31. data/mysql2.gemspec +32 -0
  32. data/spec/em/em_fiber_spec.rb +22 -0
  33. data/spec/em/em_spec.rb +9 -74
  34. data/spec/mysql2/client_spec.rb +126 -593
  35. data/spec/mysql2/error_spec.rb +44 -58
  36. data/spec/mysql2/result_spec.rb +85 -257
  37. data/spec/spec_helper.rb +3 -24
  38. data/tasks/benchmarks.rake +20 -0
  39. data/tasks/compile.rake +71 -0
  40. data/tasks/rspec.rake +16 -0
  41. data/tasks/vendor_mysql.rake +40 -0
  42. metadata +179 -92
  43. checksums.yaml +0 -7
  44. data/README.md +0 -524
  45. data/ext/mysql2/infile.c +0 -122
  46. data/ext/mysql2/infile.h +0 -1
  47. data/ext/mysql2/mysql_enc_name_to_ruby.h +0 -168
  48. data/ext/mysql2/mysql_enc_to_ruby.h +0 -246
  49. data/ext/mysql2/wait_for_single_fd.h +0 -36
  50. data/lib/active_record/connection_adapters/mysql2_adapter.rb +0 -635
  51. data/lib/arel/engines/sql/compilers/mysql2_compiler.rb +0 -11
  52. data/lib/mysql2/console.rb +0 -5
  53. data/spec/configuration.yml.example +0 -17
  54. data/spec/my.cnf.example +0 -9
  55. data/spec/test_data +0 -1
  56. data/support/mysql_enc_to_ruby.rb +0 -82
  57. data/support/ruby_enc_to_mysql.rb +0 -61
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ Makefile
2
+ *.dSYM
3
+ *.o
4
+ *.bundle
5
+ *.so
6
+ *.a
7
+ *.rbc
8
+ mkmf.log
9
+ pkg/
10
+ tmp
11
+ vendor
12
+ lib/mysql2/mysql2.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@mysql2 --create
data/CHANGELOG.md ADDED
@@ -0,0 +1,148 @@
1
+ # Changelog
2
+
3
+ ## 0.3.0 (April 26th, 2011)
4
+ * switch to MySQL Connector/C for win32 builds
5
+ * win32 bugfixes
6
+ * BREAKING CHANGE: the ActiveRecord adapter has been pulled into Rails 3.1 and is no longer part of the gem
7
+ * added Mysql2::Client.escape (class-level) for raw one-off non-encoding-aware escaping
8
+
9
+ ## 0.2.7 (March 28th, 2011)
10
+ * various fixes for em_mysql2 and fiber usage
11
+ * use our own Mysql2IndexDefinition class for better compatibility across ActiveRecord versions
12
+ * ensure the query is a string earlier in the Mysql2::Client#query codepath for 1.9
13
+ * only set binary ruby encoding on fields that have a binary flag *and* encoding set
14
+ * a few various optimizations
15
+ * add support for :read_timeout to be set on a connection
16
+ * Fix to install with MariDB on Windows
17
+ * add fibered em connection without activerecord
18
+ * fix some 1.9.3 compilation warnings
19
+ * add LD_RUN_PATH when using hard coded mysql paths - this should help users with MySQL installed in non-standard locations
20
+ * for windows support, duplicate the socket from libmysql and create a temporary CRT fd
21
+ * fix for handling years before 1970 on Windows
22
+ * fixes to the Fiber adapter
23
+ * set wait_timeout maximum on Windows to 2147483
24
+ * update supported range for Time objects
25
+ * upon being required, make sure the libmysql we're using is the one we were built against
26
+ * add Mysql2::Client#thread_id
27
+ * add Mysql2::Client#ping
28
+ * switch connection check in AR adapter to use Mysql2::Client#ping for efficiency
29
+ * prefer linking against thread-safe version of libmysqlclient
30
+ * define RSTRING_NOT_MODIFIED for an awesome rbx speed boost
31
+ * expose Mysql2::Client#encoding in 1.9, make sure we set the error message and sqlstate encodings accordingly
32
+ * do not segfault when raising for invalid charset (found in 1.9.3dev)
33
+
34
+ ## 0.2.6 (October 19th, 2010)
35
+ * version bump since the 0.2.5 win32 binary gems were broken
36
+
37
+ ## 0.2.5 (October 19th, 2010)
38
+ * fixes for easier Win32 binary gem deployment for targeting 1.8 and 1.9 in the same gem
39
+ * refactor of connection checks and management to avoid race conditions with the GC/threading to prevent the unexpected loss of connections
40
+ * update the default flags during connection
41
+ * add support for setting wait_timeout on AR adapter
42
+ * upgrade to rspec2
43
+ * bugfix for an edge case where the GC would clean up a Mysql2::Client object before the underlying MYSQL pointer had been initialized
44
+ * fix to CFLAGS to allow compilation on SPARC with sunstudio compiler - Anko painting <anko.com+github@gmail.com>
45
+
46
+ ## 0.2.4 (September 17th, 2010)
47
+ * a few patches for win32 support from Luis Lavena - thanks man!
48
+ * bugfix from Eric Wong to avoid a potential stack overflow during Mysql2::Client#escape
49
+ * added the ability to turn internal row caching on/off via the :cache_rows => true/false option
50
+ * a couple of small patches for rbx compatibility
51
+ * set IndexDefinition#length in AR adapter - Kouhei Yanagita <yanagi@shakenbu.org>
52
+ * fix a long-standing data corruption bug - thank you thank you thank you to @joedamato (http://github.com/ice799)
53
+ * bugfix from calling mysql_close on a closed/freed connection surfaced by the above fix
54
+
55
+ ## 0.2.3 (August 20th, 2010)
56
+ * connection flags can now be passed to the constructor via the :flags key
57
+ * switch AR adapter connection over to use FOUND_ROWS option
58
+ * patch to ensure we use DateTime objects in place of Time for timestamps that are out of the supported range on 32bit platforms < 1.9.2
59
+
60
+ ## 0.2.2 (August 19th, 2010)
61
+ * Change how AR adapter would send initial commands upon connecting
62
+ ** we can make multiple session variable assignments in a single query
63
+ * fix signal handling when waiting on queries
64
+ * retry connect if interrupted by signals
65
+
66
+ ## 0.2.1 (August 16th, 2010)
67
+ * bring mysql2 ActiveRecord adapter back into gem
68
+
69
+ ## 0.2.0 (August 16th, 2010)
70
+ * switch back to letting libmysql manage all allocation/thread-state/freeing for the connection
71
+ * cache various numeric type conversions in hot-spots of the code for a little speed boost
72
+ * ActiveRecord adapter moved into Rails 3 core
73
+ ** Don't worry 2.3.x users! We'll either release the adapter as a separate gem, or try to get it into 2.3.9
74
+ * Fix for the "closed MySQL connection" error (GH #31)
75
+ * Fix for the "can't modify frozen object" error in 1.9.2 (GH #37)
76
+ * Introduce cascading query and result options (more info in README)
77
+ * Sequel adapter pulled into core (will be in the next release - 3.15.0 at the time of writing)
78
+ * add a safety check when attempting to send a query before a result has been fetched
79
+
80
+ ## 0.1.9 (July 17th, 2010)
81
+ * Support async ActiveRecord access with fibers and EventMachine (mperham)
82
+ * string encoding support for 1.9, respecting Encoding.default_internal
83
+ * added support for rake-compiler (tenderlove)
84
+ * bugfixes for ActiveRecord driver
85
+ ** one minor bugfix for TimeZone support
86
+ ** fix the select_rows method to return what it should according to the docs (r-stu31)
87
+ * Mysql2::Client#fields method added - returns the array of field names from a resultset, as strings
88
+ * Sequel adapter
89
+ ** bugfix regarding sybolized field names (Eric Wong)
90
+ ** fix query logging in Sequel adapter
91
+ * Lots of nice code cleanup (tenderlove)
92
+ ** Mysql2::Error definition moved to pure-Ruby
93
+ ** Mysql2::client#initialize definition moved to pure-Ruby
94
+ ** Mysql2::Result partially moved to pure-Ruby
95
+
96
+ ## 0.1.8 (June 2nd, 2010)
97
+ * fixes for AR adapter for timezone juggling
98
+ * fixes to be able to run benchmarks and specs under 1.9.2
99
+
100
+ ## 0.1.7 (May 22nd, 2010)
101
+ * fix a bug when using the disconnect! method on a closed connection in the AR driver
102
+
103
+ ## 0.1.6 (May 14th, 2010)
104
+ * more fixes to the AR adapter related to casting
105
+ * add missing index creation override method to AR adapter
106
+ * added sql_state and error_number methods to the Mysql2::Error exception class
107
+
108
+ ## 0.1.5 (May 12th, 2010)
109
+ * quite a few patches from Eric Wong related to thread-safety, non-blocking I/O and general cleanup
110
+ ** wrap mysql_real_connect with rb_thread_blocking_region
111
+ ** release GVL for possibly blocking mysql_* library calls
112
+ ** [cleanup] quiet down warnings
113
+ ** [cleanup] make all C symbols static
114
+ ** add Mysql2::Client#close method
115
+ ** correctly free the wrapped result in case of EOF
116
+ ** Fix memory leak from the result wrapper struct itself
117
+ ** make Mysql2::Client destructor safely non-blocking
118
+ * bug fixes for ActiveRecord adapter
119
+ ** added casting for default values since they all come back from Mysql as strings (!?!)
120
+ ** missing constant was added
121
+ ** fixed a typo in the show_variable method
122
+ * switched over sscanf for date/time parsing in C
123
+ * made some specs a little finer-grained
124
+ * initial Sequel adapter added
125
+ * updated query benchmarks to reflect the difference between casting in C and in Ruby
126
+
127
+ ## 0.1.4 (April 23rd, 2010)
128
+ * optimization: implemented a local cache for rows that are lazily created in ruby during iteration. The MySQL C result is freed as soon as all the results have been cached
129
+ * optimization: implemented a local cache for field names so every row reuses the same objects as field names/keys
130
+ * refactor the Mysql2 connection adapter for ActiveRecord to not extend the Mysql adapter - now being a free-standing connection adapter
131
+
132
+ ## 0.1.3 (April 15th, 2010)
133
+ * added an EventMachine Deferrable API
134
+ * added an ActiveRecord connection adapter
135
+ ** should be compatible with 2.3.5 and 3.0 (including Arel)
136
+
137
+ ## 0.1.2 (April 9th, 2010)
138
+ * fix a bug (copy/paste fail) around checking for empty TIME values and returning nil (thanks @marius)
139
+
140
+ ## 0.1.1 (April 6th, 2010)
141
+ * added affected_rows method (mysql_affected_rows)
142
+ * added last_id method (last_insert_id)
143
+ * enable reconnect option by default
144
+ * added initial async query support
145
+ * updated extconf (thanks to the mysqlplus project) for easier gem building
146
+
147
+ ## 0.1.0 (April 6th, 2010)
148
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,257 @@
1
+ = Mysql2 - A modern, simple and very fast Mysql library for Ruby - binding to libmysql
2
+
3
+ The Mysql2 gem is meant to serve the extremely common use-case of connecting, querying and iterating on results.
4
+ Some database libraries out there serve as direct 1:1 mappings of the already complex C API's available.
5
+ This one is not.
6
+
7
+ It also forces the use of UTF-8 [or binary] for the connection [and all strings in 1.9, unless Encoding.default_internal is set then it'll convert from UTF-8 to that encoding] and uses encoding-aware MySQL API calls where it can.
8
+
9
+ The API consists of two clases:
10
+
11
+ Mysql2::Client - your connection to the database
12
+
13
+ Mysql2::Result - returned from issuing a #query on the connection. It includes Enumerable.
14
+
15
+ == Installing
16
+
17
+ gem install mysql2
18
+
19
+ You may have to specify --with-mysql-config=/some/random/path/bin/mysql_config
20
+
21
+ == Usage
22
+
23
+ Connect to a database:
24
+
25
+ # this takes a hash of options, almost all of which map directly
26
+ # to the familiar database.yml in rails
27
+ # See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html
28
+ client = Mysql2::Client.new(:host => "localhost", :username => "root")
29
+
30
+ Then query it:
31
+
32
+ results = client.query("SELECT * FROM users WHERE group='githubbers'")
33
+
34
+ Need to escape something first?
35
+
36
+ escaped = client.escape("gi'thu\"bbe\0r's")
37
+ results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
38
+
39
+ Finally, iterate over the results:
40
+
41
+ results.each do |row|
42
+ # conveniently, row is a hash
43
+ # the keys are the fields, as you'd expect
44
+ # the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
45
+ # Here's an otter: http://farm1.static.flickr.com/130/398077070_b8795d0ef3_b.jpg
46
+ end
47
+
48
+ Or, you might just keep it simple:
49
+
50
+ client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
51
+ # do something with row, it's ready to rock
52
+ end
53
+
54
+ How about with symbolized keys?
55
+
56
+ # NOTE: the :symbolize_keys and future options will likely move to the #query method soon
57
+ client.query("SELECT * FROM users WHERE group='githubbers'").each(:symbolize_keys => true) do |row|
58
+ # do something with row, it's ready to rock
59
+ end
60
+
61
+ You can get the headers and the columns in the order that they were returned
62
+ by the query like this:
63
+
64
+ headers = results.fields # <= that's an array of field names, in order
65
+ results.each(:as => :array) do |row|
66
+ # Each row is an array, ordered the same as the query results
67
+ # An otter's den is called a "holt" or "couch"
68
+ end
69
+
70
+ == Cascading config
71
+
72
+ The default config hash is at:
73
+
74
+ Mysql2::Client.default_query_options
75
+
76
+ which defaults to:
77
+
78
+ {:async => false, :as => :hash, :symbolize_keys => false}
79
+
80
+ that can be used as so:
81
+
82
+ # these are the defaults all Mysql2::Client instances inherit
83
+ Mysql2::Client.default_query_options.merge!(:as => :array)
84
+
85
+ or
86
+
87
+ # this will change the defaults for all future results returned by the #query method _for this connection only_
88
+ c = Mysql2::Client.new
89
+ c.query_options.merge!(:symbolize_keys => true)
90
+
91
+ or
92
+
93
+ # this will set the options for the Mysql2::Result instance returned from the #query method
94
+ c = Mysql2::Client.new
95
+ c.query(sql, :symbolize_keys => true)
96
+
97
+ == Result types
98
+
99
+ === Array of Arrays
100
+
101
+ Pass the :as => :array option to any of the above methods of configuration
102
+
103
+ === Array of Hashes
104
+
105
+ The default result type is set to :hash, but you can override a previous setting to something else with :as => :hash
106
+
107
+ === Others...
108
+
109
+ I may add support for :as => :csv or even :as => :json to allow for *much* more efficient generation of those data types from result sets.
110
+ If you'd like to see either of these (or others), open an issue and start bugging me about it ;)
111
+
112
+ === Timezones
113
+
114
+ Mysql2 now supports two timezone options:
115
+
116
+ :database_timezone - this is the timezone Mysql2 will assume fields are already stored as, and will use this when creating the initial Time objects in ruby
117
+ :application_timezone - this is the timezone Mysql2 will convert to before finally handing back to the caller
118
+
119
+ In other words, if :database_timezone is set to :utc - Mysql2 will create the Time objects using Time.utc(...) from the raw value libmysql hands over initially.
120
+ Then, if :application_timezone is set to say - :local - Mysql2 will then convert the just-created UTC Time object to local time.
121
+
122
+ Both options only allow two values - :local or :utc - with the exception that :application_timezone can be [and defaults to] nil
123
+
124
+ === Casting "boolean" columns
125
+
126
+ You can now tell Mysql2 to cast tinyint(1) fields to boolean values in Ruby with the :cast_booleans option.
127
+
128
+ client = Mysql2::Client.new
129
+ result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
130
+
131
+ === Async
132
+
133
+ Mysql2::Client takes advantage of the MySQL C API's (undocumented) non-blocking function mysql_send_query for *all* queries.
134
+ But, in order to take full advantage of it in your Ruby code, you can do:
135
+
136
+ client.query("SELECT sleep(5)", :async => true)
137
+
138
+ Which will return nil immediately. At this point you'll probably want to use some socket monitoring mechanism
139
+ like EventMachine or even IO.select. Once the socket becomes readable, you can do:
140
+
141
+ # result will be a Mysql2::Result instance
142
+ result = client.async_result
143
+
144
+ NOTE: Because of the way MySQL's query API works, this method will block until the result is ready.
145
+ So if you really need things to stay async, it's best to just monitor the socket with something like EventMachine.
146
+ If you need multiple query concurrency take a look at using a connection pool.
147
+
148
+ === Row Caching
149
+
150
+ By default, Mysql2 will cache rows that have been created in Ruby (since this happens lazily).
151
+ This is especially helpful since it saves the cost of creating the row in Ruby if you were to iterate over the collection again.
152
+
153
+ If you only plan on using each row once, then it's much more efficient to disable this behavior by setting the :cache_rows option to false.
154
+ This would be helpful if you wanted to iterate over the results in a streaming manner. Meaning the GC would cleanup rows you don't need anymore as you're iterating over the result set.
155
+
156
+ == ActiveRecord
157
+
158
+ To use the ActiveRecord driver (with our without rails), all you should need to do is have this gem installed and set the adapter in your database.yml to "mysql2".
159
+ That was easy right? :)
160
+
161
+ == Asynchronous ActiveRecord
162
+
163
+ You can also use Mysql2 with asynchronous Rails (first introduced at http://www.mikeperham.com/2010/04/03/introducing-phat-an-asynchronous-rails-app/) by
164
+ setting the adapter in your database.yml to "em_mysql2". You must be running Ruby 1.9, thin and the rack-fiber_pool middleware for it to work.
165
+
166
+ == Sequel
167
+
168
+ The Sequel adapter was pulled out into Sequel core (will be part of the next release) and can be used by specifying the "mysql2://" prefix to your connection specification.
169
+
170
+ == EventMachine
171
+
172
+ The mysql2 EventMachine deferrable api allows you to make async queries using EventMachine,
173
+ while specifying callbacks for success for failure. Here's a simple example:
174
+
175
+ require 'mysql2/em'
176
+
177
+ EM.run do
178
+ client1 = Mysql2::EM::Client.new
179
+ defer1 = client1.query "SELECT sleep(3) as first_query"
180
+ defer1.callback do |result|
181
+ puts "Result: #{result.to_a.inspect}"
182
+ end
183
+
184
+ client2 = Mysql2::EM::Client.new
185
+ defer2 = client2.query "SELECT sleep(1) second_query"
186
+ defer2.callback do |result|
187
+ puts "Result: #{result.to_a.inspect}"
188
+ end
189
+ end
190
+
191
+ == Lazy Everything
192
+
193
+ Well... almost ;)
194
+
195
+ Field name strings/symbols are shared across all the rows so only one object is ever created to represent the field name for an entire dataset.
196
+
197
+ Rows themselves are lazily created in ruby-land when an attempt to yield it is made via #each.
198
+ For example, if you were to yield 4 rows from a 100 row dataset, only 4 hashes will be created. The rest will sit and wait in C-land until you want them (or when the GC goes to cleanup your Mysql2::Result instance).
199
+ Now say you were to iterate over that same collection again, this time yielding 15 rows - the 4 previous rows that had already been turned into ruby hashes would be pulled from an internal cache, then 11 more would be created and stored in that cache.
200
+ Once the entire dataset has been converted into ruby objects, Mysql2::Result will free the Mysql C result object as it's no longer needed.
201
+
202
+ This caching behavior can be disabled by setting the :cache_rows option to false.
203
+
204
+ As for field values themselves, I'm workin on it - but expect that soon.
205
+
206
+ == Compatibility
207
+
208
+ The specs pass on my system (SL 10.6.3, x86_64) in these rubies:
209
+
210
+ * 1.8.7-p249
211
+ * ree-1.8.7-2010.01
212
+ * 1.9.1-p378
213
+ * ruby-trunk
214
+ * rbx-head - broken at the moment, working with the rbx team for a solution
215
+
216
+ The ActiveRecord driver should work on 2.3.5 and 3.0
217
+
218
+ == Yeah... but why?
219
+
220
+ Someone: Dude, the Mysql gem works fiiiiiine.
221
+
222
+ Me: It sure does, but it only hands you nil and strings for field values. Leaving you to convert
223
+ them into proper Ruby types in Ruby-land - which is slow as balls.
224
+
225
+
226
+ Someone: OK fine, but do_mysql can already give me back values with Ruby objects mapped to MySQL types.
227
+
228
+ Me: Yep, but it's API is considerably more complex *and* can be ~2x slower.
229
+
230
+ == Benchmarks
231
+
232
+ Performing a basic "SELECT * FROM" query on a table with 30k rows and fields of nearly every Ruby-representable data type,
233
+ then iterating over every row using an #each like method yielding a block:
234
+
235
+ # These results are from the query_with_mysql_casting.rb script in the benchmarks folder
236
+ user system total real
237
+ Mysql2
238
+ 0.750000 0.180000 0.930000 ( 1.821655)
239
+ do_mysql
240
+ 1.650000 0.200000 1.850000 ( 2.811357)
241
+ Mysql
242
+ 7.500000 0.210000 7.710000 ( 8.065871)
243
+
244
+ == Development
245
+
246
+ To run the tests, you can use RVM and Bundler to create a pristine environment for mysql2 development/hacking.
247
+ Use 'bundle install' to install the necessary development and testing gems:
248
+
249
+ bundle install
250
+ rake
251
+
252
+ == Special Thanks
253
+
254
+ * Eric Wong - for the contribution (and the informative explanations) of some thread-safety, non-blocking I/O and cleanup patches. You rock dude
255
+ * Yury Korolev (http://github.com/yury) - for TONS of help testing the ActiveRecord adapter
256
+ * Aaron Patterson (http://github.com/tenderlove) - tons of contributions, suggestions and general badassness
257
+ * Mike Perham (http://github.com/mperham) - Async ActiveRecord adapter (uses Fibers and EventMachine)
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+ require 'rake'
3
+
4
+ # Load custom tasks
5
+ Dir['tasks/*.rake'].sort.each { |f| load f }
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
3
+
4
+ require 'rubygems'
5
+ require 'benchmark'
6
+ require 'active_record'
7
+
8
+ ActiveRecord::Base.default_timezone = :local
9
+ ActiveRecord::Base.time_zone_aware_attributes = true
10
+
11
+ number_of = 10
12
+ mysql2_opts = {
13
+ :adapter => 'mysql2',
14
+ :database => 'test'
15
+ }
16
+ mysql_opts = {
17
+ :adapter => 'mysql',
18
+ :database => 'test'
19
+ }
20
+
21
+ class Mysql2Model < ActiveRecord::Base
22
+ set_table_name :mysql2_test
23
+ end
24
+
25
+ class MysqlModel < ActiveRecord::Base
26
+ set_table_name :mysql2_test
27
+ end
28
+
29
+ Benchmark.bmbm do |x|
30
+ x.report "Mysql2" do
31
+ Mysql2Model.establish_connection(mysql2_opts)
32
+ number_of.times do
33
+ Mysql2Model.all(:limit => 1000).each{ |r|
34
+ r.attributes.keys.each{ |k|
35
+ r.send(k.to_sym)
36
+ }
37
+ }
38
+ end
39
+ end
40
+
41
+ x.report "Mysql" do
42
+ MysqlModel.establish_connection(mysql_opts)
43
+ number_of.times do
44
+ MysqlModel.all(:limit => 1000).each{ |r|
45
+ r.attributes.keys.each{ |k|
46
+ r.send(k.to_sym)
47
+ }
48
+ }
49
+ end
50
+ end
51
+ end