Skip to content

Instantly share code, notes, and snippets.

@mlapshin
Created November 28, 2014 09:10
Show Gist options
  • Save mlapshin/2e0cd85434c394c7f3d4 to your computer and use it in GitHub Desktop.
Save mlapshin/2e0cd85434c394c7f3d4 to your computer and use it in GitHub Desktop.
#!/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'json'
require 'pg'
require 'benchmark'
$obs_tables = ['observation',
'observation_history',
'observation_references',
'observation_search_date',
'observation_search_number',
'observation_search_quantity',
'observation_search_reference',
'observation_search_string',
'observation_search_token',
'observation_sort',
'observation_tag',
'observation_tag_history']
$conn = PG.connect(dbname: 'fhirbase',
user: 'fhirbase',
password: 'fhirbase',
host: 'localhost',
port: 5433)
$results = {}
$sampling_size = 20
def make_observation(i)
@observation ||= JSON.parse(File.read("observation.json"))
new_obs = @observation.dup
new_obs["valueQuantity"]["value"] = 1
new_obs
end
def avg(foo)
foo.reduce { |acc, i| acc + i }.to_f / foo.size.to_f
end
def sample_avg(&block)
results = []
$sampling_size.times do
results << Benchmark.measure(&block).real
end
avg(results)
end
def bench(n)
puts "Performing stuff with N=#{n}"
cfg = { base: "http://localhost.local" }
observations_count = $conn.exec("SELECT COUNT(*) FROM observation")
.values.first.first.to_i
to_create = n - observations_count
to_create = 0 if to_create < 0
create_time = Benchmark.measure do
puts "Creating #{to_create} observations..."
to_create.times do |i|
$conn.exec_params("SELECT fhir_create($1::jsonb, $2, $3::jsonb, $4::jsonb)",
[JSON.generate(cfg),
'Observation',
JSON.generate(make_observation(i)),
JSON.generate([])])
end
$obs_tables.each do |tbl|
$conn.exec "VACUUM ANALYZE #{tbl}"
end
end.real
print "Performing search"
search_time = sample_avg do
$conn.exec_params("SELECT fhir_search($1::jsonb, $2, $3)",
[JSON.generate(cfg),
'Observation',
'value=1'])
print "."
end
print "\n"
{create: create_time, search: search_time}
end
def run_bench(n)
$results[n] = bench(n)
end
run_bench(10)
run_bench(20)
puts $results.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment