Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #20 from gconnell/speedcopy

Speedcopy
  • Loading branch information...
commit 578e8e6cb78ad684175091af534e321d29f74dc4 2 parents e9a12e1 + ed68d52
@akrennmair authored
Showing with 53 additions and 10 deletions.
  1. +4 −10 pcap.go
  2. +49 −0 tools/benchmark/benchmark.go
View
14 pcap.go
@@ -93,28 +93,22 @@ func Openoffline(file string) (handle *Pcap, err error) {
}
func (p *Pcap) NextEx() (pkt *Packet, result int32) {
- var pkthdr_ptr *C.struct_pcap_pkthdr
- var pkthdr C.struct_pcap_pkthdr
+ var pkthdr *C.struct_pcap_pkthdr
var buf_ptr *C.u_char
var buf unsafe.Pointer
- result = int32(C.hack_pcap_next_ex(p.cptr, &pkthdr_ptr, &buf_ptr))
+ result = int32(C.hack_pcap_next_ex(p.cptr, &pkthdr, &buf_ptr))
buf = unsafe.Pointer(buf_ptr)
- pkthdr = *pkthdr_ptr
-
if nil == buf {
return
}
+
pkt = new(Packet)
pkt.Time = time.Unix(int64(pkthdr.ts.tv_sec), int64(pkthdr.ts.tv_usec)*1000)
pkt.Caplen = uint32(pkthdr.caplen)
pkt.Len = uint32(pkthdr.len)
- pkt.Data = make([]byte, pkthdr.caplen)
-
- for i := uint32(0); i < pkt.Caplen; i++ {
- pkt.Data[i] = *(*byte)(unsafe.Pointer(uintptr(buf) + uintptr(i)))
- }
+ pkt.Data = C.GoBytes(buf, C.int(pkthdr.caplen))
return
}
View
49 tools/benchmark/benchmark.go
@@ -0,0 +1,49 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "runtime/pprof"
+ "time"
+
+ "github.com/gconnell/gopcap"
+)
+
+func main() {
+ var filename *string = flag.String("file", "", "filename")
+ var decode *bool = flag.Bool("d", false, "If true, decode each packet")
+ var cpuprofile *string = flag.String("cpuprofile", "", "filename")
+
+ flag.Parse()
+
+ h, err := pcap.Openoffline(*filename)
+ if err != nil {
+ fmt.Printf("Couldn't create pcap reader: %v", err)
+ }
+
+ if *cpuprofile != "" {
+ if out, err := os.Create(*cpuprofile); err == nil {
+ pprof.StartCPUProfile(out)
+ defer func() {
+ pprof.StopCPUProfile()
+ out.Close()
+ }()
+ } else {
+ panic(err)
+ }
+ }
+
+ i, nilPackets := 0, 0
+ start := time.Now()
+ for pkt, code := h.NextEx(); code != -2; pkt, code = h.NextEx() {
+ if pkt == nil {
+ nilPackets++
+ } else if *decode {
+ pkt.Decode()
+ }
+ i++
+ }
+ duration := time.Since(start)
+ fmt.Printf("Took %v to process %v packets, %v per packet, %d nil packets\n", duration, i, duration/time.Duration(i), nilPackets)
+}
Please sign in to comment.
Something went wrong with that request. Please try again.