1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <cmath>
6#include <limits>
7
8#include "src/heap/gc-tracer.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace v8 {
12namespace internal {
13
14TEST(GCTracer, AverageSpeed) {
15  RingBuffer<BytesAndDuration> buffer;
16  EXPECT_EQ(100 / 2,
17            GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 0));
18  buffer.Push(MakeBytesAndDuration(100, 8));
19  EXPECT_EQ(100 / 2,
20            GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 2));
21  EXPECT_EQ(200 / 10,
22            GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 3));
23  const int max_speed = 1024 * MB;
24  buffer.Reset();
25  buffer.Push(MakeBytesAndDuration(max_speed, 0.5));
26  EXPECT_EQ(max_speed,
27            GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), 1));
28  const int min_speed = 1;
29  buffer.Reset();
30  buffer.Push(MakeBytesAndDuration(1, 10000));
31  EXPECT_EQ(min_speed,
32            GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), 1));
33  buffer.Reset();
34  int sum = 0;
35  for (int i = 0; i < buffer.kSize; i++) {
36    sum += i + 1;
37    buffer.Push(MakeBytesAndDuration(i + 1, 1));
38  }
39  EXPECT_EQ(
40      sum * 1.0 / buffer.kSize,
41      GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), buffer.kSize));
42  buffer.Push(MakeBytesAndDuration(100, 1));
43  EXPECT_EQ(
44      (sum * 1.0 - 1 + 100) / buffer.kSize,
45      GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), buffer.kSize));
46}
47
48}  // namespace internal
49}  // namespace v8
50