Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / rack-request_stats.gemspec
Last active May 11, 2024 01:24
Rack request stats
#!/usr/bin/env ruby
%w(lib shared).each do |dir|
$LOAD_PATH.unshift(__dir__) unless $LOAD_PATH.include?(__dir__)
end
Gem::Specification.new do |spec|
spec.name = "rack-request_stats"
spec.version = "0.1.0"
spec.authors = ["Jamie Gaskins"]
@jgaskins
jgaskins / active_support-executor-example.rb
Last active May 1, 2024 22:36
ActiveSupport::Executor example
require "active_support/executor"
require "active_record"
require "pg"
ActiveRecord::Base.establish_connection(
uri: "postgres:///",
adapter: :postgresql,
pool: 5,
)
@jgaskins
jgaskins / 0.output.txt
Created March 9, 2024 23:12
Minimal Redis cache implementation in pure Ruby, outperforming Hiredis by 2-3x
$ ruby -v bench_redis.rb
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
KEY : bench-redis
VALUE SIZE: 102400
ITERATIONS: 10000
GET
Rehearsal -------------------------------------------
@jgaskins
jgaskins / Dockerfile
Created February 12, 2023 13:09
KeyDB with RedisJSON and RediSearch
FROM ubuntu:20.04 AS redisjson
RUN apt-get update
RUN apt-get upgrade -y git
RUN apt-get install -y curl gcc
RUN apt-get install -y libclang-dev
WORKDIR /
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o install-rust.sh && sh ./install-rust.sh -y
RUN git clone https://github.com/RedisJSON/RedisJSON.git --branch v2.4.4
@jgaskins
jgaskins / numeric_vs_uuid
Created January 17, 2014 01:38
Comparing numeric vs UUID in Postgres. Rehearsal benchmarks are thrown out. Only the second run is kept. Insertions are done all in one query to minimize the impact of I/O. This is harder to do with retrieval, but the ids are randomized in each one to minimize the effects of caching inside the database.
Inserting
user system total real
Numeric 0.070000 0.010000 0.080000 ( 0.586738)
UUID 0.070000 0.020000 0.090000 ( 3.101085)
Retrieving by id
user system total real
Numeric 0.810000 0.980000 1.790000 ( 6.831551)
UUID 0.830000 0.990000 1.820000 ( 6.981944)
@jgaskins
jgaskins / 000-provision.sh
Last active November 17, 2022 23:25
Running Mastodon on Kubernetes
#!/usr/bin/env bash
# Install nginx ingress controller
# NOTE: This uses DigitalOcean. If you use another Kubernetes provider,
# substitute the appropriate command from here: https://kubernetes.github.io/ingress-nginx/deploy/#cloud-deployments
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/do/deploy.yaml
# FOR DIGITALOCEAN DEPLOYMENTS:
export LB_HOSTNAME=example.com # Make this the DNS name that will point to your LB
export LB_NAME=my-lb # Give this a useful name to identify it on the DigitalOcean control panel
@jgaskins
jgaskins / patch-lb.sh
Created November 1, 2022 21:55
Give DigitalOcean load balancers provisioned for Kubernetes clusters a name and hostname so the Cert Manager HTTP solver will work
#!/usr/bin/env bash
# Fill out these two
export LB_HOSTNAME=example.com
export LB_NAME=my-lb
echo '{"metadata":{"annotations":{"service.beta.kubernetes.io/do-loadbalancer-hostname":"$LB_HOSTNAME","service.beta.kubernetes.io/do-loadbalancer-name":"$LB_NAME"}}}' |
envsubst |
awk "{ print \"'\" \$1 \"'\" }" |
xargs kubectl patch svc -n ingress-nginx ingress-nginx-controller -p
@jgaskins
jgaskins / benchmark.rb
Last active June 25, 2022 14:54
Benchmarking redis gem vs hiredis vs custom Redis client
require 'bundler/inline'
require 'socket'
class MyRedis
CRLF = "\r\n"
def initialize
@connection = TCPSocket.new('localhost', 6379)
@connection.sync = false
end
@jgaskins
jgaskins / require.cr
Created May 11, 2022 21:18
Project-local requires in Crystal
# Experiment with macros to allow for project-local requires
# https://forum.crystal-lang.org/t/project-relative-require/4617
{% begin %}
ROOT = "{{system("pwd").strip.id}}"
{% end %}
macro spec(file)
macro load_spec
\{% path = __DIR__.gsub(%r{\A{{ROOT.id}}}, "").gsub(%r{[^/]+}, "..").id %}
require "\{{path[1..]}}/spec/{{file.id}}"
@jgaskins
jgaskins / app.cr
Created January 18, 2021 05:31
Roda-like routing mixin for Crystal
require "http"
require "./route"
class App
include HTTP::Handler
include Route
def call(context)
route context do |r, response|