~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to tags/mysql-proxy-0.6.0/lib/analyze-query.lua

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--[[
 
2
 
 
3
   Copyright (C) 2007 MySQL AB
 
4
 
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; version 2 of the License.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; if not, write to the Free Software
 
16
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
--]]
 
19
 
 
20
local commands     = require("proxy.commands")
 
21
local tokenizer    = require("proxy.tokenizer")
 
22
local auto_config  = require("proxy.auto-config")
 
23
 
 
24
if not proxy.global.config.analyze_query then
 
25
        proxy.global.config.analyze_query = {
 
26
                analyze_queries = true,
 
27
        }
 
28
end
 
29
 
 
30
baseline = {}
 
31
 
 
32
function read_query(packet) 
 
33
        local cmd = commands.parse(packet)
 
34
 
 
35
        local r = auto_config.handle(cmd)
 
36
        if r then return r end
 
37
 
 
38
        -- analyzing queries is disabled, just pass them on
 
39
        if not proxy.global.config.analyze_query then
 
40
                return
 
41
        end
 
42
 
 
43
        -- we only handle normal queries
 
44
        if cmd.type ~= proxy.COM_QUERY then
 
45
                return
 
46
        end
 
47
 
 
48
        baseline = {}
 
49
 
 
50
        -- cover the query in SHOW SESSION STATUS
 
51
        proxy.queries:append(2, string.char(proxy.COM_QUERY) .. "SHOW SESSION STATUS")
 
52
        proxy.queries:append(1, packet)
 
53
        proxy.queries:append(3, string.char(proxy.COM_QUERY) .. "SHOW SESSION STATUS")
 
54
 
 
55
        return proxy.PROXY_SEND_QUERY
 
56
end
 
57
 
 
58
function read_query_result(inj)
 
59
        local res = assert(inj.resultset)
 
60
 
 
61
        if inj.id == 1 then
 
62
                if not res.query_status or res.query_status == proxy.MYSQLD_PACKET_ERR then
 
63
                        -- the query failed, let's clean the queue
 
64
                        --
 
65
                        proxy.queries:reset()
 
66
                end
 
67
 
 
68
                local tokens = tokenizer.tokenize(inj.query:sub(2))
 
69
                local norm_query = tokenizer.normalize(tokens)
 
70
 
 
71
                o = "# " .. os.date("%Y-%m-%d %H:%M:%S") .. 
 
72
                        " [".. proxy.connection.server.thread_id .. 
 
73
                        "] user: " .. proxy.connection.client.username .. 
 
74
                        ", db: " .. proxy.connection.client.default_db .. "\n" ..
 
75
                        "Query: " .. string.format("%q", inj.query:sub(2)) .. "\n" ..
 
76
                        "Norm_Query: " .. string.format("%q", norm_query) .. "\n" ..
 
77
                        "Exec_time: " .. inj.query_time .. " us" .. "\n"
 
78
 
 
79
                return
 
80
        elseif inj.id == 2 then
 
81
                -- the first SHOW SESSION STATUS
 
82
                
 
83
                for row in res.rows do
 
84
                        -- 1 is the key, 2 is the value
 
85
                        baseline[row[1]] = row[2]
 
86
                end
 
87
        elseif inj.id == 3 then
 
88
                local delta_counters
 
89
                
 
90
                for row in res.rows do
 
91
                        if baseline[row[1]] then
 
92
                                local num1 = tonumber(baseline[row[1]])
 
93
                                local num2 = tonumber(row[2])
 
94
 
 
95
                                if row[1] == "Com_show_status" then
 
96
                                        num2 = num2 - 1
 
97
                                elseif row[1] == "Questions" then
 
98
                                        num2 = num2 - 1
 
99
                                elseif row[1] == "Last_query_cost" then
 
100
                                        num1 = 0
 
101
                                end
 
102
                                
 
103
                                if num1 and num2 and num2 - num1 > 0 then
 
104
                                        o = o .. ".. " .. row[1] .. " = " .. (num2 - num1) .. "\n"
 
105
                                end
 
106
                        end
 
107
                end
 
108
                -- add a newline
 
109
                print(o)
 
110
        end
 
111
 
 
112
        return proxy.PROXY_IGNORE_RESULT
 
113
end