Skip to content

Instantly share code, notes, and snippets.

@jorgemanrubia
jorgemanrubia / measure.rb
Last active Jul 14, 2021
Script to measure and compare speed of running tests in parallel versus sequentially
View measure.rb
require "benchmark"
# https://github.com/rails/rails/pull/42761
class Execution
TEST_FILE_PATH = "test/threshold_test.rb"
SINGLE_TEST_DURATION = 0.1
attr_reader :threshold, :parallel
def initialize(threshold, parallel: false)
@jorgemanrubia
jorgemanrubia / count_from_table_stats.rb
Created Apr 22, 2021
Estimated count from table stats (MySQL)
View count_from_table_stats.rb
def count_from_table_stats(klass)
result = klass.connection.execute("show table status like '#{klass.table_name}'")
result.first[result.fields.index("Rows")]
end
View Disable reloading while tests are running.rb
module DisableReloaderWhileRunningTests
extend ActiveSupport::Concern
included do
setup do
@reloader_check = Rails.application.reloader.check
Rails.application.reloader.check = -> { false}
end
teardown do
@jorgemanrubia
jorgemanrubia / performance_helpers.rb
Last active May 1, 2020
Minitest helper to compare code with benchmark-ips
View performance_helpers.rb
require 'benchmark/ips'
module PerformanceHelpers
BENCHMARK_DURATION = 1
BENCHMARK_WARMUP = 1
BASELINE_LABEL = "Baseline"
CODE_TO_TEST_LABEL = "Code"
# Usage:
@jorgemanrubia
jorgemanrubia / enumerators_performance.rb
Last active May 4, 2017
Ruby enumerators performance
View enumerators_performance.rb
require 'benchmark/ips'
COUNT = 500000
Benchmark.ips do |x|
x.report('Enumerable') do
total = 0
COUNT.times do |i|
total += i
@jorgemanrubia
jorgemanrubia / converter.rb
Created Oct 4, 2015
Script to convert reading list in csv format
View converter.rb
require 'csv'
require 'date'
require 'ostruct'
require 'open-uri'
require 'fileutils'
class BooksConverter
OUTPUT_FOLDER = "output"
@jorgemanrubia
jorgemanrubia / discourse_users_merger.rb
Last active Dec 25, 2018
Script for merging existing discourse users
View discourse_users_merger.rb
module DiscourseUsersMerger
class Merger
def merge(target_username, source_username)
raise "User to merge and target user can't be the same" if target_username == source_username
target_user = User.find_by_username!(target_username)
source_user = User.find_by_username!(source_username)
puts "About to merge #{source_username} (#{source_user.email}) into #{target_username} (#{target_user.email})"
puts "#{source_user.topics.count} topics with #{source_user.posts.count} posts will be moved. Ok to continue? (y/n)"
continue = STDIN.gets.chomp.downcase == 'y'
@jorgemanrubia
jorgemanrubia / bundler.rb
Last active Dec 10, 2015
Exclude gems in heroku Cedar. Heroku will ignore :development and :test by default, but not other groups (such as :cucumber). Previous Bamboo stack let you define BUNDLE_WITHOUT, but it doesn't work anymore on Cedar.
View bundler.rb
# Put this code in some file and require it at the beginning of your Gemfile
# Then you can list the groups to exclude from heroku. E.g:
#
# exclude_in_heroku :cucumber, :development, :test
#
# Based on this post by Matt Campbell: http://soupmatt.com/fixing-bundlewithout-on-heroku-cedar,
# which is based in an idea by @grosser. All the credits to them.
module Bundler
class Dsl
old_group = instance_method(:group)
@jorgemanrubia
jorgemanrubia / gist:3012801
Created Jun 28, 2012
script for preparing the execution of the build pipeline of zendone
View gist:3012801
#!/bin/sh
script/killall
# Deletes trash files
rm -f tmp/*.oauth
rm -f log/*.log
rm -f db/schema.rb
rm -f tmp/evernote_session_data
rm -f rerun.txt