Skip to content

Commit

Permalink
Add integration test for ThrottleCPU
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Nov 9, 2023
1 parent 59d6009 commit 699c07e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,3 +1177,61 @@ func TestPageThrottleNetwork(t *testing.T) {
})
}
}

// This test will first navigate to the ping.html site a few times, record the
// average time it takes to run the test. Next it will repeat the steps but
// first apply CPU throttling. The average duration with CPU throttling
// enabled should be longer than without it.
func TestPageThrottleCPU(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t, withFileServer())

tb.withHandler("/ping", func(w http.ResponseWriter, req *http.Request) {
defer func() {
err := req.Body.Close()
require.NoError(t, err)
}()
bb, err := io.ReadAll(req.Body)
require.NoError(t, err)

fmt.Fprint(w, string(bb))
})

page := tb.NewPage(nil)
iterations := 5

noCPUThrottle := performPingTest(t, tb, page, iterations)

err := page.ThrottleCPU(common.CPUProfile{
Rate: 50,
})
require.NoError(t, err)

withCPUThrottle := performPingTest(t, tb, page, iterations)

assert.Greater(t, withCPUThrottle, noCPUThrottle)
}

func performPingTest(t *testing.T, tb *testBrowser, page *common.Page, iterations int) int64 {
t.Helper()

var ms int64
for i := 0; i < iterations; i++ {
start := time.Now()

_, err := page.Goto(tb.staticURL("ping.html"), nil)
require.NoError(t, err)

selector := `div[id="result"]`

// result selector only appears once the page gets a response
// from the async ping request.
_, err = page.WaitForSelector(selector, nil)
require.NoError(t, err)

ms += time.Since(start).Abs().Milliseconds()
}

return ms / int64(iterations)
}

0 comments on commit 699c07e

Please sign in to comment.