Skip to content

Instantly share code, notes, and snippets.

View kuntoaji's full-sized avatar
🎯
Focusing

KAK kuntoaji

🎯
Focusing
View GitHub Profile
// Tracking cursor position in real-time without JavaScript
// Demo: https://twitter.com/davywtf/status/1124146339259002881
package main
import (
"fmt"
"net/http"
"strings"
)
@kuntoaji
kuntoaji / 1-activerecord.rb
Created November 21, 2018 03:40 — forked from janko/1-activerecord.rb
INSERTing 50,000 records into a database in ActiveRecord, Arel, SQL, activerecord-import and Sequel.
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Migration.class_eval do
create_table(:records) do |t|
t.string :column
end
end
data = 50_000.times.map { |i| Hash[column: "Column #{i}"] }
@kuntoaji
kuntoaji / encryptor.rb
Created October 19, 2018 02:50 — forked from aloucas/encryptor.rb
How to create dynamic attr_accessors (getters/setters) for encrypted attributes in a Ruby on Rails 5 Model.
# lib/encryptor.rb
# Module to dynamic encrypt attributes per model
module Encryptor
def has_encrypted_attributes(*attrs)
attrs.each do |attr|
# Define the dynamic getter
define_method(attr) do
self[attr].present? ? Encryptor.crypt.decrypt_and_verify(self[attr]) : self[attr]
end
# Define the dynamic setter
activerecord-postgresql-4-2-4 sequel-postgresql-2-26-0 Benchmark
4.305316 0.307019 Eager Loading Query Per Association With 1-1 Records: 640 objects 22 times-No Transaction
4.281943 0.335119 Eager Loading Query Per Association With 1-1 Records: 640 objects 22 times-Transaction
1.229413 0.185485 Eager Loading Query Per Association With 1-32 Records: 1024 objects 22 times-No Transaction
1.226506 0.200746 Eager Loading Query Per Association With 1-32 Records: 1024 objects 22 times-Transaction
0.900579 0.146086 Eager Loading Query Per Association With 1-32-32 Records: 2048 objects 9 times-No Transaction
0.951023 0.146339 Eager Loading Query Per Association With 1-32-32 Records: 2048 objects 9 times-Transaction
4.925186 0.391609 Eager Loading Single Query With 1-1 Records: 640 objects 22 times-No Transaction
4.936297 0.420436 Eager Loading Single Query With 1-1 Records: 640 objects 22 times-Transaction
1.871573 0.417916 Eager Loading Single Query With 1-32 Records: 1024 objects 22 times-No Transaction
@kuntoaji
kuntoaji / deploy.rb
Created August 8, 2015 17:57
Discourse Configuration
# config/deploy.rb
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv'
set :term_mode, nil
set :rails_env, 'production'

This all applies to Ruby 2.1. In some cases a setting is not available in 2.0, this is noted. There is also a different with 1.9, 1.8, and REE --- these are not noted.

All the relevant code is in https://github.com/ruby/ruby/blob/trunk/gc.c

RUBY_HEAP_MIN_SLOTS

default: 10000

The number of heap slots to start out with. This should be set high enough so that your app has enough or almost enough memory after loading so that it doesn't have to allocate more memory on the first request (althogh this probably isn't such a big deal for most apps).

(todo: figure out how big a slot is. i think the answer can be infered from this code.)

#!/usr/bin/env ruby
require 'open-uri'
user_agent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"
saved_url_list = "saved_list.txt"
accessed_urls = []
accessed_urls = File.readlines(saved_url_list) if File.exists?(saved_url_list)
# tps terbanyak 149
@kuntoaji
kuntoaji / nginx.conf
Last active August 29, 2015 13:55
Konfigurasi Nginx Static Assets Files
# Nginx default configuration
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
<!DOCTYPE html>
<html>
<head>
<title>CSS jQuery Modal Overlay</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="main.js" type="text/javascript"></script>
<link href="main.css" media="all" rel="stylesheet" type="text/css">
</head>
<body>
<div id="overlay"></div>
@kuntoaji
kuntoaji / nginx.conf
Last active August 29, 2015 13:55
Nginx Load Balancer Configuration
# default nginx configuration
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;