Skip to content

Instantly share code, notes, and snippets.

@ohaddahan
ohaddahan / benchmark.rb
Created October 2, 2019 20:21
Algorithm Optimisation: A Real Example - My insights
#! /usr/bin/env ruby
require 'fileutils'
require 'benchmark/ips'
DATA_DIR = "tmp_dir"
FileUtils.mkdir_p DATA_DIR
FILE_SIZES = [
100000,
1000000,
10000000,
100000000
@accounts_users = Account.find_by(id: account_id).accounts_users ; nil
# Total New Volunteer's Visa - all new users from "AccountUser" table :
@total_new_users = @accounts_users.where('created_at > ?', from).where('created_at < ?', to)
# Toal New Volunteer Registered - sign-in > 0 from previous new volunteer visa
@toal_new_volunteer_registered = @accounts_users.joins(:user).where('sign_in_count > 0')
@total_new_volunteers_visa = @accounts_users.where('visa_start > ?', from)
# Total Visa Ended - all visa_expiry from account_users table
@total_visa_ended = @accounts_users.where('visa_end > ?', from).where('visa_end < ?', to)
# The "Table" of follow-up needed come from "activities_follow_ups" table, and it will present all feedback analyzed for the month before the last month for"overdue" feedbacks only.
/*global chrome*/
import React from 'react';
//import logo from './logo.svg';
import './App.css';
import Dashboard from "./components/Dashboard";
export default class App extends React.Component {
componentWillMount() {
chrome.runtime.onMessage.addListener(
require 'memory_profiler'
SIZE = 10_000
def dump()
IO.pipe { |r, w| Marshal.dump('a' * SIZE, w) }
end
def write()
IO.pipe { |r, w| w.write( Marshal.dump('a' * SIZE) ) }
end
require 'securerandom'
require 'benchmark/ips'
require 'active_support/cache'
def bm(description, value)
streaming = ActiveSupport::Cache::StreamingCompressor
puts "#{description} dump (#{value.bytesize / 1.kilobyte}kb)"
Benchmark.ips do |x|
Warming up --------------------------------------
dump : 136.247k i/100ms
dump2 : 209.935k i/100ms
Calculating -------------------------------------
dump : 2.095M (± 3.3%) i/s - 10.491M in 5.013742s
dump2 : 4.570M (± 5.9%) i/s - 22.883M in 5.032849s
Comparison:
dump2 :: 4570234.5 i/s
dump :: 2094819.0 i/s - 2.18x slower
@ohaddahan
ohaddahan / json_aggregate_helper.rb
Last active September 22, 2018 08:53
Fetch raw JSON from PostgreSQL
module JsonAggregateHelper
def to_json_agg_qeury(input, raw=false)
return "select json_agg(t) from (#{input}) t;" if raw
[ActiveRecord::Relation, ActiveRecord::AssociationRelation].each do |type|
return "select json_agg(t) from (#{input.to_sql}) t;" if input.class.ancestors.include?(type)
end
raise ArgumentError.new("#{__method__} input class '#{input.class.to_s}' is the wrong type")
end
@ohaddahan
ohaddahan / CheckJEMalloc.rb
Last active March 14, 2019 10:43
Checking if jemalloc is present
module CheckJEMalloc
require 'ffi'
extend FFI::Library
def self.je_malloc_exists?(jemalloc_so = nil)
if !jemalloc_so.nil? && !jemalloc_so.is_a?(String)
puts "ArgumentError : Only strings allowed as arguments"
return false
end
jemalloc_so = ENV.fetch('LD_PRELOAD','') if jemalloc_so.nil?