Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 95f6fc6b authored by Jim Shargo's avatar Jim Shargo Committed by Automerger Merge Worker
Browse files

nativewindow: Add C++/Rust benchmarks am: e4680d75 am: e76841b5

parents 1ef3979c e76841b5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ impl AHardwareBuffer {
    /// program termination.
    ///
    /// Available since API level 26.
    #[inline]
    pub fn new(
        width: u32,
        height: u32,
+50 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

cc_defaults {
    name: "nativewindow_benchmark_defaults_cc",
    shared_libs: ["libnativewindow"],
    static_libs: [
        "libbase",
        "libgoogle-benchmark-main",
    ],
    test_suites: [
        "device-tests",
        "NativeWindowBenchmarks",
    ],
}

cc_benchmark {
    name: "nativewindow_buffer_benchmarks_cc",
    srcs: ["buffer_benchmarks.cc"],
    defaults: ["nativewindow_benchmark_defaults_cc"],
}

rust_defaults {
    name: "nativewindow_benchmark_defaults_rs",
    rustlibs: [
        "libnativewindow_rs",
        "libcriterion",
    ],
    test_suites: [
        "device-tests",
        "NativeWindowBenchmarks",
    ],
}

rust_benchmark {
    name: "nativewindow_buffer_benchmarks_rs",
    srcs: ["buffer_benchmarks.rs"],
    defaults: ["nativewindow_benchmark_defaults_rs"],
}
+22 −0
Original line number Diff line number Diff line
# libnativewindow Benchmarks

This directory contains benchmarks for the C++ and Rust variants of
libnativewindow.

## Running

It is currently a little tricky to get statistics from Rust benchmarks directly
from tradefed. But we can hack it by using atest to build/push, then running
the benchmarks by hand to get stats.

```
  $ atest nativewindow_buffer_benchmarks_rs nativewindow_buffer_benchmarks_cc -d
  $ adb shell /data/local/tmp/nativewindow_buffer_benchmarks_cc/x86_64/nativewindow_buffer_benchmarks_cc
  $ adb shell /data/local/tmp/nativewindow_buffer_benchmarks_rs/x86_64/nativewindow_buffer_benchmarks_rs --bench
```

## Results

On a remote emulator, the results we see from the benchmarks from Rust and C++
seem to be roughly equivalent! Allocating/deallocating a 720p buffer takes
~2.3ms on each.
+38 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <android-base/macros.h>
#include <android/hardware_buffer.h>
#include <benchmark/benchmark.h>

static void BM_BufferAllocationDeallocation(benchmark::State& state) {
    AHardwareBuffer_Desc buffer_desc = {.width = 1280,
                                        .height = 720,
                                        .layers = 1,
                                        .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
                                        .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
                                        .stride = 0};
    AHardwareBuffer* buffer = nullptr;
    for (auto _ : state) {
        int status = AHardwareBuffer_allocate(&buffer_desc, &buffer);
        if (UNLIKELY(status != 0)) {
            state.SkipWithError("Unable to allocate buffer.");
        }
        AHardwareBuffer_release(buffer);
        buffer = nullptr;
    }
}
BENCHMARK(BM_BufferAllocationDeallocation);

BENCHMARK_MAIN();
 No newline at end of file
+40 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Benchmark for libnativewindow AHardwareBuffer bindings

#![allow(dead_code)]
#![allow(missing_docs)]

use criterion::*;
use nativewindow::*;

fn allocate_deallocate() {
    let buffer = AHardwareBuffer::new(
        1280,
        720,
        1,
        AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
        AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
    )
    .unwrap();
    drop(buffer);
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("allocate_deallocate", |b| b.iter(allocate_deallocate));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);