#!/usr/bin/env ruby require 'sequel' require 'better-benchmark' $dbh = Sequel( 'postgres://dbtest:dbtest@localhost/dbtest' ) require 'models' def report( result ) puts puts( "Set 1 mean: %.3f s" % [ result[ :results1 ][ :mean ] ] ) puts( "Set 1 std dev: %.3f" % [ result[ :results1 ][ :stddev ] ] ) puts( "Set 2 mean: %.3f s" % [ result[ :results2 ][ :mean ] ] ) puts( "Set 2 std dev: %.3f" % [ result[ :results2 ][ :stddev ] ] ) puts "p.value: #{result[ :p ]}" puts "W: #{result[ :W ]}" puts( "The difference (%+.1f%%) %s statistically significant." % [ ( ( result[ :results2 ][ :mean ] - result[ :results1 ][ :mean ] ) / result[ :results1 ][ :mean ] ) * 100, result[ :significant ] ? 'IS' : 'IS NOT' ] ) end # Test 1: Create records NUM_COMPANIES = 1000 puts "*" * 70 puts "Test 1: Record creation hook" puts result = Benchmark.compare_realtime( :iterations => 20, :verbose => true ) { |iteration| (0...NUM_COMPANIES).each do |i| j = ( iteration - 1 ) * NUM_COMPANIES + i c = Company1.create( :name => "company#{j}" ) end }.with { |iteration| (0...NUM_COMPANIES).each do |i| j = ( iteration - 1 ) * NUM_COMPANIES + i c = Company2.create( :name => "company#{j}" ) end } report result # Validate insertion puts puts "*" * 70 puts "Test 2: Insertion validation" puts NUM_EMPLOYEES_TO_VALIDATE = 1000 result = Benchmark.compare_realtime( :iterations => 20, :verbose => true ) { |iteration| (0...NUM_EMPLOYEES_TO_VALIDATE).each do |i| j = ( iteration - 1 ) * NUM_EMPLOYEES_TO_VALIDATE + i # Too short created = Employee1.create( :name => "emp#{j}", :company_id => 1 ) if not created # error handling would be here end # Long enough created = Employee1.create( :name => "anemployee#{j}", :company_id => 1 ) if not created # error handling would be here end end }.with { |iteration| (0...NUM_EMPLOYEES_TO_VALIDATE).each do |i| j = ( iteration - 1 ) * NUM_EMPLOYEES_TO_VALIDATE + i # Too short begin Employee2.create( :name => "emp#{j}", :company_id => 1 ) rescue PGError #ignore end # Long enough begin Employee2.create( :name => "anemployee#{j}", :company_id => 1 ) rescue PGError #ignore end end } report result # Delete records puts puts "*" * 70 puts "Test 3: Record deletion hook 1 (deletion cascade)" puts NUM_COMPANIES_TO_DELETE = 20 NUM_EMPLOYEES_PER_COMPANY = 5000 $stdout.print "Adding employee records (schema1)"; $stdout.flush (0...NUM_COMPANIES_TO_DELETE).each do |i| c = Company1.where( :name => "company#{i}" ).first (1..NUM_EMPLOYEES_PER_COMPANY).each do |j| Employee1.create( :name => "employee#{j}", :company_id => c.values[ :id ] ) end $stdout.print "."; $stdout.flush end puts $stdout.print "Adding employee records (schema2)"; $stdout.flush (0...NUM_COMPANIES_TO_DELETE).each do |i| c = Company2.where( :name => "company#{i}" ).first (1..NUM_EMPLOYEES_PER_COMPANY).each do |j| Employee2.create( :name => "employee#{j}", :company_id => c.values[ :id ] ) end $stdout.print "."; $stdout.flush end puts puts "Running deletion iterations..." result = Benchmark.compare_realtime( :iterations => NUM_COMPANIES_TO_DELETE, :verbose => true ) { |iteration| c = Company1[ :name => "company#{iteration}" ] c.destroy }.with { |iteration| c = Company2[ :name => "company#{iteration}" ] c.destroy } report result # Deletion hook puts puts "*" * 70 puts "Test 4: Record deletion hook 2 (update other table)" puts NUM_AUTHORS = 20 NUM_POSTS_PER_AUTHOR = 300 $stdout.print "Adding authors and posts (schema1)"; $stdout.flush (0...NUM_AUTHORS).each do |i| a = Author1.create( :name => "author#{i}" ) (1..NUM_POSTS_PER_AUTHOR).each do |j| Post1.create( :text => "post #{j}", :author_id => a.values[ :id ] ) end $stdout.print "."; $stdout.flush end puts $stdout.print "Adding authors and posts (schema2)"; $stdout.flush (0...NUM_AUTHORS).each do |i| a = Author2.create( :name => "author#{i}" ) (1..NUM_POSTS_PER_AUTHOR).each do |j| Post2.create( :text => "post #{j}", :author_id => a.values[ :id ] ) end $stdout.print "."; $stdout.flush end puts puts "Running deletion iterations..." result = Benchmark.compare_realtime( :iterations => NUM_AUTHORS, :verbose => true ) { |iteration| a = Author1.where( :name => "author#{iteration-1}" ).first a.posts.each do |post| post.destroy end }.with { |iteration| a = Author2.where( :name => "author#{iteration-1}" ).first a.posts.each do |post| post.destroy end } report result