Skip to content

Commit

Permalink
test: add more benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 3, 2023
1 parent 4b311e8 commit eca90f1
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/plugin/controllx/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,39 @@ func TestSwitchNode_Send(t *testing.T) {
})
}
}

func BenchmarkSwitchNode_Send(b *testing.B) {
n := NewSwitchNode(SwitchNodeConfig{})
defer func() { _ = n.Close() }()

in := port.New()
inPort, _ := n.Port(node.PortIn)
inPort.Link(in)

n.Add("$.a", "out[0]")
n.Add("$.b", "out[1]")
n.Add("$.a = $.b", "out[2]")
n.Add("true", "out[3]")

out := port.New()
defer out.Close()
outPort, _ := n.Port(port.SetIndex(node.PortOut, 0))
outPort.Link(out)

proc := process.New()
defer proc.Exit(nil)

inStream := in.Open(proc)
outStream := out.Open(proc)

b.ResetTimer()

inPayload, _ := primitive.MarshalText(map[string]bool{
"a": true,
})

for i := 0; i < b.N; i++ {
inStream.Send(packet.New(inPayload))
<-outStream.Receive()
}
}
79 changes: 79 additions & 0 deletions pkg/plugin/networkx/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,82 @@ func TestHTTPNode_ServeHTTP(t *testing.T) {
assert.Equal(t, "Hello World!", w.Body.String())
})
}

func BenchmarkHTTPNode_Send(b *testing.B) {
b.Run("IO", func(b *testing.B) {
n := NewHTTPNode(HTTPNodeConfig{})
defer func() { _ = n.Close() }()

io := port.New()
ioPort, _ := n.Port(node.PortIO)
ioPort.Link(io)

io.AddInitHook(port.InitHookFunc(func(proc *process.Process) {
ioStream := io.Open(proc)

for {
inPck, ok := <-ioStream.Receive()
if !ok {
return
}

outPck := packet.New(primitive.NewMap(
primitive.NewString("body"), primitive.NewString("Hello World!"),
primitive.NewString("status"), primitive.NewInt(200),
))
proc.Stack().Link(inPck.ID(), outPck.ID())
ioStream.Send(outPck)
}
}))

b.ResetTimer()

for i := 0; i < b.N; i++ {
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()

n.ServeHTTP(w, r)
}
})

b.Run("In/Out", func(b *testing.B) {
n := NewHTTPNode(HTTPNodeConfig{})
defer func() { _ = n.Close() }()

in := port.New()
inPort, _ := n.Port(node.PortIn)
inPort.Link(in)

out := port.New()
outPort, _ := n.Port(node.PortOut)
outPort.Link(out)

out.AddInitHook(port.InitHookFunc(func(proc *process.Process) {
inStream := in.Open(proc)
outStream := out.Open(proc)

for {
inPck, ok := <-outStream.Receive()
if !ok {
return
}

outPck := packet.New(primitive.NewMap(
primitive.NewString("body"), primitive.NewString("Hello World!"),
primitive.NewString("status"), primitive.NewInt(200),
))
proc.Stack().Link(inPck.ID(), outPck.ID())
inStream.Send(outPck)
}
}))

b.ResetTimer()

for i := 0; i < b.N; i++ {
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()

n.ServeHTTP(w, r)
}
})
}
33 changes: 33 additions & 0 deletions pkg/plugin/networkx/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,38 @@ func TestRouterNode_Send(t *testing.T) {
}
})
}
}

func BenchmarkRouterNode_Send(b *testing.B) {
n := NewRouterNode(RouterNodeConfig{})
defer func() { _ = n.Close() }()

in := port.New()
inPort, _ := n.Port(node.PortIn)
inPort.Link(in)

n.Add(http.MethodGet, "/*", port.SetIndex(node.PortOut, 0))
n.Add(http.MethodGet, "/:1/second", port.SetIndex(node.PortOut, 1))
n.Add(http.MethodGet, "/:1/:2", port.SetIndex(node.PortOut, 2))

out := port.New()
defer out.Close()
outPort, _ := n.Port(port.SetIndex(node.PortOut, 0))
outPort.Link(out)

proc := process.New()
defer proc.Exit(nil)

inStream := in.Open(proc)
outStream := out.Open(proc)

b.ResetTimer()

for i := 0; i < b.N; i++ {
inStream.Send(packet.New(primitive.NewMap(
primitive.NewString(KeyMethod), primitive.NewString(http.MethodGet),
primitive.NewString(KeyPath), primitive.NewString("/first"),
)))
<-outStream.Receive()
}
}

0 comments on commit eca90f1

Please sign in to comment.