Skip to content

Instantly share code, notes, and snippets.

@jrochkind
jrochkind / optimized_presigned_s3.rb
Created August 25, 2020 20:45
Optimized creation of S3 presigned_url in ruby
AWS_SIG4_SIGNER = Aws::Sigv4::Signer.new(
service: 's3',
region: AWS_CLIENT.config.region,
credentials_provider: SOME_AWS_CLIENT.config.credentials,
unsigned_headers: Aws::S3::Presigner::BLACKLISTED_HEADERS,
uri_escape_path: false
)
def naive_with_uri_escape_escaping(shrine_file)
# because URI.escape does NOT escape `/`, we don't need to split it,
@jrochkind
jrochkind / gist:f1510c7777de1c57e05950fc9ffd1aaa
Created August 25, 2020 20:42
ruby S3 public_url benchmark, SDK vs naive implementation
original AWS SDK public_url implementation 0.053043 0.000275 0.053318 ( 0.053782)
naive implementation 0.004730 0.000016 0.004746 ( 0.004760)
@jrochkind
jrochkind / naive_s3.rb
Created August 25, 2020 20:39
A naive ruby S3 public url generation
def naive_public_url(shrine_file)
"https://#{["#{shrine_file.storage.bucket.name}.s3.amazonaws.com", *shrine_file.storage.prefix, shrine_file.id].join('/')}"
end
naive_public_url(model.image)
#=> "https://somebucket.s3.amazonaws.com/path/to/image.jpg"
@jrochkind
jrochkind / naive_s3_rb
Created August 25, 2020 20:39
A naive ruby S3 public URL generation implementation
def naive_public_url(shrine_file)
"https://#{["#{shrine_file.storage.bucket.name}.s3.amazonaws.com", *shrine_file.storage.prefix, shrine_file.id].join('/')}"
end
naive_public_url(model.image)
#=> "https://somebucket.s3.amazonaws.com/path/to/image.jpg"
user system total real
sdk public_url 0.054114 0.000335 0.054449 ( 0.054802)
naive S3 public url 0.004575 0.000009 0.004584 ( 0.004582)
naive S3 public url with URI.escape 0.009892 0.000090 0.009982 ( 0.011209)
sdk presigned_url 0.756642 0.005855 0.762497 ( 0.789622)
re-use instantiated SDK Presigner 0.817595 0.005955 0.823550 ( 0.859270)
use inline instantiated Aws::Sigv4::Signer directly for presigned url (with escaping) 0.216338 0.001941 0.218279 ( 0.226991)
Re-use Aws::Sigv4::Signer for presigned url (with escaping) 0.185855 0.
@jrochkind
jrochkind / url_gen_benchmark.rb
Created August 25, 2020 20:12
Benchmarking different ways of creating presigned S3 URLs
# A local domain ActiveRecord, Asset#file is a shrine attachment
asset = Asset.last
# load outside of benchmarking
asset.file
s3_object = asset.file.storage.object(asset.file.id)
ITERATIONS = (ENV['ITERATIONS'] || "1200").to_i
$stderr.puts "#{ITERATIONS} iterations\n\n"
@jrochkind
jrochkind / s3_public_url_benchmark.rb
Created August 24, 2020 16:22
Comparing S3 public_url generation implementations for performance
# A local domain ActiveRecord, Asset#file is a shrine attachment
asset = Asset.last
asset.file # load outside of benchmarking
s3_object = asset.file.storage.object(asset.file.id) # Aws::S3::Object
require 'erb'
require 'escape_utils'
def naive_public_url(shrine_file)
expected Lu category: 1415
Lt (31)
Dž,Lj,Nj,Dz,ᾈ,ᾉ,ᾊ,ᾋ,ᾌ,ᾍ,ᾎ,ᾏ,ᾘ,ᾙ,ᾚ,ᾛ,ᾜ,ᾝ,ᾞ,ᾟ,ᾨ,ᾩ,ᾪ,ᾫ,ᾬ,ᾭ,ᾮ,ᾯ,ᾼ,ῌ,ῼ
no_categories_found (105)
Ϳ,Ԩ,Ԫ,Ԭ,Ԯ,Ᏽ,Ꚙ,Ꚛ,Ꞗ,Ꞙ,Ꞛ,Ꞝ,Ꞟ,Ɜ,Ɡ,Ɬ,Ʞ,Ʇ,Ʝ,Ꭓ,Ꞵ,Ꞷ,𐲀,𐲁,𐲂,𐲃,𐲄,𐲅,𐲆,𐲇,𐲈,𐲉,𐲊,𐲋,𐲌,𐲍,𐲎,𐲏,𐲐,𐲑,𐲒,𐲓,𐲔,𐲕,𐲖,𐲗,𐲘,𐲙,𐲚,𐲛,𐲜,𐲝,𐲞,𐲟,𐲠,𐲡,𐲢,𐲣,𐲤,𐲥,𐲦,𐲧,𐲨,𐲩,𐲪,𐲫,𐲬,𐲭,𐲮,𐲯,𐲰,𐲱,𐲲,𑢠,𑢡,𑢢,𑢣,𑢤,𑢥,𑢦,𑢧,𑢨,𑢩,𑢪,𑢫,𑢬,𑢭,𑢮,𑢯,𑢰,𑢱,𑢲,𑢳,𑢴,𑢵,𑢶,𑢷,𑢸,𑢹,𑢺,𑢻,𑢼,𑢽,𑢾,𑢿
Lo (85)
require 'twitter_cldr'
chars = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞĀĂĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨĪĬĮİIJĴĶĹĻĽĿŁŃŅŇŊŌŎŐŒŔŖŘŚŜŞŠŢŤŦŨŪŬŮŰŲŴŶŸŹŻŽƁƂƄƆƇƉƊƋƎƏƐƑƓƔƖƗƘƜƝƟƠƢƤƦƧƩƬƮƯƱƲƳƵƷƸƼDŽDžLJLjNJNjǍǏǑǓǕǗǙǛǞǠǢǤǦǨǪǬǮDZDzǴǶǷǸǺǼǾȀȂȄȆȈȊȌȎȐȒȔȖȘȚȜȞȠȢȤȦȨȪȬȮȰȲȺȻȽȾɁɃɄɅɆɈɊɌɎͰͲͶͿΆΈΉΊΌΎΏΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩΪΫϏϒϓϔϘϚϜϞϠϢϤϦϨϪϬϮϴϷϹϺϽϾϿЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯѠѢѤѦѨѪѬѮѰѲѴѶѸѺѼѾҀҊҌҎҐҒҔҖҘҚҜҞҠҢҤҦҨҪҬҮҰҲҴҶҸҺҼҾӀӁӃӅӇӉӋӍӐӒӔӖӘӚӜӞӠӢӤӦӨӪӬӮӰӲӴӶӸӺӼӾԀԂԄԆԈԊԌԎԐԒԔԖԘԚԜԞԠԢԤԦԨԪԬԮԱԲԳԴԵԶԷԸԹԺԻԼԽԾԿՀՁՂՃՄՅՆՇՈՉՊՋՌՍՎՏՐՑՒՓՔՕՖႠႡႢႣႤႥႦႧႨႩႪႫႬႭႮႯႰႱႲႳႴႵႶႷႸႹႺႻႼႽႾႿჀჁჂჃჄჅჇჍᎠᎡᎢᎣᎤᎥᎦᎧᎨᎩᎪᎫᎬᎭᎮᎯᎰᎱᎲᎳᎴᎵᎶᎷᎸᎹᎺᎻᎼᎽᎾᎿᏀᏁᏂᏃᏄᏅᏆᏇᏈᏉᏊᏋᏌᏍᏎᏏᏐᏑᏒᏓᏔᏕᏖᏗᏘᏙᏚᏛᏜᏝᏞᏟᏠᏡᏢᏣᏤᏥᏦᏧᏨᏩᏪᏫᏬᏭᏮᏯᏰᏱᏲᏳᏴᏵḀḂḄḆḈḊḌḎḐḒḔḖḘḚḜḞḠḢḤḦḨḪḬḮḰḲḴḶḸḺḼḾṀṂṄṆṈṊṌṎṐṒṔṖṘṚṜṞṠṢṤṦṨṪṬṮṰṲṴṶṸṺṼṾẀẂẄẆẈẊẌẎẐẒẔẞẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼẾỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪỬỮỰỲỴỶỸỺỼỾἈἉἊἋἌἍἎἏἘἙἚἛἜἝἨἩἪἫἬἭἮἯἸἹἺἻἼἽἾἿὈὉὊὋὌὍὙὛὝὟὨὩὪὫὬὭὮὯᾈᾉᾊᾋᾌᾍᾎᾏᾘᾙᾚᾛᾜᾝᾞᾟᾨᾩᾪᾫᾬᾭᾮᾯᾸᾹᾺΆᾼῈΈῊΉῌῘῙῚΊῨῩῪΎῬῸΌῺΏῼℂℇℋℌℍℐℑℒℕℙℚℛℜℝℤΩℨKÅℬℭℰℱℲℳℾℿⅅⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯↃⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⰀⰁⰂⰃⰄⰅⰆⰇⰈⰉⰊⰋⰌⰍⰎⰏⰐⰑⰒⰓⰔⰕⰖⰗⰘⰙⰚⰛⰜⰝⰞⰟⰠⰡⰢⰣⰤⰥⰦⰧⰨⰩⰪⰫⰬⰭⰮⱠⱢⱣⱤⱧⱩⱫⱭⱮⱯⱰⱲⱵⱾⱿⲀⲂⲄⲆⲈⲊⲌⲎⲐⲒⲔⲖⲘⲚⲜⲞⲠⲢⲤⲦⲨⲪⲬⲮⲰⲲⲴⲶⲸⲺⲼⲾⳀⳂⳄⳆⳈⳊⳌⳎⳐⳒⳔⳖⳘⳚⳜⳞⳠⳢⳫⳭⳲꙀꙂꙄꙆꙈ
RuntimeError: Cannot load `Rails.application.database_configuration`:
Could not load database configuration. No such file - ["config/database.yml"]
/Users/jrochkind/.gem/ruby/2.4.2/gems/railties-5.2.0/lib/rails/application/configuration.rb:185:in `database_configuration'
/Users/jrochkind/.gem/ruby/2.4.2/gems/activerecord-5.2.0/lib/active_record/railtie.rb:133:in `block (2 levels) in <class:Railtie>'
/Users/jrochkind/.gem/ruby/2.4.2/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:71:in `instance_eval'
/Users/jrochkind/.gem/ruby/2.4.2/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
/Users/jrochkind/.gem/ruby/2.4.2/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
/Users/jrochkind/.gem/ruby/2.4.2/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
/Users/jrochkind/.gem/ruby/2.4.2/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:52:in `block in run_loa