Skip to content

Instantly share code, notes, and snippets.

Keenan Brock kbrock

Block or report user

Report or block kbrock

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
kbrock /
Created May 30, 2018
Compare string to array
method nil speed str speed
?split:[] 9,761,148.9 i/s 2,869,972.9 i/s 3.40x
&.split||[] 9,271,343.0 i/s same 2,879,248.1 i/s 3.39x
&&split||[] 9,096,687.7 i/s same 2,941,303.8 i/s 3.32x
to_s.split 4,288,858.0 i/s 2.28x 2,822,293.7 i/s 3.46x
ruby -v
kbrock /
Last active May 30, 2018
Comparing Array blank vs empty
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
nil slower empty slower full slower
x&.empty? 11985357.7 i/s same 11617138.6 i/s same 11247144.5 i/s same
x.blank? 9909235.8 i/s 1.21x 10488507.9 i/s 1.14x 10418916.8 i/s 1.15x
x.nil? || x.empty? 10194622.1 i/s 1.18x 9584369.0 i/s 1.25x 9667811.7 i/s 1.24x
x.try!(:empty?) 5437472.5 i/s 2.20x 3493090.9 i/s 3.43x 3523153.2 i/s 3.40x
kbrock /
Last active Apr 26, 2018
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

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 /
Last active Aug 29, 2017 — forked from aishfenton/serializer_benchmarks.rb
Performance comparison of different ruby serializer methods
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 / timing.rb
Created Jun 21, 2017
Metrics capture timing
View timing.rb
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")
[[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 }
View index.html
table {border-collapse:collapse;}
table, td, th { border:1px solid black; }
.f0, a.f0 { color: #999; }
.f1, a.f1 { color: #ccc; }
kbrock / coordinator.rb
Last active Jun 8, 2017
multi threaded example
View coordinator.rb
#!/usr/bin/env ruby
require 'thread'
COLLECTOR_COUNT = 2 # define to run in separate threads. comment to run inline
class Db
def initialize
@mutex =
@data = {}
def []=(n, v) ; @mutex.synchronize { @data[n] = v } ; end
kbrock / transaction_fix_repro.rb
Last active Mar 15, 2017
Reproduce rails transaction timeout error
View transaction_fix_repro.rb
#!/usr/bin/env ruby
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
gemfile(true) do
kbrock /
Created Mar 1, 2017
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)

You can’t perform that action at this time.