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.
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"
13 static const int kBenchmarkIterations
= 100;
16 void 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(*interleaved
);
21 base::TimeTicks start
= base::TimeTicks::HighResNow();
22 for (int i
= 0; i
< kBenchmarkIterations
; ++i
) {
23 bus
->ToInterleaved(bus
->frames(), byte_size
, interleaved
.get());
25 double total_time_seconds
=
26 (base::TimeTicks::HighResNow() - start
).InSecondsF();
27 perf_test::PrintResult(
28 "audio_bus_to_interleaved", "", trace_name
,
29 kBenchmarkIterations
/ total_time_seconds
, "runs/s", true);
31 start
= base::TimeTicks::HighResNow();
32 for (int i
= 0; i
< kBenchmarkIterations
; ++i
) {
33 bus
->FromInterleaved(interleaved
.get(), bus
->frames(), byte_size
);
35 total_time_seconds
= (base::TimeTicks::HighResNow() - start
).InSecondsF();
36 perf_test::PrintResult(
37 "audio_bus_from_interleaved", "", trace_name
,
38 kBenchmarkIterations
/ total_time_seconds
, "runs/s", true);
41 // Benchmark the FromInterleaved() and ToInterleaved() methods.
42 TEST(AudioBusPerfTest
, Interleave
) {
43 scoped_ptr
<AudioBus
> bus
= AudioBus::Create(2, 48000 * 120);
44 FakeAudioRenderCallback
callback(0.2);
45 callback
.Render(bus
.get(), 0);
47 RunInterleaveBench
<int8
>(bus
.get(), "int8");
48 RunInterleaveBench
<int16
>(bus
.get(), "int16");
49 RunInterleaveBench
<int32
>(bus
.get(), "int32");