1// Copyright 2013 The Chromium 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 "base/time/time.h"
6#include "media/base/audio_bus.h"
7#include "media/base/fake_audio_render_callback.h"
8#include "testing/gtest/include/gtest/gtest.h"
9#include "testing/perf/perf_test.h"
10
11namespace media {
12
13static const int kBenchmarkIterations = 20;
14
15template <typename T>
16void RunInterleaveBench(AudioBus* bus, const std::string& trace_name) {
17  const int frame_size = bus->frames() * bus->channels();
18  scoped_ptr<T[]> interleaved(new T[frame_size]);
19  const int byte_size = sizeof(T);
20
21  base::TimeTicks start = base::TimeTicks::HighResNow();
22  for (int i = 0; i < kBenchmarkIterations; ++i) {
23    bus->ToInterleaved(bus->frames(), byte_size, interleaved.get());
24  }
25  double total_time_milliseconds =
26      (base::TimeTicks::HighResNow() - start).InMillisecondsF();
27  perf_test::PrintResult(
28      "audio_bus_to_interleaved", "", trace_name,
29      total_time_milliseconds / kBenchmarkIterations, "ms", true);
30
31  start = base::TimeTicks::HighResNow();
32  for (int i = 0; i < kBenchmarkIterations; ++i) {
33    bus->FromInterleaved(interleaved.get(), bus->frames(), byte_size);
34  }
35  total_time_milliseconds =
36      (base::TimeTicks::HighResNow() - start).InMillisecondsF();
37  perf_test::PrintResult(
38      "audio_bus_from_interleaved", "", trace_name,
39      total_time_milliseconds / kBenchmarkIterations, "ms", true);
40}
41
42// Benchmark the FromInterleaved() and ToInterleaved() methods.
43TEST(AudioBusPerfTest, Interleave) {
44  scoped_ptr<AudioBus> bus = AudioBus::Create(2, 48000 * 120);
45  FakeAudioRenderCallback callback(0.2);
46  callback.Render(bus.get(), 0);
47
48  RunInterleaveBench<int8>(bus.get(), "int8");
49  RunInterleaveBench<int16>(bus.get(), "int16");
50  RunInterleaveBench<int32>(bus.get(), "int32");
51}
52
53} // namespace media
54