Skip to content

Instantly share code, notes, and snippets.

View splattael's full-sized avatar
🏠
Working from home

Peter Leitzen splattael

🏠
Working from home
View GitHub Profile
var entries = $$("div[data-entryid]");
var ids = extract_ids(entries);
streets.service("reader").askMarkEntriesAsRead(ids);
@splattael
splattael / string_tokenize.rb
Last active August 29, 2015 14:04
Benchmark Lotus::Utils::String#tokenize
require 'benchmark/ips'
require 'lotus/utils/string'
simple = Lotus::Utils::String.new("Lotus")
alternate = Lotus::Utils::String.new("Lotus::(Utils|App)")
Benchmark.ips do |x|
x.report "simple" do
simple.tokenize {}
end
@splattael
splattael / bench.rb
Created August 5, 2014 12:56
Speed up Lotus::Model coercer
require 'benchmark/ips'
Benchmark.ips do |x|
x.report "hash simple" do
Hash[:foo, 1, :bar, 2]
end
x.report "hash splat" do
Hash[*[:foo, 1, :bar, 2]]
end
@splattael
splattael / unroll_attributes.rb
Created August 5, 2014 14:43
unroll list of entity attributes
require 'benchmark/ips'
require 'lotus/entity'
def attributes_for(times)
times.times.map { |i| :"field#{i}" }
end
def hash_for(times)
Hash[times.times.map { |i| [:"field#{i}", i] }]
end
def extract_log(dir)
`(cd #{dir} && git log --oneline --shortstat --pretty=format:%aE)`
end
def extract_stats(log_string)
log_string.split(/\n\n/).map do |log_entry|
StatEntry.new log_entry
end
end
@splattael
splattael / basic.rb
Created February 24, 2015 09:08
ROM benchmark
#!/usr/bin/env ruby
# encoding: utf-8
require_relative 'setup'
run "Loading ONE user object" do |x|
x.expect { |user| user.name == 'name 1' }
x.report("AR") { ARUser.by_name('name 1').first }
x.report("ROM") { users.by_name('name 1').first }
end
require 'set'
require 'benchmark/ips'
KEYS = (:a .. :z).to_a
SET = Set.new(KEYS)
HASH = Hash[KEYS.product([true])]
Benchmark.ips do |x|
x.report "set.include" do
SET.include?(:z)
end
@splattael
splattael / allocation_tracer.rb
Created February 28, 2015 09:21
Trace object allocations and sum them per path
require 'allocation_tracer'
ObjectSpace::AllocationTracer.setup(%i{path line type})
ObjectSpace::AllocationTracer.trace
at_exit{
results = ObjectSpace::AllocationTracer.stop
sum = Hash.new { |hash, key| hash[key] = [0] * 6 }
puts ObjectSpace::AllocationTracer.header.join("\t")
results.sort_by{|k, v| v[0]}.each{|k, v|
path, line, type = k
@splattael
splattael / test.rb
Last active August 29, 2015 14:19
mt 5.6
require 'minitest/spec'
require 'minitest/autorun'
require 'ostruct'
describe "Failure" do
it "fails with value" do
data = OpenStruct.new(:value => "bar")
assert_equal "bar", data.value
end
# apt-get install crystal
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package crystal