Skip to content

Instantly share code, notes, and snippets.

@catmando
Last active March 17, 2021 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catmando/6a8037806f77aecf09dd550ac06387a6 to your computer and use it in GitHub Desktop.
Save catmando/6a8037806f77aecf09dd550ac06387a6 to your computer and use it in GitHub Desktop.
module Hyperstack
class InternalPolicy
def self.log_times(model, times, messages_sent, accumlated_broadcast_time)
times[:broadcast] = Time.now
return if times[:broadcast]-times[:start] < 0.0100 # adjust this as needed
::Rails.logger.warn "*******************BROADCASTING MODEL CHANGES**************************"
::Rails.logger.warn "Model: #{model.inspect}"
justification = 0
last_item_finished_at = times[:start]
times.collect { |event, timestamp| [timestamp, event] }
.sort { |a, b| a[0] <=> b[0] }
.collect do |finished, event|
justification = [justification, event.length].max
duration = finished - last_item_finished_at
last_item_finished_at = finished
[finished, event, duration]
end
.push([times[:broadcast], :total, (times[:broadcast] - times[:start])])
.each do |finished, event, duration|
::Rails.logger.warn "#{finished}: #{event.to_s.rjust(justification)} took: #{(duration * 1000).to_i.to_s.ljust(6)} MS"
end
::Rails.logger.warn "Actual broadcasting took: #{(accumlated_broadcast_time * 1000).to_i} MS "\
"for #{messages_sent} #{'message'.pluralize(messages_sent)}"
::Rails.logger.warn "***********************************************************************"
end
def self.regulate_broadcast(model, &block)
times = {start: Time.now}
internal_policy = InternalPolicy.new(
model, model.attribute_names, Connection.active
)
times[:initialized] = Time.now
ChannelBroadcastRegulation.broadcast(internal_policy)
times[:channels_calculated] = Time.now
InstanceBroadcastRegulation.broadcast(model, internal_policy)
times[:instances_calculated] = Time.now
accumulated_broadcast_time = 0
messages_sent = 0
internal_policy.broadcast do |*args|
start_time = Time.now
yield(*args)
accumulated_broadcast_time += Time.now-start_time
messages_sent += 1
end
log_times(model, times, messages_sent, accumulated_broadcast_time)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment