blob: 373ade6d5c17574f1d10e11348e65828a585224b [file] [log] [blame]
Sebastian Pop7a4752d2017-05-30 09:14:20 -05001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080020#include <memory>
Sebastian Pop7a4752d2017-05-30 09:14:20 -050021#include <string>
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080022#include <string_view>
Sebastian Pop7a4752d2017-05-30 09:14:20 -050023#include <vector>
24
25#include <android-base/test_utils.h>
26#include <benchmark/benchmark.h>
27#include <ziparchive/zip_archive.h>
28#include <ziparchive/zip_archive_stream_entry.h>
29#include <ziparchive/zip_writer.h>
30
Yurii Zubrytskyi717921e2022-11-15 23:12:27 -080031static std::unique_ptr<TemporaryFile> CreateZip(int size = 4, int count = 1000,
32 bool compress = true) {
Yurii Zubrytskyie75a3e12020-04-06 19:35:33 -070033 auto result = std::make_unique<TemporaryFile>();
Sebastian Pop7a4752d2017-05-30 09:14:20 -050034 FILE* fp = fdopen(result->fd, "w");
35
36 ZipWriter writer(fp);
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080037 std::string baseName = "file";
Yurii Zubrytskyie75a3e12020-04-06 19:35:33 -070038 for (size_t i = 0; i < count; i++) {
Sebastian Pop7a4752d2017-05-30 09:14:20 -050039 // Make file names longer and longer.
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080040 if (i && (i % 100 == 0)) {
41 baseName += "more";
42 }
43 std::string name = baseName + std::to_string(i);
Yurii Zubrytskyi717921e2022-11-15 23:12:27 -080044 writer.StartEntry(name.c_str(), compress ? ZipWriter::kCompress : 0);
Yurii Zubrytskyie75a3e12020-04-06 19:35:33 -070045 while (size > 0) {
46 writer.WriteBytes("helo", 4);
47 size -= 4;
48 }
Sebastian Pop7a4752d2017-05-30 09:14:20 -050049 writer.FinishEntry();
50 }
51 writer.Finish();
52 fclose(fp);
53
54 return result;
55}
56
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080057static void OpenClose(benchmark::State& state) {
58 std::unique_ptr<TemporaryFile> temp_file(CreateZip(4, int(state.range(0))));
59 ZipArchiveHandle handle;
60 for (auto _ : state) {
61 OpenArchive(temp_file->path, &handle);
62 CloseArchive(handle);
63 }
64}
65BENCHMARK(OpenClose)->Arg(1)->Arg(10)->Arg(1000)->Arg(10000);
66
Sebastian Pop7a4752d2017-05-30 09:14:20 -050067static void FindEntry_no_match(benchmark::State& state) {
68 // Create a temporary zip archive.
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080069 std::unique_ptr<TemporaryFile> temp_file(CreateZip(4, int(state.range(0))));
Sebastian Pop7a4752d2017-05-30 09:14:20 -050070 ZipArchiveHandle handle;
71 ZipEntry data;
72
73 // In order to walk through all file names in the archive, look for a name
74 // that does not exist in the archive.
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -070075 std::string_view name("thisFileNameDoesNotExist");
Sebastian Pop7a4752d2017-05-30 09:14:20 -050076
77 // Start the benchmark.
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080078 OpenArchive(temp_file->path, &handle);
Yurii Zubrytskyibb0e58f2019-06-17 15:43:16 -070079 for (auto _ : state) {
Sebastian Pop7a4752d2017-05-30 09:14:20 -050080 FindEntry(handle, name, &data);
Sebastian Pop7a4752d2017-05-30 09:14:20 -050081 }
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080082 CloseArchive(handle);
Sebastian Pop7a4752d2017-05-30 09:14:20 -050083}
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080084BENCHMARK(FindEntry_no_match)->Arg(1)->Arg(10)->Arg(1000)->Arg(10000);
Sebastian Pop7a4752d2017-05-30 09:14:20 -050085
86static void Iterate_all_files(benchmark::State& state) {
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080087 std::unique_ptr<TemporaryFile> temp_file(CreateZip(4, int(state.range(0))));
Sebastian Pop7a4752d2017-05-30 09:14:20 -050088 ZipArchiveHandle handle;
89 void* iteration_cookie;
90 ZipEntry data;
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080091 std::string_view name;
Sebastian Pop7a4752d2017-05-30 09:14:20 -050092
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -080093 OpenArchive(temp_file->path, &handle);
Yurii Zubrytskyibb0e58f2019-06-17 15:43:16 -070094 for (auto _ : state) {
Elliott Hughes4e78d052019-05-08 10:44:06 -070095 StartIteration(handle, &iteration_cookie);
Sebastian Pop7a4752d2017-05-30 09:14:20 -050096 while (Next(iteration_cookie, &data, &name) == 0) {
97 }
98 EndIteration(iteration_cookie);
Sebastian Pop7a4752d2017-05-30 09:14:20 -050099 }
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -0800100 CloseArchive(handle);
Sebastian Pop7a4752d2017-05-30 09:14:20 -0500101}
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -0800102BENCHMARK(Iterate_all_files)->Arg(1)->Arg(10)->Arg(1000)->Arg(10000);
Sebastian Pop7a4752d2017-05-30 09:14:20 -0500103
Yurii Zubrytskyibb0e58f2019-06-17 15:43:16 -0700104static void StartAlignedEntry(benchmark::State& state) {
105 TemporaryFile file;
106 FILE* fp = fdopen(file.fd, "w");
107
108 ZipWriter writer(fp);
109
110 auto alignment = uint32_t(state.range(0));
111 std::string name = "name";
112 int counter = 0;
113 for (auto _ : state) {
114 writer.StartAlignedEntry(name + std::to_string(counter++), 0, alignment);
115 state.PauseTiming();
Yurii Zubrytskyibb0e58f2019-06-17 15:43:16 -0700116 writer.FinishEntry();
117 state.ResumeTiming();
118 }
119
120 writer.Finish();
121 fclose(fp);
122}
123BENCHMARK(StartAlignedEntry)->Arg(2)->Arg(16)->Arg(1024)->Arg(4096);
124
Yurii Zubrytskyie75a3e12020-04-06 19:35:33 -0700125static void ExtractEntry(benchmark::State& state) {
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -0800126 const auto size = int(state.range(0));
127 std::unique_ptr<TemporaryFile> temp_file(CreateZip(size * 1024, 1));
Yurii Zubrytskyie75a3e12020-04-06 19:35:33 -0700128
129 ZipArchiveHandle handle;
130 ZipEntry data;
131 if (OpenArchive(temp_file->path, &handle)) {
132 state.SkipWithError("Failed to open archive");
133 }
134 if (FindEntry(handle, "file0", &data)) {
135 state.SkipWithError("Failed to find archive entry");
136 }
137
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -0800138 std::vector<uint8_t> buffer(size * 1024);
Yurii Zubrytskyie75a3e12020-04-06 19:35:33 -0700139 for (auto _ : state) {
140 if (ExtractToMemory(handle, &data, buffer.data(), uint32_t(buffer.size()))) {
141 state.SkipWithError("Failed to extract archive entry");
142 break;
143 }
144 }
145 CloseArchive(handle);
146}
147
Yurii Zubrytskyiabb81de2022-11-15 11:41:54 -0800148BENCHMARK(ExtractEntry)->Arg(2)->Arg(16)->Arg(64)->Arg(1024)->Arg(4096);
Yurii Zubrytskyibb0e58f2019-06-17 15:43:16 -0700149
Yurii Zubrytskyi717921e2022-11-15 23:12:27 -0800150static void ExtractStored(benchmark::State& state) {
151 const auto size = int(state.range(0));
152 std::unique_ptr<TemporaryFile> temp_file(CreateZip(size * 1024, 1, false));
153
154 ZipArchiveHandle handle;
155 ZipEntry data;
156 if (OpenArchive(temp_file->path, &handle)) {
157 state.SkipWithError("Failed to open archive");
158 }
159 if (FindEntry(handle, "file0", &data)) {
160 state.SkipWithError("Failed to find archive entry");
161 }
162
163 std::vector<uint8_t> buffer(size * 1024);
164 for (auto _ : state) {
165 if (ExtractToMemory(handle, &data, buffer.data(), uint32_t(buffer.size()))) {
166 state.SkipWithError("Failed to extract archive entry");
167 break;
168 }
169 }
170 CloseArchive(handle);
171}
172
173BENCHMARK(ExtractStored)->Arg(2)->Arg(16)->Arg(64)->Arg(1024)->Arg(4096);
174
Elliott Hughes2fb01ef2017-12-13 18:19:18 -0800175BENCHMARK_MAIN();