Skip to content

Commit

Permalink
👍 Optimize by tuning draw rate
Browse files Browse the repository at this point in the history
  • Loading branch information
Takumasa Sakao committed Feb 12, 2018
1 parent c2e6f37 commit 2743a7a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
snapshot_len int32 = 1024
promiscuous bool = false
err error
timeout time.Duration = 100 * time.Millisecond
timeout time.Duration = 500 * time.Millisecond
handle *pcap.Handle
)

Expand Down
2 changes: 1 addition & 1 deletion panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func preparePacketList() *tview.Table {
SetBackgroundColor(tcell.ColorDefault).
SetBorder(true)

columns := []string{"No.", "Time", "Flow", "Length", "Link Type", "Network Type", "Transport Type"}
columns := []string{"No.", "Time", "Flow", "Length", "Network Type", "Transport Type"}
for i, column := range columns {
table.SetCell(0, i,
tview.NewTableCell(column).
Expand Down
57 changes: 42 additions & 15 deletions tcpterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"errors"
"fmt"
"io"
"strconv"
"time"

"github.com/gdamore/tcell"
"github.com/google/gopacket"
Expand Down Expand Up @@ -87,23 +89,48 @@ func NewTcpterm(src *gopacket.PacketSource) *Tcpterm {
}

func (app *Tcpterm) Run() {
refreshTrigger := make(chan bool)

go func() {
cnt := 0
for packet := range app.src.Packets() {
cnt++
rowCount := app.table.GetRowCount()

flow := packet.NetworkLayer().NetworkFlow()
app.table.SetCell(rowCount, 0, tview.NewTableCell(strconv.Itoa(cnt)))
app.table.SetCell(rowCount, 1, tview.NewTableCell(packet.Metadata().Timestamp.Format(timestampFormt)))
app.table.SetCell(rowCount, 2, tview.NewTableCell(flow.String()))
app.table.SetCell(rowCount, 3, tview.NewTableCell(strconv.Itoa(packet.Metadata().Length)))
app.table.SetCell(rowCount, 4, tview.NewTableCell(packet.LinkLayer().LayerType().String()))
app.table.SetCell(rowCount, 5, tview.NewTableCell(packet.NetworkLayer().LayerType().String()))
app.table.SetCell(rowCount, 6, tview.NewTableCell(packet.TransportLayer().LayerType().String()))

app.packets = append(app.packets, packet)
app.view.Draw()
for {
packet, err := app.src.NextPacket()
if err == io.EOF {
return
} else if err == nil {
cnt++
rowCount := app.table.GetRowCount()

flow := packet.NetworkLayer().NetworkFlow()
app.table.SetCell(rowCount, 0, tview.NewTableCell(strconv.Itoa(cnt)))
app.table.SetCell(rowCount, 1, tview.NewTableCell(packet.Metadata().Timestamp.Format(timestampFormt)))
app.table.SetCell(rowCount, 2, tview.NewTableCell(flow.String()))
app.table.SetCell(rowCount, 3, tview.NewTableCell(strconv.Itoa(packet.Metadata().Length)))
app.table.SetCell(rowCount, 4, tview.NewTableCell(packet.NetworkLayer().LayerType().String()))
app.table.SetCell(rowCount, 5, tview.NewTableCell(packet.TransportLayer().LayerType().String()))

app.packets = append(app.packets, packet)

if cnt%1000 == 0 {
refreshTrigger <- true
}
}
}
}()

go func() {
for {
time.Sleep(1000 * time.Millisecond)
refreshTrigger <- true
}
}()

go func() {
for {
_, ok := <-refreshTrigger
if ok {
app.view.Draw()
}
}
}()

Expand Down

0 comments on commit 2743a7a

Please sign in to comment.