summaryrefslogtreecommitdiffstats
path: root/examples/test_pcap_read.lua
blob: 7d29c7acd8979d715db056649c8fe0c0b388da94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env dnsjit
local clock = require("dnsjit.lib.clock")
local log = require("dnsjit.core.log")
local getopt = require("dnsjit.lib.getopt").new({
    { "v", "verbose", 0, "Enable and increase verbosity for each time given", "?+" },
    { "l", "layer", false, "Test also with dnsjit.filter.layer", "?" },
    { "p", "producer", false, "Test with the producer interface rather then receiver interface", "?" },
})
local pcap, runs = unpack(getopt:parse())
if getopt:val("help") then
    getopt:usage()
    return
end
local v = getopt:val("v")
if v > 0 then
    log.enable("warning")
end
if v > 1 then
    log.enable("notice")
end
if v > 2 then
    log.enable("info")
end
if v > 3 then
    log.enable("debug")
end

if pcap == nil then
    print("usage: "..arg[1].." <pcap> [runs]")
    return
end

inputs = { "fpcap", "mmpcap", "pcap" }
result = {}
results = {}
highest = nil

if runs == nil then
    runs = 10
else
    runs = tonumber(runs)
end

if getopt:val("p") then
    for _, name in pairs(inputs) do
        rt = 0.0
        p = 0

        print("run", name)
        for n = 1, runs do
            o = require("dnsjit.output.null").new()
            i = require("dnsjit.input."..name).new()

            if name == "pcap" then
                i:open_offline(pcap)
            else
                i:open(pcap)
            end

            if getopt:val("l") then
                f = require("dnsjit.filter.layer").new()
                f:producer(i)
                o:producer(f)
            else
                o:producer(i)
            end

            ss, sns = clock:monotonic()
            o:run()
            es, ens = clock:monotonic()

            if es > ss then
                rt = rt + ((es - ss) - 1) + ((1000000000 - sns + ens)/1000000000)
            elseif es == ss and ens > sns then
                rt = rt + (ens - sns) / 1000000000
            end

            p = p + o:packets()
        end

        result[name] = {
            rt = rt,
            p = p
        }
        if highest == nil or rt > result[highest].rt then
            highest = name
        end
        table.insert(results, name)
    end
else
    for _, name in pairs(inputs) do
        rt = 0.0
        p = 0

        print("run", name)
        for n = 1, runs do
            o = require("dnsjit.output.null").new()
            i = require("dnsjit.input."..name).new()

            if name == "pcap" then
                i:open_offline(pcap)
            else
                i:open(pcap)
            end

            if getopt:val("l") then
                f = require("dnsjit.filter.layer").new()
                f:receiver(o)
                i:receiver(f)
            else
                i:receiver(o)
            end

            ss, sns = clock:monotonic()
            if name == "pcap" then
                i:dispatch()
            else
                i:run()
            end
            es, ens = clock:monotonic()

            if es > ss then
                rt = rt + ((es - ss) - 1) + ((1000000000 - sns + ens)/1000000000)
            elseif es == ss and ens > sns then
                rt = rt + (ens - sns) / 1000000000
            end

            p = p + o:packets()
        end

        result[name] = {
            rt = rt,
            p = p
        }
        if highest == nil or rt > result[highest].rt then
            highest = name
        end
        table.insert(results, name)
    end
end

print("name", "runtime", "pps", "x", "pkts")
print(highest, result[highest].rt, result[highest].p/result[highest].rt, 1.0, result[highest].p)
for _, name in pairs(results) do
    if name ~= highest then
        local f = result[name].p / result[highest].p
        print(name, result[name].rt, result[name].p/result[name].rt, (result[highest].rt/result[name].rt)*f, result[name].p)
    end
end