Skip to content

Commit

Permalink
dhcp: add benchmarks (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu authored Nov 26, 2024
1 parent 294cdd7 commit 0137fe2
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions pkg/roles/dhcp/dhcp_handler4_request_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package dhcp_test

import (
"net"
"net/netip"
"testing"

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dhcp"
"beryju.io/gravity/pkg/roles/dhcp/types"
"beryju.io/gravity/pkg/tests"
"github.com/insomniacslk/dhcp/dhcpv4"
)

func BenchmarkRoleDHCP_Request(b *testing.B) {
defer tests.Setup(nil)()
rootInst := instance.New()
ctx := tests.Context()
inst := rootInst.ForRole("dhcp", ctx)
role := dhcp.New(inst)

tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyScopes,
"test1",
).String(),
tests.MustJSON(dhcp.Scope{
SubnetCIDR: "10.100.0.0/24",
Default: true,
TTL: 86400,
IPAM: map[string]string{
"type": "internal",
"range_start": "10.100.0.100",
"range_end": "10.100.0.250",
},
}),
))
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyScopes,
"test2",
).String(),
tests.MustJSON(dhcp.Scope{
SubnetCIDR: "10.100.1.0/24",
Default: true,
TTL: 86400,
IPAM: map[string]string{
"type": "internal",
"range_start": "10.100.1.100",
"range_end": "10.100.1.250",
},
}),
))
tests.PanicIfError(inst.KV().Put(
ctx,
inst.KV().Key(
types.KeyRole,
types.KeyScopes,
"test3",
).String(),
tests.MustJSON(dhcp.Scope{
SubnetCIDR: "10.100.2.0/24",
Default: true,
TTL: 86400,
IPAM: map[string]string{
"type": "internal",
"range_start": "10.100.2.100",
"range_end": "10.100.2.250",
},
}),
))

tests.PanicIfError(role.Start(ctx, []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 1067,
}))))
defer role.Stop()
c, err := netip.ParsePrefix("10.100.0.0/24")
if err != nil {
panic(err)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
rr := &dhcpv4.DHCPv4{
GatewayIPAddr: net.ParseIP(c.Addr().Next().String()),
ClientHWAddr: generateHW(),
OpCode: dhcpv4.OpcodeBootRequest,
}
rr.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeRequest))
req4 := role.NewRequest4(rr)
_ = role.Handler4(req4)
}
}

0 comments on commit 0137fe2

Please sign in to comment.