Skip to content

Commit

Permalink
Merge pull request #1 from virtalabs/feature/benchmark-tests
Browse files Browse the repository at this point in the history
Feature/benchmark tests
  • Loading branch information
ransford committed Jan 17, 2019
2 parents 6abce78 + c9722fc commit 9042a2b
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -114,6 +114,13 @@ RHEL7 or CentOS:

$ sudo yum install libpcap-devel

# Tests and Benchmarking

Run `go test github.com/virtalabs/tapirx` to run the test suite.

Run `go test -bench=. github.com/virtalabs/tapirx` to run tests and performance
benchmarks.

# Frequently Asked Questions

## How do I select specific network traffic to monitor?
Expand Down
10 changes: 10 additions & 0 deletions dicom_decode_test.go
Expand Up @@ -84,6 +84,16 @@ func TestDicomAppLayerGood(t *testing.T) {
}
}

func BenchmarkDicomDecode(b *testing.B) {
appBytes := make([]byte, len(goodAppLayerBytes))
copy(appBytes, goodAppLayerBytes)
appLayer := gopacket.ApplicationLayer(gopacket.Payload(appBytes))

for i := 0; i < b.N; i++ {
dicomDecode(&appLayer)
}
}

var whitespaceTests = []struct {
callingTitle string
expectedID string
Expand Down
150 changes: 150 additions & 0 deletions hl7_decode_test.go
@@ -0,0 +1,150 @@
// Unit tests for HL7 v2 decoding

package main

import (
"strings"
"testing"

"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)

func TestHL7DecodeFile(t *testing.T) {
handle, err := pcap.OpenOffline("testdata/HL7-ADT-UDI-PRT.pcap")
if err != nil {
panic(err)
}

packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
app := packet.ApplicationLayer()
if app == nil {
continue // Ignore packets without an application layer
}

_, _, err := hl7Decode(&app)
if err != nil {
panic(err)
}
}
}

func appLayerFromString(s string) *gopacket.ApplicationLayer {
bytes := []byte(s)
appLayer := gopacket.ApplicationLayer(gopacket.Payload(bytes))
return &appLayer
}

func TestHL7DecodeTooShort(t *testing.T) {
appLayer := appLayerFromString("MSH")
ident, _, err := hl7Decode(appLayer)
if ident != "" {
t.Errorf("Got identifier when none was expected")
}
if err == nil {
t.Errorf("Expected an error from too-short HL7 message")
}
}

func testHL7DecodeEmpty(s string, t *testing.T) {
appLayer := appLayerFromString(s)
ident, _, err := hl7Decode(appLayer)
if ident != "" {
t.Errorf("Got identifier when none was expected")
}
if err != nil {
panic(err)
}
}

func TestHL7DecodeEmpty1(t *testing.T) { testHL7DecodeEmpty("MSH|^~\\&", t) }
func TestHL7DecodeEmpty2(t *testing.T) { testHL7DecodeEmpty("MSH|^~\\&|", t) }

func identFromString(s string) string {
appLayer := appLayerFromString(s)
ident, _, err := hl7Decode(appLayer)
if err != nil {
panic(err)
}
return ident
}

// Well-formed message header segment to be prepended to messages for testing
const okHL7Header = ("" +
// Header and delimiter
"MSH|^~\\&|" +

// Envelope information
"Sender|Sender Facility|" +
"Receiver|Receiver Facility|" +

// Timestamp (YYYYMMDDHHMM) + Security (blank)
"201801131030||" +

// Message type: ORU = observations & results
"ORU^R01|" +

// Control ID
"CNTRL-12345|" +

// Processing ID
"P|" +

// Version ID + segment delimiter (carriage return)
"2.4\r")

func getNRecordString(nrec int) string {
if nrec < 1 || nrec > 26 {
return ""
}
alphas := make([]string, nrec)
for i := 0; i < nrec; i++ {
alphas[i] = string('A' + i)
}
return strings.Join(alphas, "|")
}

func TestNRecordString(t *testing.T) {
if getNRecordString(-1) != "" || getNRecordString(0) != "" || getNRecordString(27) != "" {
panic("Out-of-range n-record string broken")
}

if getNRecordString(1) != "A" {
panic("1-record string broken")
}

if getNRecordString(3) != "A|B|C" {
panic("3-record string broken")
}
}

func TestHL7IdentFromOBX18(t *testing.T) {
str := okHL7Header + "OBX|" + getNRecordString(17) + "|Grospira Peach B+\r"
parsed := identFromString(str)
if parsed != "Grospira Peach B+" {
t.Errorf("Failed to parse identifier from string; got '%s'", parsed)
}
}

func BenchmarkHL7IdentFromOBX18(b *testing.B) {
str := okHL7Header + "OBX|" + getNRecordString(17) + "|Grospira Peach B+\r"
for i := 0; i < b.N; i++ {
identFromString(str)
}
}

func TestHL7IdentFromPRT10(t *testing.T) {
str := okHL7Header + "PRT|" + getNRecordString(9) + "|Grospira Peach B+\r"
parsed := identFromString(str)
if parsed != "Grospira Peach B+" {
t.Errorf("Failed to parse identifier from string; got '%s'", parsed)
}
}

func BenchmarkHL7IdentFromPRT10(b *testing.B) {
str := okHL7Header + "PRT|" + getNRecordString(9) + "|Grospira Peach B+\r"
for i := 0; i < b.N; i++ {
identFromString(str)
}
}
27 changes: 27 additions & 0 deletions packet_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"

// import layers to run its init function
Expand Down Expand Up @@ -48,3 +49,29 @@ func TestPacketParseSimple(t *testing.T) {
}

}

// Create an empty Packet and ignore it
func TestSkipEmptyPacket(t *testing.T) {
var data []byte
pkt := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)
stats := NewStats()
handlePacket(pkt, stats, nil, nil, nil)

if stats.TotalPacketCount != 1 {
t.Errorf("Wrong number of packets")
}

if len(stats.Identifiers) != 0 {
t.Errorf("Expected to find no identifiers; found %d", len(stats.Identifiers))
}
}

// Create an empty Packet to measure the overhead of ignoring it
func BenchmarkSkipEmptyPacket(b *testing.B) {
var data []byte
pkt := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)
stats := NewStats()
for i := 0; i < b.N; i++ {
handlePacket(pkt, stats, nil, nil, nil)
}
}

0 comments on commit 9042a2b

Please sign in to comment.