zip_archive_benchmark.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include <cstdio>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <iostream>
  20. #include <string>
  21. #include <tuple>
  22. #include <vector>
  23. #include <android-base/test_utils.h>
  24. #include <benchmark/benchmark.h>
  25. #include <ziparchive/zip_archive.h>
  26. #include <ziparchive/zip_archive_stream_entry.h>
  27. #include <ziparchive/zip_writer.h>
  28. static TemporaryFile* CreateZip() {
  29. TemporaryFile* result = new TemporaryFile;
  30. FILE* fp = fdopen(result->fd, "w");
  31. ZipWriter writer(fp);
  32. std::string lastName = "file";
  33. for (size_t i = 0; i < 1000; i++) {
  34. // Make file names longer and longer.
  35. lastName = lastName + std::to_string(i);
  36. writer.StartEntry(lastName.c_str(), ZipWriter::kCompress);
  37. writer.WriteBytes("helo", 4);
  38. writer.FinishEntry();
  39. }
  40. writer.Finish();
  41. fclose(fp);
  42. return result;
  43. }
  44. static void FindEntry_no_match(benchmark::State& state) {
  45. // Create a temporary zip archive.
  46. std::unique_ptr<TemporaryFile> temp_file(CreateZip());
  47. ZipArchiveHandle handle;
  48. ZipEntry data;
  49. // In order to walk through all file names in the archive, look for a name
  50. // that does not exist in the archive.
  51. ZipString name("thisFileNameDoesNotExist");
  52. // Start the benchmark.
  53. while (state.KeepRunning()) {
  54. OpenArchive(temp_file->path, &handle);
  55. FindEntry(handle, name, &data);
  56. CloseArchive(handle);
  57. }
  58. }
  59. BENCHMARK(FindEntry_no_match);
  60. static void Iterate_all_files(benchmark::State& state) {
  61. std::unique_ptr<TemporaryFile> temp_file(CreateZip());
  62. ZipArchiveHandle handle;
  63. void* iteration_cookie;
  64. ZipEntry data;
  65. ZipString name;
  66. while (state.KeepRunning()) {
  67. OpenArchive(temp_file->path, &handle);
  68. StartIteration(handle, &iteration_cookie, nullptr, nullptr);
  69. while (Next(iteration_cookie, &data, &name) == 0) {
  70. }
  71. EndIteration(iteration_cookie);
  72. CloseArchive(handle);
  73. }
  74. }
  75. BENCHMARK(Iterate_all_files);
  76. BENCHMARK_MAIN();