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. 58 ZipString name("thisFileNameDoesNotExist"); 59 60 // Start the benchmark. 61 while (state.KeepRunning()) { 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; 74 ZipString name; 75 76 while (state.KeepRunning()) { 77 OpenArchive(temp_file->path, &handle); 78 StartIteration(handle, &iteration_cookie, nullptr, nullptr); 79 while (Next(iteration_cookie, &data, &name) == 0) { 80 } 81 EndIteration(iteration_cookie); 82 CloseArchive(handle); 83 } 84 } 85 BENCHMARK(Iterate_all_files); 86 87 BENCHMARK_MAIN(); 88