audio_bus_perftest.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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 = 100; 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(*interleaved); 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_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); 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_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); 39} 40 41// Benchmark the FromInterleaved() and ToInterleaved() methods. 42TEST(AudioBusPerfTest, Interleave) { 43 scoped_ptr<AudioBus> bus = AudioBus::Create(2, 48000 * 120); 44 FakeAudioRenderCallback callback(0.2); 45 callback.Render(bus.get(), 0); 46 47 RunInterleaveBench<int8>(bus.get(), "int8"); 48 RunInterleaveBench<int16>(bus.get(), "int16"); 49 RunInterleaveBench<int32>(bus.get(), "int32"); 50} 51 52} // namespace media 53