Skip to content

Instantly share code, notes, and snippets.

View koshigoe's full-sized avatar

Masataka SUZUKI koshigoe

View GitHub Profile
@koshigoe
koshigoe / benchmark-csv.rb
Last active November 12, 2018 06:16
Golang { CSV -> MessagePack } -> Ruby { MessagePack -> Array }
require 'csv'
CSV.foreach(ARGV[0]) do |_|
end
__END__
$ time ruby benchmark-csv.rb 100chr-100col-10000row.csv
237.94 real 227.03 user 1.65 sys
@koshigoe
koshigoe / benchmark-hash-vs-struct-vs-ar.rb
Created November 1, 2018 22:33
Benchmark: Hash vs Struct vs ActiveRecord
require 'benchmark'
require 'active_record'
# $ docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres:9.6
# $ psql -h localhost -U postgres -d postgres
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
encoding: 'unicode',
host: 'localhost',
database: 'postgres',
@koshigoe
koshigoe / benchmark-hash-vs-struct.rb
Created November 1, 2018 12:40
Benchmark: Hash vs Struct in Ruby
require 'benchmark'
keys = Array.new(100) { |i| i }
hash = Hash[keys.map(&:to_s).zip(keys)]
TYPE = Struct.new('Type', *keys.map(&:to_s))
struct = TYPE.new(*keys)
N = 100_000
Benchmark.bm(20) do |x|
x.report('Hash') do
@koshigoe
koshigoe / Gemfile
Created October 31, 2018 12:08
Benchmark: Bulk INSERT vs COPY in PostgreSQL
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'activerecord', '5.2.1'
gem 'pg', '1.1.3'
@koshigoe
koshigoe / 01_nested_instrument.rb
Last active August 16, 2018 11:17
Nested ActiveSupport::Notifications.instrument may cause memory leak
require 'active_support'
require 'active_support/subscriber'
class Subscriber < ActiveSupport::Subscriber
def event(event)
end
end
Subscriber.attach_to :sample
puts "### Not nested instrument\n\n"
require 'active_record'
require 'benchmark'
require 'csv'
require 'uri'
RECORD_SIZE = ENV.fetch('RECORD_SIZE', 10_000).to_i
BULK_SIZE = ENV.fetch('BULK_SIZE', 100).to_i
CONCURRENCY = ENV.fetch('CONCURRENCY', 4).to_i
dsn = URI.parse(ENV['DATABASE_URL'])
require 'net/ftp'
require 'tmpdir'
require 'thwait'
DEBUG = !ENV.fetch('DEBUG', '').empty?
CONCURRENT = ENV.fetch('CONCURRENT').to_i
FTP_HOST = ENV.fetch('FTP_HOST')
FTP_PORT = ENV.fetch('FTP_PORT').to_i
FTP_USER = ENV.fetch('FTP_USER')
FTP_PASS = ENV.fetch('FTP_PASS')
@koshigoe
koshigoe / README.md
Created May 10, 2018 22:40
Rails 5.1.6 と Rails 5.2.0 の新規アプリ間の差分確認
$ rails _5.1.6_ new -d postgresql --api --skip-test --skip-bundle --skip-git rails-api-5.1.6
$ rails _5.2.0_ new -d postgresql --api --skip-test --skip-bundle --skip-git rails-api-5.2.0
$ diff -ur rails-api-5.1.6/ rails-api-5.2.0/
@koshigoe
koshigoe / Dockerfile
Created January 24, 2018 00:06
Dockerfile to build Ruby trunk
# see https://github.com/docker-library/ruby/blob/e5d5ed23036c66c5797c39e0e156de5d006dbfc0/2.5/alpine3.7/Dockerfile
FROM alpine:3.7
# skip installing gem documentation
RUN mkdir -p /usr/local/etc \
&& { \
echo 'install: --no-document'; \
echo 'update: --no-document'; \
} >> /usr/local/etc/gemrc
@koshigoe
koshigoe / Dockerfile
Created December 28, 2017 12:48
stack level too deep
FROM alpine:3.7
# skip installing gem documentation
RUN mkdir -p /usr/local/etc \
&& { \
echo 'install: --no-document'; \
echo 'update: --no-document'; \
} >> /usr/local/etc/gemrc
ENV RUBY_MAJOR 2.5