cuboid 0.0.0 → 0.0.1alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -0
- data/Gemfile +20 -5
- data/LICENSE.md +22 -0
- data/README.md +158 -19
- data/Rakefile +56 -3
- data/config/paths.yml +15 -0
- data/cuboid.gemspec +61 -23
- data/lib/cuboid.rb +96 -4
- data/lib/cuboid/application.rb +326 -0
- data/lib/cuboid/application/parts/data.rb +18 -0
- data/lib/cuboid/application/parts/report.rb +29 -0
- data/lib/cuboid/application/parts/state.rb +274 -0
- data/lib/cuboid/application/runtime.rb +25 -0
- data/lib/cuboid/banner.rb +13 -0
- data/lib/cuboid/data.rb +86 -0
- data/lib/cuboid/data/application.rb +52 -0
- data/lib/cuboid/error.rb +9 -0
- data/lib/cuboid/option_group.rb +129 -0
- data/lib/cuboid/option_groups.rb +8 -0
- data/lib/cuboid/option_groups/datastore.rb +23 -0
- data/lib/cuboid/option_groups/dispatcher.rb +38 -0
- data/lib/cuboid/option_groups/output.rb +14 -0
- data/lib/cuboid/option_groups/paths.rb +184 -0
- data/lib/cuboid/option_groups/report.rb +39 -0
- data/lib/cuboid/option_groups/rpc.rb +105 -0
- data/lib/cuboid/option_groups/scheduler.rb +27 -0
- data/lib/cuboid/option_groups/snapshot.rb +13 -0
- data/lib/cuboid/option_groups/system.rb +10 -0
- data/lib/cuboid/options.rb +254 -0
- data/lib/cuboid/processes.rb +13 -0
- data/lib/cuboid/processes/dispatchers.rb +140 -0
- data/lib/cuboid/processes/executables/base.rb +54 -0
- data/lib/cuboid/processes/executables/dispatcher.rb +5 -0
- data/lib/cuboid/processes/executables/instance.rb +12 -0
- data/lib/cuboid/processes/executables/rest_service.rb +13 -0
- data/lib/cuboid/processes/executables/scheduler.rb +5 -0
- data/lib/cuboid/processes/helpers.rb +4 -0
- data/lib/cuboid/processes/helpers/dispatchers.rb +23 -0
- data/lib/cuboid/processes/helpers/instances.rb +39 -0
- data/lib/cuboid/processes/helpers/processes.rb +23 -0
- data/lib/cuboid/processes/helpers/schedulers.rb +23 -0
- data/lib/cuboid/processes/instances.rb +203 -0
- data/lib/cuboid/processes/manager.rb +262 -0
- data/lib/cuboid/processes/schedulers.rb +128 -0
- data/lib/cuboid/report.rb +220 -0
- data/lib/cuboid/rest/server.rb +165 -0
- data/lib/cuboid/rest/server/instance_helpers.rb +99 -0
- data/lib/cuboid/rest/server/routes/dispatcher.rb +41 -0
- data/lib/cuboid/rest/server/routes/grid.rb +41 -0
- data/lib/cuboid/rest/server/routes/instances.rb +131 -0
- data/lib/cuboid/rest/server/routes/scheduler.rb +140 -0
- data/lib/cuboid/rpc/client.rb +3 -0
- data/lib/cuboid/rpc/client/base.rb +58 -0
- data/lib/cuboid/rpc/client/dispatcher.rb +58 -0
- data/lib/cuboid/rpc/client/instance.rb +100 -0
- data/lib/cuboid/rpc/client/instance/service.rb +37 -0
- data/lib/cuboid/rpc/client/scheduler.rb +46 -0
- data/lib/cuboid/rpc/serializer.rb +92 -0
- data/lib/cuboid/rpc/server/active_options.rb +38 -0
- data/lib/cuboid/rpc/server/application_wrapper.rb +138 -0
- data/lib/cuboid/rpc/server/base.rb +63 -0
- data/lib/cuboid/rpc/server/dispatcher.rb +317 -0
- data/lib/cuboid/rpc/server/dispatcher/node.rb +247 -0
- data/lib/cuboid/rpc/server/dispatcher/service.rb +145 -0
- data/lib/cuboid/rpc/server/instance.rb +338 -0
- data/lib/cuboid/rpc/server/output.rb +92 -0
- data/lib/cuboid/rpc/server/scheduler.rb +482 -0
- data/lib/cuboid/ruby.rb +4 -0
- data/lib/cuboid/ruby/array.rb +17 -0
- data/lib/cuboid/ruby/hash.rb +41 -0
- data/lib/cuboid/ruby/object.rb +32 -0
- data/lib/cuboid/snapshot.rb +186 -0
- data/lib/cuboid/state.rb +94 -0
- data/lib/cuboid/state/application.rb +309 -0
- data/lib/cuboid/state/options.rb +27 -0
- data/lib/cuboid/support.rb +11 -0
- data/lib/cuboid/support/buffer.rb +3 -0
- data/lib/cuboid/support/buffer/autoflush.rb +61 -0
- data/lib/cuboid/support/buffer/base.rb +91 -0
- data/lib/cuboid/support/cache.rb +7 -0
- data/lib/cuboid/support/cache/base.rb +226 -0
- data/lib/cuboid/support/cache/least_cost_replacement.rb +77 -0
- data/lib/cuboid/support/cache/least_recently_pushed.rb +21 -0
- data/lib/cuboid/support/cache/least_recently_used.rb +31 -0
- data/lib/cuboid/support/cache/preference.rb +31 -0
- data/lib/cuboid/support/cache/random_replacement.rb +20 -0
- data/lib/cuboid/support/crypto.rb +2 -0
- data/lib/cuboid/support/crypto/rsa_aes_cbc.rb +86 -0
- data/lib/cuboid/support/database.rb +5 -0
- data/lib/cuboid/support/database/base.rb +177 -0
- data/lib/cuboid/support/database/categorized_queue.rb +195 -0
- data/lib/cuboid/support/database/hash.rb +300 -0
- data/lib/cuboid/support/database/queue.rb +149 -0
- data/lib/cuboid/support/filter.rb +3 -0
- data/lib/cuboid/support/filter/base.rb +110 -0
- data/lib/cuboid/support/filter/set.rb +29 -0
- data/lib/cuboid/support/glob.rb +27 -0
- data/lib/cuboid/support/mixins.rb +8 -0
- data/lib/cuboid/support/mixins/observable.rb +99 -0
- data/lib/cuboid/support/mixins/parts.rb +20 -0
- data/lib/cuboid/support/mixins/profiler.rb +93 -0
- data/lib/cuboid/support/mixins/spec_instances.rb +65 -0
- data/lib/cuboid/support/mixins/terminal.rb +57 -0
- data/lib/cuboid/system.rb +119 -0
- data/lib/cuboid/system/platforms.rb +84 -0
- data/lib/cuboid/system/platforms/linux.rb +26 -0
- data/lib/cuboid/system/platforms/mixins/unix.rb +46 -0
- data/lib/cuboid/system/platforms/osx.rb +25 -0
- data/lib/cuboid/system/platforms/windows.rb +81 -0
- data/lib/cuboid/system/slots.rb +143 -0
- data/lib/cuboid/ui/output.rb +52 -0
- data/lib/cuboid/ui/output_interface.rb +43 -0
- data/lib/cuboid/ui/output_interface/abstract.rb +68 -0
- data/lib/cuboid/ui/output_interface/controls.rb +84 -0
- data/lib/cuboid/ui/output_interface/error_logging.rb +119 -0
- data/lib/cuboid/ui/output_interface/implemented.rb +58 -0
- data/lib/cuboid/ui/output_interface/personalization.rb +62 -0
- data/lib/cuboid/utilities.rb +155 -0
- data/lib/cuboid/version.rb +4 -3
- data/lib/version +1 -0
- data/logs/placeholder +0 -0
- data/spec/cuboid/application/parts/data_spec.rb +12 -0
- data/spec/cuboid/application/parts/report_spec.rb +6 -0
- data/spec/cuboid/application/parts/state_spec.rb +192 -0
- data/spec/cuboid/application/runtime_spec.rb +21 -0
- data/spec/cuboid/application_spec.rb +37 -0
- data/spec/cuboid/data/application_spec.rb +22 -0
- data/spec/cuboid/data_spec.rb +47 -0
- data/spec/cuboid/error_spec.rb +23 -0
- data/spec/cuboid/option_groups/datastore_spec.rb +54 -0
- data/spec/cuboid/option_groups/dispatcher_spec.rb +12 -0
- data/spec/cuboid/option_groups/output_spec.rb +11 -0
- data/spec/cuboid/option_groups/paths_spec.rb +184 -0
- data/spec/cuboid/option_groups/report_spec.rb +26 -0
- data/spec/cuboid/option_groups/rpc_spec.rb +53 -0
- data/spec/cuboid/option_groups/snapshot_spec.rb +26 -0
- data/spec/cuboid/option_groups/system.rb +12 -0
- data/spec/cuboid/options_spec.rb +218 -0
- data/spec/cuboid/report_spec.rb +221 -0
- data/spec/cuboid/rest/server_spec.rb +1205 -0
- data/spec/cuboid/rpc/client/base_spec.rb +151 -0
- data/spec/cuboid/rpc/client/dispatcher_spec.rb +13 -0
- data/spec/cuboid/rpc/client/instance_spec.rb +38 -0
- data/spec/cuboid/rpc/server/active_options_spec.rb +21 -0
- data/spec/cuboid/rpc/server/base_spec.rb +60 -0
- data/spec/cuboid/rpc/server/dispatcher/node_spec.rb +222 -0
- data/spec/cuboid/rpc/server/dispatcher/service_spec.rb +112 -0
- data/spec/cuboid/rpc/server/dispatcher_spec.rb +317 -0
- data/spec/cuboid/rpc/server/instance_spec.rb +307 -0
- data/spec/cuboid/rpc/server/output_spec.rb +32 -0
- data/spec/cuboid/rpc/server/scheduler_spec.rb +400 -0
- data/spec/cuboid/ruby/array_spec.rb +77 -0
- data/spec/cuboid/ruby/hash_spec.rb +63 -0
- data/spec/cuboid/ruby/object_spec.rb +22 -0
- data/spec/cuboid/snapshot_spec.rb +123 -0
- data/spec/cuboid/state/application_spec.rb +538 -0
- data/spec/cuboid/state/options_spec.rb +37 -0
- data/spec/cuboid/state_spec.rb +53 -0
- data/spec/cuboid/support/buffer/autoflush_spec.rb +78 -0
- data/spec/cuboid/support/buffer/base_spec.rb +193 -0
- data/spec/cuboid/support/cache/least_cost_replacement_spec.rb +61 -0
- data/spec/cuboid/support/cache/least_recently_pushed_spec.rb +90 -0
- data/spec/cuboid/support/cache/least_recently_used_spec.rb +80 -0
- data/spec/cuboid/support/cache/preference_spec.rb +37 -0
- data/spec/cuboid/support/cache/random_replacement_spec.rb +42 -0
- data/spec/cuboid/support/crypto/rsa_aes_cbc_spec.rb +28 -0
- data/spec/cuboid/support/database/categorized_queue_spec.rb +327 -0
- data/spec/cuboid/support/database/hash_spec.rb +204 -0
- data/spec/cuboid/support/database/scheduler_spec.rb +325 -0
- data/spec/cuboid/support/filter/set_spec.rb +19 -0
- data/spec/cuboid/support/glob_spec.rb +75 -0
- data/spec/cuboid/support/mixins/observable_spec.rb +95 -0
- data/spec/cuboid/system/platforms/linux_spec.rb +31 -0
- data/spec/cuboid/system/platforms/osx_spec.rb +32 -0
- data/spec/cuboid/system/platforms/windows_spec.rb +41 -0
- data/spec/cuboid/system/slots_spec.rb +202 -0
- data/spec/cuboid/system_spec.rb +105 -0
- data/spec/cuboid/utilities_spec.rb +131 -0
- data/spec/spec_helper.rb +46 -0
- data/spec/support/factories/placeholder +0 -0
- data/spec/support/factories/scan_report.rb +18 -0
- data/spec/support/fixtures/empty/placeholder +0 -0
- data/spec/support/fixtures/executables/node.rb +50 -0
- data/spec/support/fixtures/mock_app.rb +61 -0
- data/spec/support/fixtures/mock_app/test_service.rb +64 -0
- data/spec/support/fixtures/services/echo.rb +64 -0
- data/spec/support/helpers/framework.rb +3 -0
- data/spec/support/helpers/matchers.rb +5 -0
- data/spec/support/helpers/misc.rb +3 -0
- data/spec/support/helpers/paths.rb +15 -0
- data/spec/support/helpers/request_helpers.rb +38 -0
- data/spec/support/helpers/requires.rb +8 -0
- data/spec/support/helpers/resets.rb +52 -0
- data/spec/support/helpers/web_server.rb +15 -0
- data/spec/support/lib/factory.rb +107 -0
- data/spec/support/lib/web_server_client.rb +41 -0
- data/spec/support/lib/web_server_dispatcher.rb +25 -0
- data/spec/support/lib/web_server_manager.rb +118 -0
- data/spec/support/logs/placeholder +0 -0
- data/spec/support/pems/cacert.pem +37 -0
- data/spec/support/pems/client/cert.pem +37 -0
- data/spec/support/pems/client/foo-cert.pem +39 -0
- data/spec/support/pems/client/foo-key.pem +51 -0
- data/spec/support/pems/client/key.pem +51 -0
- data/spec/support/pems/server/cert.pem +37 -0
- data/spec/support/pems/server/key.pem +51 -0
- data/spec/support/reports/placeholder +0 -0
- data/spec/support/shared/application.rb +10 -0
- data/spec/support/shared/component.rb +31 -0
- data/spec/support/shared/component/options/base.rb +187 -0
- data/spec/support/shared/option_group.rb +98 -0
- data/spec/support/shared/support/cache.rb +419 -0
- data/spec/support/shared/support/filter.rb +143 -0
- data/spec/support/shared/system/platforms/base.rb +25 -0
- data/spec/support/shared/system/platforms/mixins/unix.rb +37 -0
- data/spec/support/snapshots/placeholder +0 -0
- metadata +566 -21
- data/.gitignore +0 -8
- data/bin/console +0 -15
- data/bin/setup +0 -8
@@ -0,0 +1,110 @@
|
|
1
|
+
module Cuboid
|
2
|
+
module Support::Filter
|
3
|
+
|
4
|
+
# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
|
5
|
+
# @abstract
|
6
|
+
class Base
|
7
|
+
|
8
|
+
attr_reader :collection
|
9
|
+
|
10
|
+
DEFAULT_OPTIONS = {
|
11
|
+
hasher: :hash
|
12
|
+
}
|
13
|
+
|
14
|
+
# @param [Set] options
|
15
|
+
# @option options [Symbol] (:hasher)
|
16
|
+
# Method to call on the item to obtain its hash.
|
17
|
+
def initialize( options = {} )
|
18
|
+
@options = DEFAULT_OPTIONS.merge( options )
|
19
|
+
@hasher = @options[:hasher].to_sym
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param [#persistent_hash] item
|
23
|
+
# Item to insert.
|
24
|
+
#
|
25
|
+
# @return [Base]
|
26
|
+
# `self`
|
27
|
+
def <<( item )
|
28
|
+
@collection << calculate_hash( item )
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param [#persistent_hash] item
|
33
|
+
# Item to check.
|
34
|
+
#
|
35
|
+
# @return [Bool]
|
36
|
+
def include?( item )
|
37
|
+
@collection.include? calculate_hash( item )
|
38
|
+
end
|
39
|
+
|
40
|
+
def empty?
|
41
|
+
@collection.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
def any?
|
45
|
+
!empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
def size
|
49
|
+
@collection.size
|
50
|
+
end
|
51
|
+
|
52
|
+
def clear
|
53
|
+
@collection.clear
|
54
|
+
end
|
55
|
+
|
56
|
+
def merge( other )
|
57
|
+
case other
|
58
|
+
when self.class
|
59
|
+
|
60
|
+
@collection.merge other.collection
|
61
|
+
|
62
|
+
when Array
|
63
|
+
|
64
|
+
other.each do |k|
|
65
|
+
fail 'Cannot merge with unhashed entries' if !k.is_a?( Numeric )
|
66
|
+
@collection << k
|
67
|
+
end
|
68
|
+
|
69
|
+
else
|
70
|
+
fail ArgumentError,
|
71
|
+
"Don't know how to merge with: #{other.class}"
|
72
|
+
end
|
73
|
+
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
def ==( other )
|
78
|
+
hash == other.hash
|
79
|
+
end
|
80
|
+
|
81
|
+
def hash
|
82
|
+
@collection.hash
|
83
|
+
end
|
84
|
+
|
85
|
+
def dup
|
86
|
+
self.class.new( @options.dup ).merge self
|
87
|
+
end
|
88
|
+
|
89
|
+
def _dump( _ )
|
90
|
+
Marshal.dump( to_rpc_data )
|
91
|
+
end
|
92
|
+
|
93
|
+
def self._load( data )
|
94
|
+
from_rpc_data Marshal.load( data )
|
95
|
+
end
|
96
|
+
|
97
|
+
def collection=( c )
|
98
|
+
@collection = c
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def calculate_hash( item )
|
104
|
+
item.send @hasher
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Cuboid
|
4
|
+
module Support::Filter
|
5
|
+
|
6
|
+
# Filter based on a Set.
|
7
|
+
#
|
8
|
+
# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
|
9
|
+
class Set < Base
|
10
|
+
|
11
|
+
# @param (see Base#initialize)
|
12
|
+
def initialize(*)
|
13
|
+
super
|
14
|
+
@collection = ::Set.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_rpc_data
|
18
|
+
[@options, @collection.to_a]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.from_rpc_data( data )
|
22
|
+
options, items = data
|
23
|
+
new( options ).merge items
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Cuboid
|
2
|
+
module Support
|
3
|
+
|
4
|
+
# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
|
5
|
+
class Glob
|
6
|
+
|
7
|
+
def self.to_regexp( glob )
|
8
|
+
escaped = Regexp.escape( glob ).gsub( '\*', '.*?' )
|
9
|
+
Regexp.new( "^#{escaped}$", Regexp::IGNORECASE )
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :regexp
|
13
|
+
|
14
|
+
def initialize( glob )
|
15
|
+
@regexp = self.class.to_regexp( glob )
|
16
|
+
end
|
17
|
+
|
18
|
+
def =~( str )
|
19
|
+
@regexp.match? str
|
20
|
+
end
|
21
|
+
alias :matches? :=~
|
22
|
+
alias :match? :matches?
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module Cuboid
|
2
|
+
module Support
|
3
|
+
module Mixins
|
4
|
+
|
5
|
+
# Provides a flexible way to make any object observable for multiple events.
|
6
|
+
#
|
7
|
+
# The observable classes use:
|
8
|
+
#
|
9
|
+
# * `notify_<event>( *args )`
|
10
|
+
#
|
11
|
+
# to notify observers of events.
|
12
|
+
#
|
13
|
+
# The observers request notifications using:
|
14
|
+
#
|
15
|
+
# * `observable.<event>( &block )`
|
16
|
+
#
|
17
|
+
# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
|
18
|
+
module Observable
|
19
|
+
include UI::Output
|
20
|
+
include Utilities
|
21
|
+
|
22
|
+
include MonitorMixin
|
23
|
+
|
24
|
+
def self.included( base )
|
25
|
+
base.extend ClassMethods
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods
|
29
|
+
def advertise( *ad_events )
|
30
|
+
ad_events.each do |event|
|
31
|
+
define_method event do |&block|
|
32
|
+
add_observer( event, &block )
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method "notify_#{event}" do |*args|
|
36
|
+
notify_observers( event, *args )
|
37
|
+
end
|
38
|
+
|
39
|
+
private "notify_#{event}"
|
40
|
+
end
|
41
|
+
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def initialize
|
47
|
+
super
|
48
|
+
|
49
|
+
@__observers = {}
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def observers
|
55
|
+
@__observers
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_observer( event, &block )
|
59
|
+
fail ArgumentError, 'Missing block' if !block
|
60
|
+
synchronize do
|
61
|
+
observers_for( event ) << block
|
62
|
+
end
|
63
|
+
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
def notify_observers( event, *args )
|
68
|
+
synchronize do
|
69
|
+
observers_for( event ).each do |block|
|
70
|
+
exception_jail( false ) { block.call( *args ) }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
nil
|
75
|
+
end
|
76
|
+
|
77
|
+
def dup_observers
|
78
|
+
observers.inject({}) { |h, (k, v)| h[k] = v.dup; h }
|
79
|
+
end
|
80
|
+
|
81
|
+
def set_observers( obs )
|
82
|
+
@__observers = obs
|
83
|
+
end
|
84
|
+
|
85
|
+
def observers_for( event )
|
86
|
+
observers[event.to_sym] ||= []
|
87
|
+
end
|
88
|
+
|
89
|
+
def clear_observers
|
90
|
+
synchronize do
|
91
|
+
observers.clear
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Cuboid
|
2
|
+
module Support
|
3
|
+
module Mixins
|
4
|
+
|
5
|
+
module Parts
|
6
|
+
|
7
|
+
def self.included( base )
|
8
|
+
dir = Utilities.caller_path( 3 ).split( '.rb', 2 ).first
|
9
|
+
Dir.glob( "#{dir}/parts/**/*.rb" ).each { |f| require f }
|
10
|
+
|
11
|
+
parts = base.const_get( :Parts )
|
12
|
+
parts.constants.each do |part_name|
|
13
|
+
base.include parts.const_get( part_name )
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Cuboid
|
2
|
+
module Support
|
3
|
+
module Mixins
|
4
|
+
|
5
|
+
module Profiler
|
6
|
+
|
7
|
+
@@on = false
|
8
|
+
|
9
|
+
class <<self
|
10
|
+
|
11
|
+
def included( base )
|
12
|
+
base.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
def enable!
|
16
|
+
@@on = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def disable!
|
20
|
+
@@on = false
|
21
|
+
end
|
22
|
+
|
23
|
+
def on?
|
24
|
+
@@on
|
25
|
+
end
|
26
|
+
|
27
|
+
def results
|
28
|
+
h = {}
|
29
|
+
ObjectSpace.each_object( Class ) do |klass|
|
30
|
+
next if !klass.included_modules.include? self
|
31
|
+
next if klass.profile_data.empty?
|
32
|
+
|
33
|
+
h[klass] = {
|
34
|
+
sorted_total: klass.profile_data_total,
|
35
|
+
sorted_avg: klass.profile_data_avg
|
36
|
+
}
|
37
|
+
end
|
38
|
+
h
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
module ClassMethods
|
44
|
+
|
45
|
+
def profile_data
|
46
|
+
@profile ||= {}
|
47
|
+
end
|
48
|
+
|
49
|
+
def profile_data_avg
|
50
|
+
::Hash[self.profile_data.sort_by { |_, d| d[:avg] }.reverse]
|
51
|
+
end
|
52
|
+
|
53
|
+
def profile_data_total
|
54
|
+
::Hash[self.profile_data.sort_by { |_, d| d[:total] }.reverse]
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def profile_proc( *args, &block )
|
60
|
+
return block.call( *args ) if !Support::Mixins::Profiler.on?
|
61
|
+
|
62
|
+
profile_wrap_proc( &block ).call *args
|
63
|
+
end
|
64
|
+
|
65
|
+
def profile_wrap_proc( &block )
|
66
|
+
return block if !Support::Mixins::Profiler.on?
|
67
|
+
|
68
|
+
proc do |*args|
|
69
|
+
t = Time.now
|
70
|
+
r = block.call( *args )
|
71
|
+
|
72
|
+
loc = block.source_location.join( ':' )
|
73
|
+
loc.gsub!( Options.paths.root, '' )
|
74
|
+
|
75
|
+
data = self.class.profile_data[loc] ||= {
|
76
|
+
total: 0,
|
77
|
+
count: 0,
|
78
|
+
avg: 0
|
79
|
+
}
|
80
|
+
|
81
|
+
data[:total] += Time.now - t
|
82
|
+
data[:count] += 1
|
83
|
+
data[:avg] = data[:total] / data[:count]
|
84
|
+
|
85
|
+
r
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Cuboid
|
2
|
+
module Support
|
3
|
+
module Mixins
|
4
|
+
|
5
|
+
# @note Only for when running specs!
|
6
|
+
#
|
7
|
+
# Keeps track of every initialized instance so that they can be properly
|
8
|
+
# cleaned-up later.
|
9
|
+
module SpecInstances
|
10
|
+
|
11
|
+
def self.prepended( base )
|
12
|
+
base.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
# @abstract
|
18
|
+
def _spec_instance_cleanup( i )
|
19
|
+
fail 'Not implemented.'
|
20
|
+
end
|
21
|
+
|
22
|
+
def _spec_instances_cleanup
|
23
|
+
_spec_instances.each do |i|
|
24
|
+
_spec_instance_cleanup i
|
25
|
+
end
|
26
|
+
|
27
|
+
_spec_instances_clear
|
28
|
+
end
|
29
|
+
|
30
|
+
def _spec_instances_clear
|
31
|
+
_spec_instances.clear
|
32
|
+
end
|
33
|
+
|
34
|
+
def _spec_instance( instance )
|
35
|
+
return if !_spec_instances_collect?
|
36
|
+
_spec_instances << instance
|
37
|
+
end
|
38
|
+
|
39
|
+
def _spec_instances_collect!
|
40
|
+
@_spec_instances_collect = true
|
41
|
+
end
|
42
|
+
|
43
|
+
def _spec_instances_collect?
|
44
|
+
@_spec_instances_collect
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def _spec_instances
|
50
|
+
@_spec_instances ||= Concurrent::Array.new
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def initialize(*)
|
56
|
+
super
|
57
|
+
|
58
|
+
self.class._spec_instance self
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|