Skip to content

Instantly share code, notes, and snippets.

View palkan's full-sized avatar
🗺️
RailsConf / RubyKaigi

Vladimir Dementyev palkan

🗺️
RailsConf / RubyKaigi
View GitHub Profile
@palkan
palkan / factory_doctor.rb
Created March 27, 2017 15:42
FactoryDoc: detect useless data generation in tests
module FactoryGirl
module Doctor
module FloatDuration
refine Float do
def duration
t = self
format("%02d:%02d.%03d", t / 60, t % 60, t.modulo(1) * 1000)
end
end
end
@palkan
palkan / Gemfile
Last active April 27, 2024 02:02
FactoryProf: profiler for your FactoryGirl
# if you want to render flamegraphs
gem "stackprof", require: false # required by flamegraph
gem "flamegraph", require: false
@palkan
palkan / Gemfile
Last active April 25, 2024 14:23
RSpec profiling with RubyProf and StackProf
gem 'stackprof', require: false
gem 'ruby-prof', require: false
@palkan
palkan / README.md
Created January 24, 2022 13:29
Rails boot time profiling

Add the following to application.rb:

$icallbacks = []
$icallbacks.define_singleton_method(:print) do
  puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a}" }.join("\n")
end

ActiveSupport::Notifications.subscribe("load_config_initializer.railties") do |event|
 $icallbacks << [event.payload[:initializer], event.duration]
@palkan
palkan / README.md
Last active April 16, 2024 04:14
ActionCable over SSE

Action Cable over SSE

This is a demo of leveraging the proposed Action Cable architecture to implement an SSE transport for Action Cable (without changing the user-space code, e.g., Connection and Channel classes).

Start the server by running the following command:

 ruby main.rb

Now, you can connect to Action Cable over SSE via cURL as follows:

# frozen_string_literal: true
require "benchmark_driver"
source = <<~RUBY
class Hash
def symbolize_keys
transform_keys { |key| key.to_sym rescue key }
end
@palkan
palkan / application_view_component.rb
Last active March 2, 2024 18:06
[ViewComponent] html_option
# From https://evilmartians.com/chronicles/viewcomponent-in-the-wild-embracing-tailwindcss-classes-and-html-attributes
class ApplicationViewComponent < ViewComponentContrib::Base
# Move default attrs to a constants so we can re-use it
EMPTY_ATTRS = {}.freeze
class << self
def html_option(name, default: nil, **opts)
if default
opts[:type] = proc { default.merge(_1) }
end
@palkan
palkan / 01_readme.md
Last active January 15, 2024 10:32
Docker Dev
@palkan
palkan / bulletify.rb
Created September 7, 2020 17:00
Bulletify: RSpec helpers to run Bullet in tests
RSpec.shared_context "bullet" do
before(:each) do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true # raise an error if N+1 query occurs
Bullet.start_request
end
after(:each) do
Bullet.perform_out_of_channel_notifications if Bullet.notification?
@palkan
palkan / patch.rb
Created November 14, 2023 02:42
TestProf vs. Wisper
# frozen_string_literal: true
# Patch Wisper to disable broadcasting by default
# (to speed up tests).
# Profile Wisper publishing via EventProf
TestProf::EventProf.monitor(Wisper::Publisher, "wisper.publisher.broadcast", :broadcast)
module Wisper
module Testing