Skip to content

Instantly share code, notes, and snippets.

@benweint
benweint / go.mod
Created December 12, 2023 06:07
Go maps vs. SQLite for very simple ops
module github.com/benweint/sqlite-demo
go 1.20
require github.com/mattn/go-sqlite3 v1.14.18 // indirect
@benweint
benweint / package.json
Created December 4, 2023 18:45
pnpm signal handling issue repro case
{
"name": "pnpm-signals",
"private": true,
"version": "1.0.0",
"description": "",
"scripts": {
"dev": "node signals.js"
},
"author": "",
"license": "ISC"
@benweint
benweint / poolsim.py
Created March 11, 2023 06:02
Connection pool metastability simulation
#!/usr/bin/env python
import simpy
import random
import math
import pandas
import matplotlib
import matplotlib.pyplot as plt
# Represents a shared database instance with a fixed CPU allocation.
@benweint
benweint / gdb-stuck-ruby.txt
Created April 16, 2013 14:55
An example of how to gather C and Ruby backtraces from a stuck Ruby process using gdb.
# Here's the script I'll use to demonstrate - it just loops forever:
$ cat test.rb
#!/usr/bin/env ruby
loop do
sleep 1
end
# Now, I'll start the script in the background, and redirect stdout and stderr
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCu9eMRYwWS5Z1bLjpfPfKzuvQhKOhGX0Q7KLPOf3rkgsyZ8mJGGnKMAjpDFeH7MIjwQ1au9Gp3tc+ENbnyjHhtRDUkajTjU6odq2DYMUuGJo5JJTbUFPOrHcTn5kgDpc4a9XTT+7EHS9gC8SjGzsxqTMf1WafoaKrDH2WS3vdo91MMmsmDJorro0FjWcN1gkGlXkqqxdUuiQjY19NAEetCyoMOxnmjFI4qFF7U4L1ynHVUfGyE0XOUru1YgAOYvncNoSShkFqoln7fSPE6lLz1GBt6e5KDOhpVSRU/S5FLmFBGUg8ajG7UzokFE1QE2Oqg8KJTAF42gr5xRMIkH/D ben@Bens-MacBook-Pro.local
@benweint
benweint / sidekiq-sampler.rb
Created April 10, 2015 19:55
Recording Sidekiq job counts by job class and queue name to New Relic Insights
require 'sidekiq/api'
if File.basename($0) == 'sidekiq'
Thread.new do
loop do
begin
t0 = Time.now
qcount = 0
njobs = 0
@benweint
benweint / as-note-thread-safety-demo.rb
Last active April 22, 2017 15:28
This is a demo of a thread-safety issue with ActiveSupport::Notifications for Rails issue https://github.com/rails/rails/issues/12069
#!/usr/bin/env ruby
#
# When run against rails/rails commit 91684fb193a91671d682701cc1357e7b4b3fbe2b,
# this code produces the following output on my machine:
#
# long start @ 0.000
# short start @ 0.510
# short finish @ 1.511
# short reported 1.511186 s
@benweint
benweint / threads-across-fork.rb
Last active May 12, 2016 08:28
threads across fork Ruby demo
#!/usr/bin/env ruby
# Pass 'fork' as the first argument to use Process.fork, otherwise Process.daemon is used.
do_fork = (ARGV[0] == 'fork')
thread = Thread.new do
loop { sleep(1) }
end
@benweint
benweint / string-subclass-encodings.rb
Created January 21, 2014 22:53
String subclasses and encodings in Rubinius
#!/usr/bin/env ruby
class BadString < String; end
original = (0..255).to_a.pack("C*").force_encoding("UTF-8")
subclass = BadString.new(original)
round_tripped = subclass.to_s
puts "encoding: original = #{original.encoding}, subclass = #{subclass.encoding}, round_tripped = #{round_tripped.encoding}"
puts "valid_encoding? original = #{original.valid_encoding?}, subclass = #{subclass.valid_encoding?}, round_tripped = #{round_tripped.valid_encoding?}"
@benweint
benweint / array-racc.rb
Created January 21, 2014 00:10
Array / racc / rbx bug demo
#!/usr/bin/env ruby
string = "\n\v"
puts "before racc require: #{Array(string).inspect}"
require 'racc'
puts "after racc require: #{Array(string).inspect}"