activesupport 3.1.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (276) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +798 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +13 -7
  5. data/lib/active_support/array_inquirer.rb +44 -0
  6. data/lib/active_support/backtrace_cleaner.rb +38 -34
  7. data/lib/active_support/benchmarkable.rb +17 -28
  8. data/lib/active_support/cache/file_store.rb +85 -70
  9. data/lib/active_support/cache/mem_cache_store.rb +75 -66
  10. data/lib/active_support/cache/memory_store.rb +31 -23
  11. data/lib/active_support/cache/null_store.rb +41 -0
  12. data/lib/active_support/cache/strategy/local_cache.rb +73 -70
  13. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  14. data/lib/active_support/cache.rb +360 -294
  15. data/lib/active_support/callbacks.rb +563 -393
  16. data/lib/active_support/concern.rb +42 -34
  17. data/lib/active_support/concurrency/latch.rb +19 -0
  18. data/lib/active_support/concurrency/share_lock.rb +186 -0
  19. data/lib/active_support/configurable.rb +70 -12
  20. data/lib/active_support/core_ext/array/access.rb +53 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +109 -62
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +39 -32
  24. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  25. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  26. data/lib/active_support/core_ext/array/wrap.rb +16 -18
  27. data/lib/active_support/core_ext/array.rb +2 -2
  28. data/lib/active_support/core_ext/benchmark.rb +7 -0
  29. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
  30. data/lib/active_support/core_ext/class/attribute.rb +47 -34
  31. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
  32. data/lib/active_support/core_ext/class/subclasses.rb +12 -7
  33. data/lib/active_support/core_ext/class.rb +0 -3
  34. data/lib/active_support/core_ext/date/blank.rb +12 -0
  35. data/lib/active_support/core_ext/date/calculations.rb +57 -167
  36. data/lib/active_support/core_ext/date/conversions.rb +31 -42
  37. data/lib/active_support/core_ext/date/zones.rb +2 -10
  38. data/lib/active_support/core_ext/date.rb +5 -0
  39. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  40. data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
  41. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  42. data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
  43. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  44. data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
  45. data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
  46. data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
  47. data/lib/active_support/core_ext/date_time.rb +5 -0
  48. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  49. data/lib/active_support/core_ext/enumerable.rb +81 -74
  50. data/lib/active_support/core_ext/file/atomic.rb +53 -26
  51. data/lib/active_support/core_ext/file.rb +0 -1
  52. data/lib/active_support/core_ext/hash/compact.rb +20 -0
  53. data/lib/active_support/core_ext/hash/conversions.rb +175 -70
  54. data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
  55. data/lib/active_support/core_ext/hash/except.rb +11 -12
  56. data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
  57. data/lib/active_support/core_ext/hash/keys.rb +147 -24
  58. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  59. data/lib/active_support/core_ext/hash/slice.rb +22 -14
  60. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  61. data/lib/active_support/core_ext/hash.rb +2 -2
  62. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  63. data/lib/active_support/core_ext/integer/multiple.rb +4 -0
  64. data/lib/active_support/core_ext/integer/time.rb +12 -22
  65. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
  66. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  67. data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
  68. data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
  69. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  70. data/lib/active_support/core_ext/kernel.rb +2 -3
  71. data/lib/active_support/core_ext/load_error.rb +14 -7
  72. data/lib/active_support/core_ext/marshal.rb +22 -0
  73. data/lib/active_support/core_ext/module/aliasing.rb +16 -12
  74. data/lib/active_support/core_ext/module/anonymous.rb +12 -8
  75. data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
  76. data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
  77. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  78. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  79. data/lib/active_support/core_ext/module/delegation.rb +141 -68
  80. data/lib/active_support/core_ext/module/deprecation.rb +17 -3
  81. data/lib/active_support/core_ext/module/introspection.rb +9 -31
  82. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  83. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  84. data/lib/active_support/core_ext/module/reachable.rb +1 -3
  85. data/lib/active_support/core_ext/module/remove_method.rb +24 -5
  86. data/lib/active_support/core_ext/module.rb +3 -3
  87. data/lib/active_support/core_ext/name_error.rb +15 -2
  88. data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
  89. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  90. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  91. data/lib/active_support/core_ext/numeric/time.rb +31 -36
  92. data/lib/active_support/core_ext/numeric.rb +2 -0
  93. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  94. data/lib/active_support/core_ext/object/blank.rb +52 -18
  95. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  96. data/lib/active_support/core_ext/object/duplicable.rb +12 -20
  97. data/lib/active_support/core_ext/object/inclusion.rb +13 -1
  98. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  99. data/lib/active_support/core_ext/object/json.rb +205 -0
  100. data/lib/active_support/core_ext/object/to_param.rb +1 -55
  101. data/lib/active_support/core_ext/object/to_query.rb +66 -9
  102. data/lib/active_support/core_ext/object/try.rb +124 -33
  103. data/lib/active_support/core_ext/object/with_options.rb +37 -11
  104. data/lib/active_support/core_ext/object.rb +2 -1
  105. data/lib/active_support/core_ext/range/conversions.rb +17 -7
  106. data/lib/active_support/core_ext/range/each.rb +21 -0
  107. data/lib/active_support/core_ext/range/include_range.rb +20 -18
  108. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  109. data/lib/active_support/core_ext/range.rb +1 -2
  110. data/lib/active_support/core_ext/securerandom.rb +23 -0
  111. data/lib/active_support/core_ext/string/access.rb +95 -90
  112. data/lib/active_support/core_ext/string/behavior.rb +1 -1
  113. data/lib/active_support/core_ext/string/conversions.rb +41 -38
  114. data/lib/active_support/core_ext/string/exclude.rb +6 -1
  115. data/lib/active_support/core_ext/string/filters.rb +70 -17
  116. data/lib/active_support/core_ext/string/indent.rb +43 -0
  117. data/lib/active_support/core_ext/string/inflections.rb +139 -59
  118. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  119. data/lib/active_support/core_ext/string/multibyte.rb +46 -65
  120. data/lib/active_support/core_ext/string/output_safety.rb +153 -56
  121. data/lib/active_support/core_ext/string/strip.rb +3 -6
  122. data/lib/active_support/core_ext/string/zones.rb +14 -0
  123. data/lib/active_support/core_ext/string.rb +2 -3
  124. data/lib/active_support/core_ext/struct.rb +3 -0
  125. data/lib/active_support/core_ext/time/calculations.rb +173 -173
  126. data/lib/active_support/core_ext/time/compatibility.rb +5 -0
  127. data/lib/active_support/core_ext/time/conversions.rb +33 -29
  128. data/lib/active_support/core_ext/time/marshal.rb +2 -56
  129. data/lib/active_support/core_ext/time/zones.rb +57 -32
  130. data/lib/active_support/core_ext/time.rb +5 -0
  131. data/lib/active_support/core_ext/uri.rb +13 -19
  132. data/lib/active_support/core_ext.rb +3 -2
  133. data/lib/active_support/dependencies/autoload.rb +47 -20
  134. data/lib/active_support/dependencies/interlock.rb +51 -0
  135. data/lib/active_support/dependencies.rb +315 -265
  136. data/lib/active_support/deprecation/behaviors.rb +71 -30
  137. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  138. data/lib/active_support/deprecation/method_wrappers.rb +59 -18
  139. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
  140. data/lib/active_support/deprecation/reporting.rb +61 -14
  141. data/lib/active_support/deprecation.rb +38 -13
  142. data/lib/active_support/descendants_tracker.rb +34 -19
  143. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  144. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  145. data/lib/active_support/duration.rb +85 -14
  146. data/lib/active_support/evented_file_update_checker.rb +194 -0
  147. data/lib/active_support/execution_wrapper.rb +117 -0
  148. data/lib/active_support/executor.rb +6 -0
  149. data/lib/active_support/file_update_checker.rb +138 -17
  150. data/lib/active_support/gem_version.rb +15 -0
  151. data/lib/active_support/gzip.rb +11 -5
  152. data/lib/active_support/hash_with_indifferent_access.rb +199 -49
  153. data/lib/active_support/i18n.rb +6 -2
  154. data/lib/active_support/i18n_railtie.rb +40 -21
  155. data/lib/active_support/inflections.rb +22 -13
  156. data/lib/active_support/inflector/inflections.rb +175 -144
  157. data/lib/active_support/inflector/methods.rb +328 -91
  158. data/lib/active_support/inflector/transliterate.rb +51 -37
  159. data/lib/active_support/json/decoding.rb +31 -22
  160. data/lib/active_support/json/encoding.rb +88 -248
  161. data/lib/active_support/key_generator.rb +71 -0
  162. data/lib/active_support/lazy_load_hooks.rb +27 -25
  163. data/lib/active_support/locale/en.yml +102 -3
  164. data/lib/active_support/log_subscriber/test_helper.rb +24 -21
  165. data/lib/active_support/log_subscriber.rb +36 -49
  166. data/lib/active_support/logger.rb +106 -0
  167. data/lib/active_support/logger_silence.rb +28 -0
  168. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  169. data/lib/active_support/message_encryptor.rb +72 -36
  170. data/lib/active_support/message_verifier.rb +96 -24
  171. data/lib/active_support/multibyte/chars.rb +88 -333
  172. data/lib/active_support/multibyte/unicode.rb +156 -136
  173. data/lib/active_support/multibyte.rb +5 -28
  174. data/lib/active_support/notifications/fanout.rb +115 -19
  175. data/lib/active_support/notifications/instrumenter.rb +52 -15
  176. data/lib/active_support/notifications.rb +168 -33
  177. data/lib/active_support/number_helper/number_converter.rb +182 -0
  178. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  179. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  180. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  181. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  182. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  183. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  184. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  185. data/lib/active_support/number_helper.rb +368 -0
  186. data/lib/active_support/option_merger.rb +1 -1
  187. data/lib/active_support/ordered_hash.rb +18 -183
  188. data/lib/active_support/ordered_options.rb +44 -24
  189. data/lib/active_support/per_thread_registry.rb +58 -0
  190. data/lib/active_support/proxy_object.rb +13 -0
  191. data/lib/active_support/rails.rb +27 -0
  192. data/lib/active_support/railtie.rb +25 -34
  193. data/lib/active_support/reloader.rb +129 -0
  194. data/lib/active_support/rescuable.rb +98 -48
  195. data/lib/active_support/security_utils.rb +27 -0
  196. data/lib/active_support/string_inquirer.rb +14 -9
  197. data/lib/active_support/subscriber.rb +120 -0
  198. data/lib/active_support/tagged_logging.rb +78 -0
  199. data/lib/active_support/test_case.rb +69 -17
  200. data/lib/active_support/testing/assertions.rb +43 -41
  201. data/lib/active_support/testing/autorun.rb +12 -0
  202. data/lib/active_support/testing/constant_lookup.rb +50 -0
  203. data/lib/active_support/testing/declarative.rb +7 -21
  204. data/lib/active_support/testing/deprecation.rb +14 -33
  205. data/lib/active_support/testing/file_fixtures.rb +34 -0
  206. data/lib/active_support/testing/isolation.rb +53 -95
  207. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  208. data/lib/active_support/testing/setup_and_teardown.rb +21 -82
  209. data/lib/active_support/testing/stream.rb +42 -0
  210. data/lib/active_support/testing/tagged_logging.rb +25 -0
  211. data/lib/active_support/testing/time_helpers.rb +134 -0
  212. data/lib/active_support/time.rb +6 -23
  213. data/lib/active_support/time_with_zone.rb +239 -92
  214. data/lib/active_support/values/time_zone.rb +236 -160
  215. data/lib/active_support/values/unicode_tables.dat +0 -0
  216. data/lib/active_support/version.rb +5 -7
  217. data/lib/active_support/xml_mini/jdom.rb +19 -13
  218. data/lib/active_support/xml_mini/libxml.rb +3 -4
  219. data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
  220. data/lib/active_support/xml_mini/nokogiri.rb +3 -4
  221. data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
  222. data/lib/active_support/xml_mini/rexml.rb +8 -10
  223. data/lib/active_support/xml_mini.rb +66 -34
  224. data/lib/active_support.rb +40 -23
  225. metadata +185 -134
  226. data/CHANGELOG +0 -1534
  227. data/lib/active_support/base64.rb +0 -42
  228. data/lib/active_support/basic_object.rb +0 -21
  229. data/lib/active_support/buffered_logger.rb +0 -137
  230. data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
  231. data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
  232. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  233. data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
  234. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
  235. data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
  236. data/lib/active_support/core_ext/date/freeze.rb +0 -31
  237. data/lib/active_support/core_ext/date_time/zones.rb +0 -21
  238. data/lib/active_support/core_ext/exception.rb +0 -3
  239. data/lib/active_support/core_ext/file/path.rb +0 -5
  240. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  241. data/lib/active_support/core_ext/float.rb +0 -1
  242. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
  243. data/lib/active_support/core_ext/hash/diff.rb +0 -13
  244. data/lib/active_support/core_ext/kernel/requires.rb +0 -28
  245. data/lib/active_support/core_ext/logger.rb +0 -81
  246. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
  247. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  248. data/lib/active_support/core_ext/module/synchronization.rb +0 -43
  249. data/lib/active_support/core_ext/object/to_json.rb +0 -19
  250. data/lib/active_support/core_ext/proc.rb +0 -14
  251. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  252. data/lib/active_support/core_ext/process.rb +0 -1
  253. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  254. data/lib/active_support/core_ext/range/cover.rb +0 -3
  255. data/lib/active_support/core_ext/rexml.rb +0 -46
  256. data/lib/active_support/core_ext/string/encoding.rb +0 -11
  257. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  258. data/lib/active_support/core_ext/string/xchar.rb +0 -18
  259. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  260. data/lib/active_support/file_watcher.rb +0 -36
  261. data/lib/active_support/json/variable.rb +0 -9
  262. data/lib/active_support/memoizable.rb +0 -105
  263. data/lib/active_support/multibyte/exceptions.rb +0 -8
  264. data/lib/active_support/multibyte/utils.rb +0 -60
  265. data/lib/active_support/ruby/shim.rb +0 -22
  266. data/lib/active_support/secure_random.rb +0 -6
  267. data/lib/active_support/testing/mochaing.rb +0 -7
  268. data/lib/active_support/testing/pending.rb +0 -52
  269. data/lib/active_support/testing/performance/jruby.rb +0 -115
  270. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  271. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  272. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  273. data/lib/active_support/testing/performance/ruby.rb +0 -152
  274. data/lib/active_support/testing/performance.rb +0 -317
  275. data/lib/active_support/time/autoload.rb +0 -5
  276. data/lib/active_support/whiny_nil.rb +0 -60
