Skip to content

Instantly share code, notes, and snippets.

@psyomn
Created March 18, 2013 19:44
Show Gist options
  • Save psyomn/5190208 to your computer and use it in GitHub Desktop.
Save psyomn/5190208 to your computer and use it in GitHub Desktop.
Make blob dbs to benchmarks against selects (see if there's degradation in lookups when there's a lot of blob data).
require 'sqlite3'
require 'singleton'
class DbRegistry
include Singleton
def initialize
@handle = SQLite3::Database.new("db.db")
end
def execute(sql)
@handle.execute(sql)
end
end
require 'benchmark'
require_relative 'db_registry.rb'
ANIM = { 1 => '|', 2 => '/', 3 => '-', 4 => '\\' }
def make_blob!
str = String.new
1_000.times do
str.concat(rand(65..90).chr)
end
str
end
def poppulate!
sqlinsert = "INSERT INTO blobby (data) values (?)"
10_000.times do |x|
DbRegistry.instance.execute(sqlinsert.gsub(/\?/, "'#{make_blob!}'"))
DbRegistry.instance.execute("INSERT INTO notblobby (data) values ('derp')")
print ANIM[x%5]
print "\r"
end
end
# Make the database
DbRegistry.instance
# Before blob test
print "making tables ... "
DbRegistry.instance.execute("CREATE TABLE blobby (data blob, id INTEGER PRIMARY KEY AUTOINCREMENT);")
DbRegistry.instance.execute("CREATE TABLE notblobby (id INTEGER PRIMARY KEY AUTOINCREMENT, data varchar(50));")
puts "done!"
# Benchmarking
print "poppulating ... "
poppulate!
puts "done!"
# After blob test
print "drop tables ? y/n"
if "y" == gets.chomp
DbRegistry.instance.execute("DROP TABLE blobby;")
DbRegistry.instance.execute("DROP TABLE notblobby;")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment