Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Adrien Rey-Jarthon jarthod

🏠
Working from home
View GitHub Profile
@jarthod
jarthod / mongo_spy.rb
Created Apr 16, 2021
Ruby module (rspec) to count and assert number of mongo queries
View mongo_spy.rb
# Examples:
#
# expect { code }.to change { finds("users") }.by(3)
# expect { code }.to change { updates("contents") }.by(1)
# expect { code }.not_to change { inserts }
#
# MongoSpy.flush
# ..code..
# expect(MongoSpy.queries).to match(
# "find" => { "users" => 1, "contents" => 1 },
@jarthod
jarthod / server_benchmark.md
Last active Apr 12, 2021
Server benchmark
View server_benchmark.md

Results

Server Price* CPU (1 thread) CPU (4 threads) IO
Scaleway Start1-XS
Atom C3955, 25G NVMe
$4 21 sec
P₉₅ = 2.1ms
21 sec
P₉₅ = 14ms
🚗 653 IO/s, 10 Mb/sec
P₉₅ = 0.40ms
Scaleway VC1-S
Atom C2750, 50G SSD
$4 46 sec ⚠️
P₉₅ = 4.6ms
23 sec
P₉₅ = 14ms
🚲 289 IO/s, 4.5 Mb/sec
P₉₅ = 0.39ms
Hetzner CPX11
AMD EPYC, 40G SSD
$5 20 sec
P₉₅ = 3.2ms
9.6 sec
P₉₅ = 11ms
✈️ 3656 IO/s, 57 Mb/sec
P₉₅ = 0.29ms
Hetzner CX21
Intel Xeon, 40G SSD
$6 24 sec
P₉₅ = 2.8ms
12.5 sec
P₉₅ = 11ms
🚤 1946 IO/s, 30 Mb/sec
P₉₅ = 0.97ms
AWS t2.micro 1G
*Intel E5-2676 v3 2.4
@jarthod
jarthod / ffmpeg_cheat_sheet.md
Last active Dec 5, 2020
ffmpeg cheat sheet
View ffmpeg_cheat_sheet.md

base encode to H265 (HEVC)

ffmpeg -i in.mts -c:a copy -c:v libx265 out.mp4

base encode to H264

ffmpeg -i in.mts -c:a copy -c:v h264 out.mp4

scale to 720p

@jarthod
jarthod / vat.rb
Last active Dec 5, 2020
EU VAT rates in ruby (2020) + online fetcher to verify/update rates.
View vat.rb
module VAT
RATES = {
"AT" => 20.0,
"BE" => 21.0,
"BG" => 20.0,
"CY" => 19.0,
"CZ" => 21.0,
"DE" => 19.0,
"DK" => 25.0,
"EE" => 20.0,
@jarthod
jarthod / set_nf_conntrack_max.sh
Last active Oct 25, 2020
Fix synology DS414 nf_conntrack: table full issue
View set_nf_conntrack_max.sh
sudo cat /proc/sys/net/netfilter/nf_conntrack_max
echo 500000 | sudo tee /proc/sys/net/netfilter/nf_conntrack_max
sudo cat /proc/sys/net/netfilter/nf_conntrack_max
@jarthod
jarthod / compressed.rb
Last active Jul 22, 2020 — forked from romanbsd/compressed.rb
Compressed fields in Mongoid 6.4
View compressed.rb
require 'zlib'
class CompressedString < String
def mongoize
BSON::Binary.new(Zlib::Deflate.deflate(self))
end
class << self
# Get the object as it was stored in the database, and instantiate
# this custom class from it.
@jarthod
jarthod / mongo-hang-monitoring.rb
Created Nov 8, 2019
Ruby script which tries to detect when mongo is up but super slow (for example due to IO issue) and stop it to allow fallback to secondary
View mongo-hang-monitoring.rb
#!/usr/bin/env ruby
# Tries to detect when mongo is up but super slow (ex: IO issue)
THRESHOLD = 20_000 # ms
def test_mongo tries: 5
out = `echo -e "db.isMaster()\ndb.getReplicationInfo()" | mongo mongodb://localhost/?socketTimeoutMS=#{THRESHOLD} 2>&1`
res = $?
if res != 0 && !out['Connection refused']
@jarthod
jarthod / process_semaphore.rb
Created Apr 17, 2019
Provides a system wide file lock to ensure no more than X process is running at the same time.
View process_semaphore.rb
#!/usr/bin/env ruby
# Provides a system wide file lock to ensure no more
# than X process is running at the same time.
# Example:
#
# process_semaphore!({
# prefix: "/tmp/my_process",
# limit: 2
@jarthod
jarthod / application_helper.rb
Last active Feb 14, 2016
Rails helper to render time using client's browser locale
View application_helper.rb
# Use this in place of `time_tag`
# Ex: <%= local_time_tag user.created_at %>
def local_time_tag time, opts = {}
opts[:data] = (opts[:data] || {}).merge format: 'local'
opts[:title] ||= time
time_tag(time, opts) + content_tag(:script, raw(<<-JAVASCRIPT))
var nodes = document.querySelectorAll('time[data-format=local]');
if (nodes.length > 0) {
var elem = nodes[nodes.length - 1];