| Sebastian Pop | 7a4752d | 2017-05-30 09:14:20 -0500 | [diff] [blame] | 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 <iostream> |
| 21 | #include <string> |
| 22 | #include <tuple> |
| 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 | |
| 31 | static TemporaryFile* CreateZip() { |
| 32 | TemporaryFile* result = new TemporaryFile; |
| 33 | FILE* fp = fdopen(result->fd, "w"); |
| 34 | |
| 35 | ZipWriter writer(fp); |
| 36 | std::string lastName = "file"; |
| 37 | for (size_t i = 0; i < 1000; i++) { |
| 38 | // Make file names longer and longer. |
| 39 | lastName = lastName + std::to_string(i); |
| 40 | writer.StartEntry(lastName.c_str(), ZipWriter::kCompress); |
| 41 | writer.WriteBytes("helo", 4); |
| 42 | writer.FinishEntry(); |
| 43 | } |
| 44 | writer.Finish(); |
| 45 | fclose(fp); |
| 46 | |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | static void FindEntry_no_match(benchmark::State& state) { |
| 51 | // Create a temporary zip archive. |
| 52 | std::unique_ptr<TemporaryFile> temp_file(CreateZip()); |
| 53 | ZipArchiveHandle handle; |
| 54 | ZipEntry data; |
| 55 | |
| 56 | // In order to walk through all file names in the archive, look for a name |
| 57 | // that does not exist in the archive. |
| Elliott Hughes | 0b4bf2f | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 58 | std::string_view name("thisFileNameDoesNotExist"); |
| Sebastian Pop | 7a4752d | 2017-05-30 09:14:20 -0500 | [diff] [blame] | 59 | |
| 60 | // Start the benchmark. |
| Yurii Zubrytskyi | bb0e58f | 2019-06-17 15:43:16 -0700 | [diff] [blame^] | 61 | for (auto _ : state) { |
| Sebastian Pop | 7a4752d | 2017-05-30 09:14:20 -0500 | [diff] [blame] | 62 | OpenArchive(temp_file->path, &handle); |
| 63 | FindEntry(handle, name, &data); |
| 64 | CloseArchive(handle); |
| 65 | } |
| 66 | } |
| 67 | BENCHMARK(FindEntry_no_match); |
| 68 | |
| 69 | static void Iterate_all_files(benchmark::State& state) { |
| 70 | std::unique_ptr<TemporaryFile> temp_file(CreateZip()); |
| 71 | ZipArchiveHandle handle; |
| 72 | void* iteration_cookie; |
| 73 | ZipEntry data; |
| Elliott Hughes | 21f9ab0 | 2019-05-22 18:56:41 -0700 | [diff] [blame] | 74 | std::string name; |
| Sebastian Pop | 7a4752d | 2017-05-30 09:14:20 -0500 | [diff] [blame] | 75 | |
| Yurii Zubrytskyi | bb0e58f | 2019-06-17 15:43:16 -0700 | [diff] [blame^] | 76 | for (auto _ : state) { |
| Sebastian Pop | 7a4752d | 2017-05-30 09:14:20 -0500 | [diff] [blame] | 77 | OpenArchive(temp_file->path, &handle); |
| Elliott Hughes | 4e78d05 | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 78 | StartIteration(handle, &iteration_cookie); |
| Sebastian Pop | 7a4752d | 2017-05-30 09:14:20 -0500 | [diff] [blame] | 79 | while (Next(iteration_cookie, &data, &name) == 0) { |
| 80 | } |
| 81 | EndIteration(iteration_cookie); |
| 82 | CloseArchive(handle); |
| 83 | } |
| 84 | } |
| 85 | BENCHMARK(Iterate_all_files); |
| 86 | |
| Yurii Zubrytskyi | bb0e58f | 2019-06-17 15:43:16 -0700 | [diff] [blame^] | 87 | static void StartAlignedEntry(benchmark::State& state) { |
| 88 | TemporaryFile file; |
| 89 | FILE* fp = fdopen(file.fd, "w"); |
| 90 | |
| 91 | ZipWriter writer(fp); |
| 92 | |
| 93 | auto alignment = uint32_t(state.range(0)); |
| 94 | std::string name = "name"; |
| 95 | int counter = 0; |
| 96 | for (auto _ : state) { |
| 97 | writer.StartAlignedEntry(name + std::to_string(counter++), 0, alignment); |
| 98 | state.PauseTiming(); |
| 99 | writer.WriteBytes("hola", 4); |
| 100 | writer.FinishEntry(); |
| 101 | state.ResumeTiming(); |
| 102 | } |
| 103 | |
| 104 | writer.Finish(); |
| 105 | fclose(fp); |
| 106 | } |
| 107 | BENCHMARK(StartAlignedEntry)->Arg(2)->Arg(16)->Arg(1024)->Arg(4096); |
| 108 | |
| 109 | |
| Elliott Hughes | 2fb01ef | 2017-12-13 18:19:18 -0800 | [diff] [blame] | 110 | BENCHMARK_MAIN(); |