Skip to content

Instantly share code, notes, and snippets.

Avatar

Leandro Proença leandronsp

View GitHub Profile
@leandronsp
leandronsp / add-index.sql
Created Jun 11, 2022
Comparing B-Tree index vs CTE's
View add-index.sql
DROP INDEX IF EXISTS transfers_in, transfers_out;
CREATE INDEX transfers_in ON transfers (target_account_id);
CREATE INDEX transfers_out ON transfers (source_account_id);
@leandronsp
leandronsp / ruby101.rb
Created Jun 4, 2022
Entendendo attr_accessor, writer e reader no Ruby
View ruby101.rb
class Dog
def name
# retorna variável de instância
@name
end
end
doguinho = Dog.new
# Chamando método de instância
@leandronsp
leandronsp / server.rb
Created Apr 6, 2022
Web Login/Logout in Ruby, using TCP socket, in less than 110 lines
View server.rb
require 'socket'
require 'cgi'
PORT = 3000
socket = TCPServer.new('0.0.0.0', PORT)
puts "Listening to the port #{PORT}..."
loop do
# Wait for a new TCP connection..."
@leandronsp
leandronsp / cractor.rb
Created Jan 17, 2022
A dead simple Actor in Ruby, simulating the same API for Ractors
View cractor.rb
class Cractor
def initialize(*args, &block)
@inbox = Queue.new
@outbox = Queue.new
Thread.new do
result = yield(self, *args)
self.yield(result)
end
@leandronsp
leandronsp / palindromesX.js
Last active Jan 4, 2022
Palindromes of X random words
View palindromesX.js
/*
Reverse a word
*/
const reverse = (word) => {
const letters = word.split('')
const lastIdx = letters.length - 1
return letters
.map((letter, idx) => { return letters[lastIdx - idx] })
.join('')
@leandronsp
leandronsp / benchmark-web-concurrency
Last active Dec 23, 2021
Ruby web server - concurrency benchmark
View benchmark-web-concurrency
ab -n 20 -c 5
Req per seconds Time per req (ms)
Threads – IO 232 21
Fibers – IO 450 11
Threads – External Req 1.85 2703
Fibers – External Req 7.97 627
@leandronsp
leandronsp / main.rb
Created Dec 4, 2021
How to reduce time complexity of nested loops
View main.rb
require 'faker'
require 'benchmark'
def generate_groups
(1..1_000).map do |id|
{ id: id, name: Faker::Educator.primary_school }
end
end
def generate_users(groups_ids)
@leandronsp
leandronsp / sum_range.rb
Created Dec 4, 2021
Sum all numbers in a consecutive range
View sum_range.rb
require 'benchmark'
def sum_range_using_for_loop(min, max)
range = (min..max)
sum = 0
for n in range
sum = sum + n
end
@leandronsp
leandronsp / README.md
Created Oct 23, 2021
NGINX on Kubernetes
View README.md

Create the pod on the cluster

kubectl apply -f nginx-pod.yaml
@leandronsp
leandronsp / Pulumi.dev.yaml
Last active Oct 23, 2021
Provisioning a Kubernetes cluster on GKE using Pulumi
View Pulumi.dev.yaml
config:
gcp:project: "my-project-created-on-gcp"