Implemented BenchmarkSample function in the most efficient way possible to my current knowledge.
After understanding the code and problem, I identified operations to improve performance and efficiency to some extent. By leveraging my programming skills, I realized that some precomputation may be useful.I have taken reference of BenchmarkRawUDP function and have made following changes :
-
Precomputing connections instead of building connection for each writer function call (i.e. b.N times).
-
Instead of sending UDP packets sequencially now we are sending concurrently utilizing goroutines.
Result i got as compared to BenchmarkRawUDP function (Baseline Implementation) are following in order :
- Number of iterations per second - Almost 2 times better.
- Time for processing each iteration - Almost 1.85 times better.
- Bytes allocated per iteration - Bit better.
- Number of allocations per iteration - Bit better.
- We are not caring about the order in which each reader receiving messages.
- Increased resource usage, as each thread requires its own set of resources such as memory and CPU time.
- Precomputing the connection pool improves performance by reducing latency since connections are established beforehand. However, Preallocation of resources may not be suitable for scenarios where the number of connections ( readersCount ) varies dynamically.