K
K
korsamc2019-12-20 12:32:26
go
korsamc, 2019-12-20 12:32:26

How to write a benchmark for a function in Golang?

I have a function that processes a packet, I wrote such a benchmark for it

func BenchmarkAccept(b *testing.B) {
  q, _ := netfilter.NewNFQueue(1,1024,netfilter.NF_DEFAULT_PACKET_SIZE)
  defer q.Close()
  b.ResetTimer()
  for i := 0; i < b.N; i++ {
    p := <- q.GetPackets()
    Accept(p)
  }
}

BenchmarkAccept-4              1        4751730622 ns/op

I understand that here I am sending 1 packet and a natural output for 1 operation, but I still don’t understand how to implement it in such a way that by sending many packets I can get a benchmark.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question