Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jan 13, 2010
0 parents commit 2c336cc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.log
44 changes: 44 additions & 0 deletions mysqllog.lua
@@ -0,0 +1,44 @@
--[[
Copyright (C) 2007 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--]]
---
-- Uses MySQL-Proxy to log your queries
--
-- Written by Giuseppe Maxia, based on examples provided
-- by Jan Knesckhe
--
-- This script will log the current date and time, the connection id
-- and the query to a file named "mysql.log" in the current directory
--
local log_file = 'mysql.log'
local fh = io.open(log_file, "a+")
function read_query( packet )
if string.byte(packet) == proxy.COM_QUERY then
local query = string.sub(packet, 2)
fh:write( string.format("%s %6d -- %s \n",
os.date('%Y-%m-%d %H:%M:%S'),
proxy.connection.server.thread_id,
query
))
fh:flush()
end
end
38 changes: 38 additions & 0 deletions replay
@@ -0,0 +1,38 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'active_record'

dir = ARGV[0] || raise('gimme rails dir as ARGV[0]')
logfile = 'mysql.log'

def connect(dir)
config = YAML.load(File.read("#{dir}/config/database.yml"))['staging']
ActiveRecord::Base.establish_connection(config.merge(:port=>3306))
end

def replay(logfile)
sql = File.readlines(logfile).map do |line|
#2010-01-13 11:06:16 443 -- SELECT version FROM schema_migrations\n
line.split(' -- ',2)[1].strip
end

puts "replaying #{sql.size} lines of sql ..."

parallel(4, sql) do |line|
ActiveRecord::Base.connection.execute(line)
end
end

def parallel(count, data)
threads = []
data.in_groups_of(data.size/count, false).each do |group|
threads << Thread.new do
group.each{|x| yield(x)}
end
end
threads.each{|t| t.join}
end

connect(dir)
replay(logfile)

10 changes: 10 additions & 0 deletions run
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby
puts "change config to 127.0.0.1:4040"

Process.fork do
system "mysql-proxy --proxy-lua-script=mysqllog.lua"
end

logfile = "mysql.log"
`touch #{logfile}`
system "tail -f #{logfile}"

0 comments on commit 2c336cc

Please sign in to comment.