@@ -1,317 +0,0 @@
1
- require 'fileutils'
2
- require 'rails/version'
3
- require 'active_support/concern'
4
- require 'active_support/core_ext/class/delegating_attributes'
5
- require 'active_support/core_ext/string/inflections'
6
- require 'action_view/helpers/number_helper'
7
-
8
- module ActiveSupport
9
- module Testing
10
- module Performance
11
- extend ActiveSupport::Concern
12
-
13
- included do
14
- superclass_delegating_accessor :profile_options
15
- self.profile_options = {}
16
-
17
- if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
18
- include ForMiniTest
19
- else
20
- include ForClassicTestUnit
21
- end
22
- end
23
-
24
- # each implementation should define metrics and freeze the defaults
25
- DEFAULTS =
26
- if ARGV.include?('--benchmark') # HAX for rake test
27
- { :runs => 4,
28
- :output => 'tmp/performance',
29
- :benchmark => true }
30
- else
31
- { :runs => 1,
32
- :output => 'tmp/performance',
33
- :benchmark => false }
34
- end
35
-
36
- def full_profile_options
37
- DEFAULTS.merge(profile_options)
38
- end
39
-
40
- def full_test_name
41
- "#{self.class.name}##{method_name}"
42
- end
43
-
44
- module ForMiniTest
45
- def run(runner)
46
- @runner = runner
47
-
48
- run_warmup
49
- if full_profile_options && metrics = full_profile_options[:metrics]
50
- metrics.each do |metric_name|
51
- if klass = Metrics[metric_name.to_sym]
52
- run_profile(klass.new)
53
- end
54
- end
55
- end
56
-
57
- return
58
- end
59
-
60
- def run_test(metric, mode)
61
- result = '.'
62
- begin
63
- run_callbacks :setup
64
- setup
65
- metric.send(mode) { __send__ method_name }
66
- rescue Exception => e
67
- result = @runner.puke(self.class, method_name, e)
68
- ensure
69
- begin
70
- teardown
71
- run_callbacks :teardown, :enumerator => :reverse_each
72
- rescue Exception => e
73
- result = @runner.puke(self.class, method_name, e)
74
- end
75
- end
76
- result
77
- end
78
- end
79
-
80
- module ForClassicTestUnit
81
- def run(result)
82
- return if method_name =~ /^default_test$/
83
-
84
- yield(self.class::STARTED, name)
85
- @_result = result
86
-
87
- run_warmup
88
- if full_profile_options && metrics = full_profile_options[:metrics]
89
- metrics.each do |metric_name|
90
- if klass = Metrics[metric_name.to_sym]
91
- run_profile(klass.new)
92
- result.add_run
93
- else
94
- puts '%20s: unsupported' % metric_name
95
- end
96
- end
97
- end
98
-
99
- yield(self.class::FINISHED, name)
100
- end
101
-
102
- def run_test(metric, mode)
103
- run_callbacks :setup
104
- setup
105
- metric.send(mode) { __send__ @method_name }
106
- rescue ::Test::Unit::AssertionFailedError => e
107
- add_failure(e.message, e.backtrace)
108
- rescue StandardError, ScriptError => e
109
- add_error(e)
110
- ensure
111
- begin
112
- teardown
113
- run_callbacks :teardown, :enumerator => :reverse_each
114
- rescue ::Test::Unit::AssertionFailedError => e
115
- add_failure(e.message, e.backtrace)
116
- rescue StandardError, ScriptError => e
117
- add_error(e)
118
- end
119
- end
120
- end
121
-
122
- protected
123
- # overridden by each implementation
124
- def run_gc; end
125
-
126
- def run_warmup
127
- run_gc
128
-
129
- time = Metrics::Time.new
130
- run_test(time, :benchmark)
131
- puts "%s (%s warmup)" % [full_test_name, time.format(time.total)]
132
-
133
- run_gc
134
- end
135
-
136
- def run_profile(metric)
137
- klass = full_profile_options[:benchmark] ? Benchmarker : Profiler
138
- performer = klass.new(self, metric)
139
-
140
- performer.run
141
- puts performer.report
142
- performer.record
143
- end
144
-
145
- class Performer
146
- delegate :run_test, :full_profile_options, :full_test_name, :to => :@harness
147
-
148
- def initialize(harness, metric)
149
- @harness, @metric, @supported = harness, metric, false
150
- end
151
-
152
- def report
153
- if @supported
154
- rate = @total / full_profile_options[:runs]
155
- '%20s: %s' % [@metric.name, @metric.format(rate)]
156
- else
157
- '%20s: unsupported' % @metric.name
158
- end
159
- end
160
-
161
- protected
162
- def output_filename
163
- "#{full_profile_options[:output]}/#{full_test_name}_#{@metric.name}"
164
- end
165
- end
166
-
167
- # overridden by each implementation
168
- class Profiler < Performer
169
- def time_with_block
170
- before = Time.now
171
- yield
172
- Time.now - before
173
- end
174
-
175
- def run; end
176
- def record; end
177
- end
178
-
179
- class Benchmarker < Performer
180
- def initialize(*args)
181
- super
182
- @supported = @metric.respond_to?('measure')
183
- end
184
-
185
- def run
186
- return unless @supported
187
-
188
- full_profile_options[:runs].to_i.times { run_test(@metric, :benchmark) }
189
- @total = @metric.total
190
- end
191
-
192
- def record
193
- avg = @metric.total / full_profile_options[:runs].to_i
194
- now = Time.now.utc.xmlschema
195
- with_output_file do |file|
196
- file.puts "#{avg},#{now},#{environment}"
197
- end
198
- end
199
-
200
- def environment
201
- unless defined? @env
202
- app = "#{$1}.#{$2}" if File.directory?('.git') && `git branch -v` =~ /^\* (\S+)\s+(\S+)/
203
-
204
- rails = Rails::VERSION::STRING
205
- if File.directory?('vendor/rails/.git')
206
- Dir.chdir('vendor/rails') do
207
- rails += ".#{$1}.#{$2}" if `git branch -v` =~ /^\* (\S+)\s+(\S+)/
208
- end
209
- end
210
-
211
- ruby = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
212
- ruby += "-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL}"
213
-
214
- @env = [app, rails, ruby, RUBY_PLATFORM] * ','
215
- end
216
-
217
- @env
218
- end
219
-
220
- protected
221
- HEADER = 'measurement,created_at,app,rails,ruby,platform'
222
-
223
- def with_output_file
224
- fname = output_filename
225
-
226
- if new = !File.exist?(fname)
227
- FileUtils.mkdir_p(File.dirname(fname))
228
- end
229
-
230
- File.open(fname, 'ab') do |file|
231
- file.puts(HEADER) if new
232
- yield file
233
- end
234
- end
235
-
236
- def output_filename
237
- "#{super}.csv"
238
- end
239
- end
240
-
241
- module Metrics
242
- def self.[](name)
243
- const_get(name.to_s.camelize)
244
- rescue NameError
245
- nil
246
- end
247
-
248
- class Base
249
- include ActionView::Helpers::NumberHelper
250
-
251
- attr_reader :total
252
-
253
- def initialize
254
- @total = 0
255
- end
256
-
257
- def name
258
- @name ||= self.class.name.demodulize.underscore
259
- end
260
-
261
- def benchmark
262
- with_gc_stats do
263
- before = measure
264
- yield
265
- @total += (measure - before)
266
- end
267
- end
268
-
269
- # overridden by each implementation
270
- def profile; end
271
-
272
- protected
273
- # overridden by each implementation
274
- def with_gc_stats; end
275
- end
276
-
277
- class Time < Base
278
- def measure
279
- ::Time.now.to_f
280
- end
281
-
282
- def format(measurement)
283
- if measurement < 1
284
- '%d ms' % (measurement * 1000)
285
- else
286
- '%.2f sec' % measurement
287
- end
288
- end
289
- end
290
-
291
- class Amount < Base
292
- def format(measurement)
293
- number_with_delimiter(measurement.floor)
294
- end
295
- end
296
-
297
- class DigitalInformationUnit < Base
298
- def format(measurement)
299
- number_to_human_size(measurement, :precision => 2)
300
- end
301
- end
302
-
303
- # each implementation provides its own metrics like ProcessTime, Memory or GcRuns
304
- end
305
- end
306
- end
307
- end
308
-
309
- RUBY_ENGINE = 'ruby' unless defined?(RUBY_ENGINE) # mri 1.8
310
- case RUBY_ENGINE
311
- when 'ruby' then require 'active_support/testing/performance/ruby'
312
- when 'rbx' then require 'active_support/testing/performance/rubinius'
313
- when 'jruby' then require 'active_support/testing/performance/jruby'
314
- else
315
- $stderr.puts 'Your ruby interpreter is not supported for benchmarking.'
316
- exit
317
- end
@@ -1,5 +0,0 @@
1
- module ActiveSupport
2
- autoload :Duration, 'active_support/duration'
3
- autoload :TimeWithZone, 'active_support/time_with_zone'
4
- autoload :TimeZone, 'active_support/values/time_zone'
5
- end
@@ -1,60 +0,0 @@
1
- # Extensions to +nil+ which allow for more helpful error messages for people who
2
- # are new to Rails.
3
- #
4
- # Ruby raises NoMethodError if you invoke a method on an object that does not
5
- # respond to it:
6
- #
7
- # $ ruby -e nil.destroy
8
- # -e:1: undefined method `destroy' for nil:NilClass (NoMethodError)
9
- #
10
- # With these extensions, if the method belongs to the public interface of the
11
- # classes in NilClass::WHINERS the error message suggests which could be the
12
- # actual intended class:
13
- #
14
- # $ rails runner nil.destroy
15
- # ...
16
- # You might have expected an instance of ActiveRecord::Base.
17
- # ...
18
- #
19
- # NilClass#id exists in Ruby 1.8 (though it is deprecated). Since +id+ is a fundamental
20
- # method of Active Record models NilClass#id is redefined as well to raise a RuntimeError
21
- # and warn the user. She probably wanted a model database identifier and the 4
22
- # returned by the original method could result in obscure bugs.
23
- #
24
- # The flag <tt>config.whiny_nils</tt> determines whether this feature is enabled.
25
- # By default it is on in development and test modes, and it is off in production
26
- # mode.
27
- class NilClass
28
- METHOD_CLASS_MAP = Hash.new
29
-
30
- def self.add_whiner(klass)
31
- methods = klass.public_instance_methods - public_instance_methods
32
- class_name = klass.name
33
- methods.each { |method| METHOD_CLASS_MAP[method.to_sym] = class_name }
34
- end
35
-
36
- add_whiner ::Array
37
-
38
- # Raises a RuntimeError when you attempt to call +id+ on +nil+.
39
- def id
40
- raise RuntimeError, "Called id for nil, which would mistakenly be #{object_id} -- if you really wanted the id of nil, use object_id", caller
41
- end
42
-
43
- private
44
- def method_missing(method, *args)
45
- if klass = METHOD_CLASS_MAP[method]
46
- raise_nil_warning_for klass, method, caller
47
- else
48
- super
49
- end
50
- end
51
-
52
- # Raises a NoMethodError when you attempt to call a method on +nil+.
53
- def raise_nil_warning_for(class_name = nil, selector = nil, with_caller = nil)
54
- message = "You have a nil object when you didn't expect it!"
55
- message << "\nYou might have expected an instance of #{class_name}." if class_name
56
- message << "\nThe error occurred while evaluating nil.#{selector}" if selector
57
-
58
- raise NoMethodError, message, with_caller || caller
59
- end
60
- end