| 1 |
#!@DROOL_SHEBANG@ |
| 2 |
-- DNS Reply Tool (drool) |
| 3 |
-- |
| 4 |
-- Copyright (c) 2017-2021, OARC, Inc. |
| 5 |
-- Copyright (c) 2017, Comcast Corporation |
| 6 |
-- All rights reserved. |
| 7 |
-- |
| 8 |
-- Redistribution and use in source and binary forms, with or without |
| 9 |
-- modification, are permitted provided that the following conditions |
| 10 |
-- are met: |
| 11 |
-- |
| 12 |
-- 1. Redistributions of source code must retain the above copyright |
| 13 |
-- notice, this list of conditions and the following disclaimer. |
| 14 |
-- |
| 15 |
-- 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
-- notice, this list of conditions and the following disclaimer in |
| 17 |
-- the documentation and/or other materials provided with the |
| 18 |
-- distribution. |
| 19 |
-- |
| 20 |
-- 3. Neither the name of the copyright holder nor the names of its |
| 21 |
-- contributors may be used to endorse or promote products derived |
| 22 |
-- from this software without specific prior written permission. |
| 23 |
-- |
| 24 |
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 |
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 |
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 |
-- FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 |
-- COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 |
-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 |
-- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 |
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 |
-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 |
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 |
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 |
-- POSSIBILITY OF SUCH DAMAGE. |
| 36 |
package.path = package.path .. ";@droollibdir@/?.lua" |
| 37 |
|
| 38 |
local clock = require("dnsjit.lib.clock") |
| 39 |
local log = require("dnsjit.core.log").new("drool") |
| 40 |
local getopt = require("dnsjit.lib.getopt").new({ |
| 41 |
{ "V", "version", false, "Print version and exit", "?" }, |
| 42 |
{ "v", "verbose", 0, "Enable and increase verbosity for each time given", "?+" }, |
| 43 |
{ nil, "json", false, "Output statistics as JSON", "?" }, |
| 44 |
{ nil, "csv", false, "Output statistics as CSV", "?" }, |
| 45 |
}) |
| 46 |
getopt.usage_desc = arg[1] .. " command [options...] <arguments...>" |
| 47 |
|
| 48 |
local cmds = { |
| 49 |
replay = "drool.replay", |
| 50 |
respdiff = "drool.respdiff", |
| 51 |
} |
| 52 |
|
| 53 |
if not arg[2] or not cmds[arg[2]] then |
| 54 |
getopt:parse() |
| 55 |
|
| 56 |
if getopt:val("help") then |
| 57 |
getopt:usage() |
| 58 |
os.exit(0) |
| 59 |
end |
| 60 |
if getopt:val("version") then |
| 61 |
print("drool version @PACKAGE_VERSION@") |
| 62 |
os.exit(0) |
| 63 |
end |
| 64 |
if arg[2] then |
| 65 |
log:critical("Command "..arg[2].." not found") |
| 66 |
end |
| 67 |
|
| 68 |
getopt:usage() |
| 69 |
os.exit(-1) |
| 70 |
end |
| 71 |
|
| 72 |
local cmd = require(cmds[arg[2]]).new(getopt) |
| 73 |
getopt:parse() |
| 74 |
|
| 75 |
if getopt:val("help") then |
| 76 |
getopt:usage() |
| 77 |
os.exit(0) |
| 78 |
end |
| 79 |
|
| 80 |
local v = getopt:val("v") |
| 81 |
if v > 0 then |
| 82 |
require("dnsjit.core.log").enable("warning") |
| 83 |
end |
| 84 |
if v > 1 then |
| 85 |
require("dnsjit.core.log").enable("notice") |
| 86 |
end |
| 87 |
if v > 2 then |
| 88 |
require("dnsjit.core.log").enable("info") |
| 89 |
end |
| 90 |
if v > 3 then |
| 91 |
require("dnsjit.core.log").enable("debug") |
| 92 |
end |
| 93 |
|
| 94 |
cmd:getopt(getopt) |
| 95 |
cmd:setup() |
| 96 |
local start_sec, start_nsec = clock:monotonic() |
| 97 |
cmd:run() |
| 98 |
local end_sec, end_nsec = clock:monotonic() |
| 99 |
cmd:finish() |
| 100 |
local fin_sec, fin_nsec = clock:monotonic() |
| 101 |
|
| 102 |
local runtime = 0 |
| 103 |
if end_sec > start_sec then |
| 104 |
runtime = ((end_sec - start_sec) - 1) + ((1000000000 - start_nsec + end_nsec)/1000000000) |
| 105 |
elseif end_sec == start_sec and end_nsec > start_nsec then |
| 106 |
runtime = (end_nsec - start_nsec) / 1000000000 |
| 107 |
end |
| 108 |
|
| 109 |
local finish = 0 |
| 110 |
if fin_sec > end_sec then |
| 111 |
finish = ((fin_sec - end_sec) - 1) + ((1000000000 - end_nsec + fin_nsec)/1000000000) |
| 112 |
elseif fin_sec == end_sec and fin_nsec > end_nsec then |
| 113 |
finish = (fin_nsec - end_nsec) / 1000000000 |
| 114 |
end |
| 115 |
|
| 116 |
if getopt:val("json") then |
| 117 |
print("{") |
| 118 |
print(" \"runtime\": " .. runtime .. ",") |
| 119 |
print(" \"finish\": " .. finish .. ",") |
| 120 |
print(" \"packets\": " .. cmd.packets .. ",") |
| 121 |
print(" \"queries\": " .. cmd.queries .. ",") |
| 122 |
print(" \"sent\": " .. cmd.sent .. ",") |
| 123 |
print(" \"received\": " .. cmd.received .. ",") |
| 124 |
print(" \"responses\": " .. cmd.responses .. ",") |
| 125 |
print(" \"timeouts\": " .. cmd.timeouts .. ",") |
| 126 |
print(" \"errors\": " .. cmd.errors) |
| 127 |
print("}") |
| 128 |
elseif getopt:val("csv") then |
| 129 |
print("runtime,finish,packets,queries,sent,received,responses,timeouts,errors") |
| 130 |
print(runtime ..",".. |
| 131 |
finish ..",".. |
| 132 |
cmd.packets ..",".. |
| 133 |
cmd.queries ..",".. |
| 134 |
cmd.sent ..",".. |
| 135 |
cmd.received ..",".. |
| 136 |
cmd.responses ..",".. |
| 137 |
cmd.timeouts ..",".. |
| 138 |
cmd.errors) |
| 139 |
else |
| 140 |
print("runtime", runtime+finish, "run", runtime, "finish", finish) |
| 141 |
print("", "total", "/sec") |
| 142 |
print("packets", cmd.packets, cmd.packets/runtime) |
| 143 |
print("queries", cmd.queries, cmd.queries/runtime) |
| 144 |
print("sent", cmd.sent, cmd.sent/runtime) |
| 145 |
print("received", cmd.received, cmd.received/runtime) |
| 146 |
print("responses", cmd.responses, cmd.responses/runtime) |
| 147 |
print("timeouts", cmd.timeouts) |
| 148 |
print("errors", cmd.errors) |
| 149 |
end |