blob: 7d9c23c50ac180e60c3498e59c7ab20792f7138f [file]
/*
* Copyright (C) 2026 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 <dirent.h>
#include <sys/stat.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "Diagnostics.h"
#include "android-base/file.h"
#include "android-base/logging.h"
#include "benchmark/benchmark.h"
#include "cmd/Compile.h"
#include "cmd/Convert.h"
#include "cmd/Link.h"
#include "cmd/Optimize.h"
using namespace aapt;
// Generate dummy data that compresses well but takes CPU time.
static void CreateCompressibleFile(const std::string& path, size_t size) {
std::string buffer(size, '\0');
// True random data prevents zlib from finding any matches, forcing it to scan the full sliding
// window continuously.
for (size_t i = 0; i < size * 85 / 100; ++i) {
buffer[i] = rand();
}
// The remaining 15% are zeros, making the file technically compressible (>10%).
android::base::WriteStringToFile(buffer, path);
}
static void RunPipeline(benchmark::State& state, bool throughput_compression) {
TemporaryDir temp_dir;
std::string res_dir = std::string(temp_dir.path) + "/res";
std::string raw_dir = res_dir + "/raw";
std::string assets_dir = std::string(temp_dir.path) + "/assets";
CHECK_EQ(0, mkdir(res_dir.c_str(), 0777));
CHECK_EQ(0, mkdir(raw_dir.c_str(), 0777));
CHECK_EQ(0, mkdir(assets_dir.c_str(), 0777));
std::string manifest_path = std::string(temp_dir.path) + "/AndroidManifest.xml";
android::base::WriteStringToFile(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" "
"package=\"com.test\">\n"
" <application/>\n"
"</manifest>",
manifest_path);
// Create several large files that compress very well (10MB total).
for (int i = 0; i < 10; ++i) {
CreateCompressibleFile(raw_dir + "/file" + std::to_string(i) + ".bin", 1024 * 1024);
}
// Create several large APK files in assets that take CPU time to compress (10MB total).
for (int i = 0; i < 5; ++i) {
CreateCompressibleFile(assets_dir + "/embedded" + std::to_string(i) + ".apk", 2 * 1024 * 1024);
}
std::string compiled_dir = std::string(temp_dir.path) + "/compiled";
CHECK_EQ(0, mkdir(compiled_dir.c_str(), 0777));
std::string linked_apk = std::string(temp_dir.path) + "/linked.apk";
std::string optimized_apk = std::string(temp_dir.path) + "/optimized.apk";
std::string converted_apk = std::string(temp_dir.path) + "/converted.apk";
StdErrDiagnostics diag;
// Compile
CompileCommand compile_cmd(&diag);
std::vector<android::StringPiece> compile_args = {"--dir", res_dir, "-o", compiled_dir};
CHECK_EQ(0, compile_cmd.Execute(compile_args, &std::cerr));
std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(compiled_dir.c_str()), closedir);
std::vector<std::string> flat_files;
struct dirent* entry;
while ((entry = readdir(dir.get()))) {
std::string name = entry->d_name;
if (name.find(".flat") != std::string::npos) {
flat_files.push_back(compiled_dir + "/" + name);
}
}
for (auto _ : state) {
// Link and output proto format
LinkCommand link_cmd(&diag);
std::vector<android::StringPiece> link_args = {
"--manifest", manifest_path, "-A", assets_dir, "--proto-format", "-o", linked_apk};
if (throughput_compression) {
link_args.emplace_back("-Othroughput");
}
for (const auto& file : flat_files) {
link_args.push_back(file);
}
CHECK_EQ(0, link_cmd.Execute(link_args, &std::cerr));
// Optimize
OptimizeCommand optimize_cmd;
std::vector<android::StringPiece> optimize_args = {"-o", optimized_apk, linked_apk};
if (throughput_compression) {
optimize_args.emplace_back("-Othroughput");
}
CHECK_EQ(0, optimize_cmd.Execute(optimize_args, &std::cerr));
// Convert to binary XML format
ConvertCommand convert_cmd;
std::vector<android::StringPiece> convert_args = {"-o", converted_apk, "--output-format",
"binary", optimized_apk};
if (throughput_compression) {
convert_args.emplace_back("-Othroughput");
}
CHECK_EQ(0, convert_cmd.Execute(convert_args, &std::cerr));
}
}
static void BM_LinkOptimizeConvertPipeline(benchmark::State& state) {
RunPipeline(state, false /* throughput_compression */);
}
BENCHMARK(BM_LinkOptimizeConvertPipeline);
static void BM_LinkOptimizeConvertPipeline_Othroughput(benchmark::State& state) {
RunPipeline(state, true /* throughput_compression */);
}
BENCHMARK(BM_LinkOptimizeConvertPipeline_Othroughput);