Skip to content

Instantly share code, notes, and snippets.

View benoittgt's full-sized avatar
🏴

Benoit Tigeot benoittgt

🏴
View GitHub Profile
@benoittgt
benoittgt / display_switch_position_foostan_kicad.py
Created May 5, 2019 19:29
Display element position in Kicad with python script
# the script from the book was not working
import pcbnew
pcb = pcbnew.GetBoard()
for module in pcb.GetModules():
print "ref: %s, pos: %s" %(module.GetReference(), module.GetPosition(),)
@benoittgt
benoittgt / monitor_queries_active_record.rb
Created April 4, 2019 08:12
Monitor SQL queries made in Active Record
# From Juani Villarejo on Rails Speed Slack
class QueryReport
PRETTY_PRINT = <<-PRETTY_PRINT_TEMPLATE.freeze
Total queries executed: %s
Total execution time: %s
Queries executed
-----------------------
%s
@benoittgt
benoittgt / json_api_data_size_matcher.rb
Created March 11, 2019 09:34
JSONAPI data size matcher
# frozen_string_literal: true
RSpec::Matchers.define :have_data_count_of do |expected|
match do |actual|
if actual.is_a?(Hash)
@json_api_payload = actual
elsif actual.is_a?(String)
@json_api_payload = JSON.parse(actual)
else
@invalid_payload_type_error = 'Please provide a Hash or JSON string'
@benoittgt
benoittgt / doc_it.rb
Last active December 10, 2018 11:09
Display "it" in RSpec example documentation format output
require 'rspec/core/formatters/console_codes'
require 'rspec/core/formatters/documentation_formatter'
# Display "it" in RSpec doc format output
# $ rspec spec/foo/bar_spec.rb --format DocIt
class DocIt < RSpec::Core::Formatters::DocumentationFormatter
RSpec::Core::Formatters.register self,
:example_passed,
:example_pending,
@benoittgt
benoittgt / disctinct_value.md
Last active November 7, 2018 15:29
understand why distinct_value act differently
@benoittgt
benoittgt / understand_draper_and_active_record.md
Last active October 30, 2018 18:04
Understand why Draper gem instantiate soo many strings in ActiveRecord

Big memory consumption when processing 4000 lines of CSV

Status

When processing thousands of CSV lines of user that need to be or not added to a group (and produce a very detailed report). We are seiing we huge increase in memory. Often the 2X Heroku dyno cannot manage it.

memory profiler

We use stackprof, memory_profiler. We look at tool like speedscope for stackprof results and always it point at heavy use of ActiveRecord. Also we notice in strings retained in memory_profiler report on string very present.

@benoittgt
benoittgt / measure_ruby_process.rb
Last active October 27, 2018 10:06
Graph process consumption with psrecord
child_pid = spawn "psrecord #{Process.pid} --plot plot_#{Time.now.strftime('%H_%M_%S')}.png"
a = []
10_000_000.times { |i| a << i.to_s }
puts Process.kill('SIGINT', child_pid)
puts "continue scripting"
@benoittgt
benoittgt / lograge.rb
Created September 10, 2018 13:51
display halted_callback in lograge
# frozen_string_literal: true
Rails.application.configure do
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Logstash.new
config.lograge.custom_options = lambda do |event|
exceptions = %w[controller action format password]
{
uuid: event.payload[:uuid],
host: event.payload[:host],
# from https://twitter.com/mercier_remi/status/1021669831190433792
class CreateNotificationsWorker
include Sidekiq::Worker
def perform
users = User.all
users.each do |user|
tasks = user.tasks
tasks.each do |task|
if task.deadline && task.status == 'pending' && task.notified_user == false
@notification = Notification.create!(user_id: user.id, task_id: task.id)
@benoittgt
benoittgt / shop_ui_test.rb
Created June 20, 2018 15:49
Set cookie with capybara in Rails and ActionDispatch::IntegrationTest
# frozen_string_literal: true
require 'test_helper'
class ShopUiTest < ActionDispatch::IntegrationTest
test 'visit mobile_website' do
page.driver.browser.set_cookie('my_cookie_key=my_cookie_value')
@user = create_user
ui_log_in @user
visit mobile_website_path
end