Created
June 3, 2015 22:38
-
-
Save huned/62599019cf13aa0e073e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
require 'benchmark' | |
class HypergraphPerformanceTest < Minitest::Test | |
include Hypergraph::TestHelper::Schema | |
include Hypergraph::TestHelper::Benchmarking | |
include Hypergraph::TestHelper::Profiling | |
def setup | |
suites = (ENV['TESTS'] || '').split | |
suites = %w[ 5k ] if suites.length < 1 | |
@suites = suites.inject({}) do |h, name| | |
db_name = "hypergraph_test_#{name}" | |
schema = Hypergraph::Schema.new(db: connection_string(name: db_name)) | |
recreate_schema!(schema) | |
h[name] = { schema: schema } | |
h | |
end | |
# query context | |
@dates = %w[ 2015-06-01 ].map {|s| Time.parse(s) } | |
@fact_id = 1 | |
@scenario_id = 1 | |
end | |
def test_performance | |
with_benchmark('### Load Tables') do |name, hash| | |
schema = hash[:schema] | |
schema.truncate! | |
schema.load!("test/data/csv/#{name}") | |
end | |
with_benchmark('### Instantiate Graph') do |name, hash| | |
schema = hash[:schema] | |
options = { | |
index: { | |
keys: %i[ name ] | |
} | |
} | |
graph = nil | |
profile do | |
graph = hash[:graph] = Hypergraph.from_schema(schema, options) | |
end | |
graph.measures.derive('Total Sales') do |node, scenario_id, t| | |
# Query context. | |
dates = [t] | |
measures = ['Sales'] | |
## Implementation A. | |
## Multithreaded with ThreadSafe::Array and inject. | |
#q = ThreadSafe::Array.new | |
#graph.nodes.peach(Hypergraph.threads) do |n| | |
# q << n.measures.get(scenario_id, dates, measures)[0][0] | |
#end | |
#q.compact.inject(:+) | |
## Implementation B. | |
## Multithreaded with mutex and +=. | |
#s = 0 | |
#m = Mutex.new | |
#graph.nodes.peach(Hypergraph.threads) do |n| | |
# x = n.measures.get(scenario_id, dates, measures)[0][0] | |
# next if x.nil? | |
# m.synchronize { s += x } | |
#end | |
#s | |
## Implementation C. | |
## Single-threaded. | |
#s = 0 | |
#graph.nodes.each do |n| | |
# x = n.measures.get(scenario_id, dates, measures)[0][0] | |
# s += x unless x.nil? | |
#end | |
#s | |
end | |
end | |
with_benchmark('### Query Measures (Derived)') do |name, hash| | |
graph = hash[:graph] | |
measures = ['Sales', 'Total Sales'] | |
puts "[#{measures.join(', ')}] x [#{@dates.join(', ')}]: " | |
p graph[@fact_id].measures.get(@scenario_id, @dates, measures) | |
end | |
end | |
private | |
def with_benchmark(label = '', &block) | |
raise ArgumentError unless block_given? | |
puts label | |
puts | |
@suites.each do |name, hash| | |
GC.start | |
print name.to_s.rjust(10), " " * 10 | |
# Benchmark.measure reports user/system/user+system/elapsed times. | |
puts Benchmark.measure(label) { yield(name, hash) } | |
end | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment