Skip to content

Instantly share code, notes, and snippets.

Avatar

Janko Marohnić janko

View GitHub Profile
@janko
janko / results
Last active Mar 31, 2021
MiniMagick vs libvips benchmark (generating a 500x500 thumbnail)
View results
width, imagemagick, libvips
1000, 0.129, 0.045
1500, 0.190, 0.067
2000, 0.276, 0.054
2500, 0.380, 0.068
3000, 0.498, 0.085
@janko
janko / 01-activerecord.rb
Created May 27, 2015
PostgreSQL JSON querying in Sequel (my presentation from our local Ruby meetup)
View 01-activerecord.rb
require "active_record"
ActiveRecord::Base.establish_connection('postgres:///testing')
ActiveRecord::Migration.verbose = false
ActiveRecord::Migration.class_eval do
create_table :played_quizzes, force: true do |t|
t.integer :player_ids, array: true
t.json :quiz_snapshot
end
@janko
janko / 01-safe-download.rb
Last active Dec 20, 2020
A safe way in Ruby to download a file to disk using open-uri (with/without comments)
View 01-safe-download.rb
require "open-uri"
require "net/http"
Error = Class.new(StandardError)
DOWNLOAD_ERRORS = [
SocketError,
OpenURI::HTTPError,
RuntimeError,
URI::InvalidURIError,
View keybase.md

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@janko
janko / Gemfile
Last active Nov 1, 2020
Benchmark measuring execution time and memory allocation of different strategies of inserting many records in Sequel – 1a) individual insert, 1b) individual prepared insert, 2a) bulk insert, 2b) batched bulk insert, and 3) database insert
View Gemfile
source "https://rubygems.org"
gem "sequel"
gem "pg"
gem "sequel_pg"
gem "memory_profiler"
@janko
janko / 1-activerecord.rb
Last active Sep 25, 2020
INSERTing 50,000 records into a database in ActiveRecord, Arel, SQL, activerecord-import and Sequel.
View 1-activerecord.rb
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Migration.class_eval do
create_table(:records) do |t|
t.string :column
end
end
data = 50_000.times.map { |i| Hash[column: "Column #{i}"] }
@janko
janko / arel.rb
Created Jul 28, 2015
Insert statement in Arel
View arel.rb
require "active_record"
ActiveRecord::Base.establish_connection("postgres:///db")
insert = Arel::Nodes::InsertStatement.new
insert.relation = Arel::Table.new(:movies)
insert.columns = hash.keys.map { |k| Arel::Table.new(:movies)[k] }
insert.values = Arel::Nodes::Values.new(hash.values, insert.columns)
ActiveRecord::Base.connection.execute(insert.to_sql)
@janko
janko / benchmark.rb
Last active Sep 11, 2020
Comparing performance of Sequel and Active Record when fetching 1000 records
View benchmark.rb
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activerecord", "6.0.3.3", require: "active_record"
gem "sequel", "5.36"
gem "sequel_pg", "1.13.0", require: false
gem "pg", "1.2.3"
gem "mysql2", "0.5.3"
@janko
janko / Gemfile
Created May 31, 2018
Memory profiling of http.rb and other popular Ruby HTTP client libraries
View Gemfile
source "https://rubygems.org"
gem "roda"
gem "http", "~> 3.3"
gem "rest-client"
gem "httparty"
@janko
janko / script.rb
Last active Apr 30, 2020
Workaround for embedded documents in shrine-mongoid
View script.rb
require "mongoid"
require "shrine"
require "shrine/storage/memory"
require "stringio"
Mongoid.configure do |config|
config.clients.default = { hosts: ['localhost:27017'], database: 'my_db' }
config.log_level = :debug
end