Skip to content

Instantly share code, notes, and snippets.

View kbrock's full-sized avatar

Keenan Brock kbrock

View GitHub Profile
@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 / 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 / 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 }
<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 / 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
@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 / 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 / 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')
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 / comments.md
Created March 18, 2016 17:13
Comments on a seemingly "simple" PR

My comments on 7388

- s = MiqSearch.find(@sb[:planning][:options][:filter_value])
- s.options ||= {}
- s.options[:userid] = session[:userid]
- s.options[:results_format] = :objects
-vms, attrs = s.search
+ vms = MiqSearch.find(@sb[:planning][:options][:filter_value]).filtered(nil, :userid => current_user)