1 /*
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>
20 #include <memory>
21 #include <string>
22 #include <string_view>
23 #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
CreateZip(int size=4,int count=1000,bool compress=true)31 static std::unique_ptr<TemporaryFile> CreateZip(int size = 4, int count = 1000,
32 bool compress = true) {
33 auto result = std::make_unique<TemporaryFile>();
34 FILE* fp = fdopen(result->fd, "w");
35
36 ZipWriter writer(fp);
37 std::string baseName = "file";
38 for (size_t i = 0; i < count; i++) {
39 // Make file names longer and longer.
40 if (i && (i % 100 == 0)) {
41 baseName += "more";
42 }
43 std::string name = baseName + std::to_string(i);
44 writer.StartEntry(name.c_str(), compress ? ZipWriter::kCompress : 0);
45 while (size > 0) {
46 writer.WriteBytes("helo", 4);
47 size -= 4;
48 }
49 writer.FinishEntry();
50 }
51 writer.Finish();
52 fclose(fp);
53
54 return result;
55 }
56
OpenClose(benchmark::State & state)57 static 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 }
65 BENCHMARK(OpenClose)->Arg(1)->Arg(10)->Arg(1000)->Arg(10000);
66
FindEntry_no_match(benchmark::State & state)67 static void FindEntry_no_match(benchmark::State& state) {
68 // Create a temporary zip archive.
69 std::unique_ptr<TemporaryFile> temp_file(CreateZip(4, int(state.range(0))));
70 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.
75 std::string_view name("thisFileNameDoesNotExist");
76
77 // Start the benchmark.
78 OpenArchive(temp_file->path, &handle);
79 for (auto _ : state) {
80 FindEntry(handle, name, &data);
81 }
82 CloseArchive(handle);
83 }
84 BENCHMARK(FindEntry_no_match)->Arg(1)->Arg(10)->Arg(1000)->Arg(10000);
85
Iterate_all_files(benchmark::State & state)86 static void Iterate_all_files(benchmark::State& state) {
87 std::unique_ptr<TemporaryFile> temp_file(CreateZip(4, int(state.range(0))));
88 ZipArchiveHandle handle;
89 void* iteration_cookie;
90 ZipEntry data;
91 std::string_view name;
92
93 OpenArchive(temp_file->path, &handle);
94 for (auto _ : state) {
95 StartIteration(handle, &iteration_cookie);
96 while (Next(iteration_cookie, &data, &name) == 0) {
97 }
98 EndIteration(iteration_cookie);
99 }
100 CloseArchive(handle);
101 }
102 BENCHMARK(Iterate_all_files)->Arg(1)->Arg(10)->Arg(1000)->Arg(10000);
103
StartAlignedEntry(benchmark::State & state)104 static 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();
116 writer.FinishEntry();
117 state.ResumeTiming();
118 }
119
120 writer.Finish();
121 fclose(fp);
122 }
123 BENCHMARK(StartAlignedEntry)->Arg(2)->Arg(16)->Arg(1024)->Arg(4096);
124
ExtractEntry(benchmark::State & state)125 static void ExtractEntry(benchmark::State& state) {
126 const auto size = int(state.range(0));
127 std::unique_ptr<TemporaryFile> temp_file(CreateZip(size * 1024, 1));
128
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
138 std::vector<uint8_t> buffer(size * 1024);
139 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
148 BENCHMARK(ExtractEntry)->Arg(2)->Arg(16)->Arg(64)->Arg(1024)->Arg(4096);
149
ExtractStored(benchmark::State & state)150 static 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
173 BENCHMARK(ExtractStored)->Arg(2)->Arg(16)->Arg(64)->Arg(1024)->Arg(4096);
174
175 BENCHMARK_MAIN();
176