Skip to content

Instantly share code, notes, and snippets.

View kbrock's full-sized avatar

Keenan Brock kbrock

View GitHub Profile
create index i_hosts_i_h on hosts(name, id);
create index i_hosts_h_i on hosts(id, name);
explain analyze verbose
SELECT "vms".id FROM "vms" left join "hosts" on "hosts"."id" = "vms"."host_id" ORDER BY "hosts"."name"
;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Sort (cost=440.76..448.26 rows=3000 width=36) (actual time=30.724..30.884 rows=3000 loops=1)
Output: vms.id, hosts.name
@kbrock
kbrock / benchmark.rb
Created April 21, 2016 00:51
Eval vs defined? for a class
require 'benchmark/ips'
def eval_eval_true
name = "Benchmark"
eval("defined?(#{name}) && #{name}.class").to_s.eql?('Module')
end
def eval_eval_false
name = "XABCDE"
eval("defined?(#{name}) && #{name}.class").to_s.eql?('Module')
@kbrock
kbrock / 00_stats.md
Created March 1, 2017 15:57
Aggregates comes together
ms bytes objects queries query (ms) rows comments
290.5 21,380,122* 225,801 2 28.0 1,598 before
244.9 18,410,393* 188,824 1 4.6 after

* Memory usage does not reflect 262 freed objects. (for both of them)

@kbrock
kbrock / transaction_fix_repro.rb
Last active March 15, 2017 21:10
Reproduce rails transaction timeout error
#!/usr/bin/env ruby
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
@kbrock
kbrock / coordinator.rb
Last active June 8, 2017 02:08
multi threaded example
#!/usr/bin/env ruby
require 'thread'
COLLECTOR_COUNT = 2 # define to run in separate threads. comment to run inline
class Db
def initialize
@mutex = Mutex.new
@data = {}
end
def []=(n, v) ; @mutex.synchronize { @data[n] = v } ; end
<html>
<head><style>
table {border-collapse:collapse;}
table, td, th { border:1px solid black; }
.f0, a.f0 { color: #999; }
.f1, a.f1 { color: #ccc; }
</style>
</head>
<body>
<h2>!</h2>
@kbrock
kbrock / timing.rb
Created June 21, 2017 15:10
Metrics capture timing
def reload_models
[Vm, Host, Storage].each { |m| m.update_all(:last_perf_capture_on => 50.minutes.ago) } # <== still tweaking
[MiqTask, MiqQueue, Metric, MetricRollup, VimPerformanceState].each { |m| m.truncate}
ActiveRecord::Base.connection.execute("vacuum full")
Module.clear_all_cache_with_timeout
[[MiqProductFeature, :@feature_cache], [MiqProductFeature, :@obj_cache], [BottleneckEvent, :@event_definitions],
[Tenant, :@root_tenant]].each { |i, ivar| i.instance_variable_set(ivar, nil) }
MiqServer.my_server ; MiqServer.my_zone
[ExtManagementSystem, Host, EmsCluster, Tagging, Tag, HostStorage, Storage, Vm, MiqRegion, MiqTask, Relationship, Disk,
Endpoint, Authentication, ServerRole, MiqWorker, MiqQueue, MetricRollup, BottleneckEvent].each { |m| m.first }
@kbrock
kbrock / answer.md
Last active August 29, 2017 13:44 — forked from aishfenton/serializer_benchmarks.rb
Performance comparison of different ruby serializer methods
gem
Json oj 40.4 i/s
MessagePack 38.3 i/s same
binary json 22.0 i/s 1.84x slower
Json yajl 19.0 i/s 2.13x slower
ruby json 15.6 i/s 2.59x slower
ruby marshal 13.9 i/s 2.90x slower
ruby yaml 0.4 i/s 111.99x slower
Yaml Psych 0.4 i/s 112.09x slower
@kbrock
kbrock / 0README.md
Created December 11, 2017 17:03
Comparing

Looking at the difference between using ActiveSupport::Notification, and rolling our own metrics counters.

When logging, Notifications are 66% slower than manually adding the metrics code. The metrics code is actually 50% slower when not logging the results (no subscriber). Having no subscriber does speed up performance for all the cases.

The difference for timing for payload is probably due to allocations. Having a fixed payload ended up being equivalent to the no payload case. This is a reminder for us to pay attention to what we pass in the payload.

@kbrock
kbrock / _results.md
Last active April 26, 2018 18:51
Comparing performance implementation of determining whether a field is an integer (for ancestry

note: in the tests, we are setting a different variable @x than the guard clause @x2. I tested a terciary ?: vs the guard clause return if defined?() and they were about the same for pass and fail case

test int i/s slower text i/s slower
eq2 6,315,053.7 i/s same 6,323,409.7 i/s base
neq2 6,281,137.2 i/s same 3,707,761.0 i/s 1.71x
eq 5,978,924.2 i/s same 4,572,438.8 i/s 1.38x
include 4,368,299.8 i/s 1.45x 4,349,531.4 i/s 1.45x
neq 4,194,756.5 i/s 1.51x 4,170,358.7 i/s 1.52x