Repository navigation
Expand file tree
/
Copy pathapp.rb
More file actions
203 lines (160 loc) · 4.71 KB
/
Copy pathapp.rb
File metadata and controls
203 lines (160 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# frozen_string_literal: true
# rubocop:disable all
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __FILE__)
ENV['SECRET_KEY_BASE'] ||= 'secret_key_base'
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
require "rails"
require "global_id"
require "action_controller/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "redis"
require "anycable-rails"
require "rack/cors"
class TestApp < Rails::Application
secrets.secret_token = "secret_token"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
config.log_level = ENV.fetch("LOG_LEVEL", "info").to_sym
config.eager_load = true
config.filter_parameters << :token
config.hosts = []
config.consider_all_requests_local = true
routes.append do
mount ActionCable.server => "/cable"
post "/graphql", to: "graphql#execute"
end
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :patch, :put]
end
end
end
if ENV["DUMP"] == "1"
require "objspace"
ObjectSpace.trace_object_allocations_start
end
class ApplicationController < ActionController::API
end
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :id
def connect
self.id = SecureRandom.uuid
if request.params.key?("compact")
ts = Time.now
GC.compact
puts "GC.compact (#{Time.now - ts}s)"
end
if request.params.key?("dump")
puts "Generating heap dump..."
GC.start;GC.start;GC.start
if request.params["dump"] =~ /\bcompact\b/ || request.params.key?("compact")
GC.compact
else
GC.start;GC.start;GC.start
end
path = "tmp/heap-#{request.params["dump"]}-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}.json"
File.open(path, 'w') { |f|
ObjectSpace.dump_all(output: f)
}
puts "Done! File: #{path}"
end
end
end
end
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
Rails.logger = ActionCable.server.config.logger =
if ENV["LOG"] == "1"
Logger.new(STDOUT).tap { |logger| logger.level = ENV.fetch("LOG_LEVEL", "debug").to_sym }
else
Logger.new(IO::NULL).tap { |logger| logger.level = :fatal }
end
ActionCable.server.config.cable = { "adapter" => ENV.fetch("CABLE_ADAPTER", "redis") }
ActionCable.server.config.connection_class = -> { ApplicationCable::Connection }
ActionCable.server.config.disable_request_forgery_protection = true
class DemoChannel < ApplicationCable::Channel
def subscribed
stream_from "demo"
end
def broadcast
ActionCable.server.broadcast "demo", "hello"
end
def unsubscribed
sleep 1
end
end
class BenchmarkChannel < ApplicationCable::Channel
def subscribed
stream_from "all#{stream_id}"
end
def echo(data)
transmit data
end
def broadcast(data)
ActionCable.server.broadcast "all#{stream_id}", data
data["action"] = "broadcastResult"
transmit data
end
private
def stream_id
params[:id] || ""
end
end
if ENV["DEBUG_CHANNEL"] == "true"
require 'open3'
class DebugChannel < ApplicationCable::Channel
def eval(data)
res =
if data["ruby"]
Kernel.eval(data["ruby"])
elsif data["system"]
stdout_str, error_str, status = Open3.capture3(*data["system"].split(/\s+/))
if status.success?
stdout_str
else
raise "Failed: #{error_str}"
end
end
transmit({result: res})
rescue => e
transmit({error: e.message})
end
end
end
if ENV["OBJECT_TRACE"] == "1"
require_relative "./memprof"
WARMUP_COUNT = 200
TRACE_INTERVAL = (ENV['TRACE'] || 10).to_i
COUNTER = Concurrent::AtomicFixnum.new(0)
ACTIONCABLE_SOURCES = /\/(actioncable|active_support|action_dispatch|concurrent)/
WEBSOCKET_SOURCES = /\/websocket\-/
NIO4R_SOURCES = /\/nio4r/
def start_trace
puts "Start allocation tracing\n"
Memprof.start
end
def print_trace
puts "Print allocation tracing\nTotal clients connected after warmup: #{COUNTER.value - WARMUP_COUNT}"
Memprof.report(mapping: { nio4r: NIO4R_SOURCES, action_cable: ACTIONCABLE_SOURCES, websocket: WEBSOCKET_SOURCES, other: /.*/ }, ignore: [Thread])
end
ActionCable::Connection::Base.prepend(Module.new do
def respond_to_successful_request
res = super
COUNTER.increment
start_trace if COUNTER.value == WARMUP_COUNT
val = COUNTER.value
if val > WARMUP_COUNT && ((val - WARMUP_COUNT) % TRACE_INTERVAL) == 0
print_trace
COUNTER.value = 0
end
res
end
end)
end
require_relative "./graphql"