Skip to content

Instantly share code, notes, and snippets.

# lib/tasks/benchmark.rake
namespace :benchmark do
task run_sidekiq: :environment do
batch = Sidekiq::Batch.new
batch.on(:complete, TestWorker)
batch.jobs do
200.times do
TestWorker.perform_async
end
#!/usr/bin/env python3
import threading
import urllib.request
def worker(index, results):
req = urllib.request.urlopen('https://www.facebook.com/')
results.append(req.getcode())
results = []
#!/usr/bin/env ruby
require "benchmark"
require "httparty"
require "parallel"
# You need to modify your open file limit
# if you trying to run this on Mac OS
Benchmark.bmbm do |x|
x.report("Ruby Thread.new") do
require "heroku-api"
# heroku = Heroku::API.new(:api_key => API_KEY) # use API Key
# heroku = Heroku::API.new(:username => USERNAME, :password => PASSWORD) # use username and password
# heroku = Heroku::API.new(:headers => {'User-Agent' => 'custom'}) # use custom header
# NOTE: You can leave out the :api_key if ENV['HEROKU_API_KEY'] is set instead.
# https://github.com/heroku/heroku.rb
namespace :heroku do
@shikendon
shikendon / escape_unicode_control_characters.rb
Created April 24, 2016 04:54
轉譯 \t \r \n 以外的 Unicode 控制字元成 \uxxxx 的形式
class String
# Escape Unicode control characters except \t \r \n
def escape_unicode_control_characters
gsub(/(?<unicode_control_characters>[\p{C}&&[^\t\r\n]]+)/u) do |_match|
Regexp.last_match[:unicode_control_characters].unpack('U*').map { |i| '\u' + i.to_s(16).rjust(4, '0') }.join
end
end
end
require 'json'
@shikendon
shikendon / rijndael_128_cbc_encrypt.rb
Created April 7, 2016 04:51
RIJNDAEL 128 CBC (AES-256-CBC) encryption Ruby snippet
require 'openssl'
key = '12345678901234567890123456789012'
iv = '1234567890123456'
data = 'abcdefghijklmnop'
cipher = OpenSSL::Cipher::AES.new(256, :CBC)
cipher.encrypt
cipher.padding = 32
cipher.key = key
@shikendon
shikendon / rijndael_128_cbc_encrypt.php
Created April 7, 2016 04:48
RIJNDAEL 128 CBC encryption PHP snippet
<?php
function addpadding($string, $blocksize = 32) {
$len = strlen($string);
$pad = $blocksize - ($len % $blocksize);
$string .= str_repeat(chr($pad), $pad);
return $string;
}
$key = '12345678901234567890123456789012';
$post_data_str = 'abcdefghijklmnop';