Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created October 17, 2012 03:40
Show Gist options
  • Save kimoto/3903580 to your computer and use it in GitHub Desktop.
Save kimoto/3903580 to your computer and use it in GitHub Desktop.
MySQL Benchmark
#!/bin/env ruby
#
# create table tinyurl(
# id INT(11) NOT NULL AUTO_INCREMENT,
# shorturl VARCHAR(64) NOT NULL,
# url TEXT NOT NULL,
# PRIMARY KEY(id),
# UNIQUE(shorturl)
# );
#
# encoding: utf-8
# Author: kimoto
require 'mysql2'
def benchmark(desc, &proc)
puts ">>> #{desc}"
start = Time.now
begin
proc.call
rescue => ex
STDERR.puts ex
end
stop = Time.now
stop - start
end
client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => '', :database => 'bench')
requests = 1000
client.query("DELETE FROM tinyurl")
seconds = benchmark "mysql insert speed" do
requests.times{ |n|
client.query("INSERT INTO tinyurl (shorturl, url) VALUES('#{n}', 'http://example.com/?n=#{n}')")
}
end
puts "#{seconds} seconds"
puts "#{(requests / seconds)} req/sec"
seconds = benchmark "mysql select speed" do
requests.times{ |n|
client.query("SELECT url FROM tinyurl where shorturl = '#{n}';")
}
end
puts "#{seconds} seconds"
puts "#{(requests / seconds)} req/sec"
seconds = benchmark "mysql not found select speed" do
requests.times{ |n|
client.query("SELECT url FROM tinyurl where shorturl = 'nf_#{n}';")
}
end
puts "#{seconds} seconds"
puts "#{(requests / seconds)} req/sec"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment