func BenchmarkSort(b *testing.B) { for i := 0; i < b.N; i++ { data := generateRandomSlice(1000) sort(data) } } func generateRandomSlice(size int) []int { slice := make([]int, size) for i := 0; i < size; i++ { slice[i] = rand.Intn(100) } return slice } func sort(data []int) { sort.Slice(data, func(i, j int) bool { return data[i] < data[j] }) }
func BenchmarkServer(b *testing.B) { for i := 0; i < b.N; i++ { res, err := http.Get("http://localhost:8080") if err != nil { log.Fatal(err) } res.Body.Close() } }In this example, the `BenchmarkServer` function makes multiple requests to a web server running on `localhost:8080`, using the `http.Get` method. The function is called multiple times, with `b.N` indicating the number of times the benchmark should be run. This example uses the `net/http` package library.