|
17 | 17 | #include <cstdio>
|
18 | 18 | #include <cstdlib>
|
19 | 19 | #include <cstring>
|
20 |
| -#include <iostream> |
21 | 20 | #include <string>
|
22 | 21 | #include <tuple>
|
23 | 22 | #include <vector>
|
|
28 | 27 | #include <ziparchive/zip_archive_stream_entry.h>
|
29 | 28 | #include <ziparchive/zip_writer.h>
|
30 | 29 |
|
31 |
| -static TemporaryFile* CreateZip() { |
32 |
| - TemporaryFile* result = new TemporaryFile; |
| 30 | +static std::unique_ptr<TemporaryFile> CreateZip(int size = 4, int count = 1000) { |
| 31 | + auto result = std::make_unique<TemporaryFile>(); |
33 | 32 | FILE* fp = fdopen(result->fd, "w");
|
34 | 33 |
|
35 | 34 | ZipWriter writer(fp);
|
36 | 35 | std::string lastName = "file";
|
37 |
| - for (size_t i = 0; i < 1000; i++) { |
| 36 | + for (size_t i = 0; i < count; i++) { |
38 | 37 | // Make file names longer and longer.
|
39 | 38 | lastName = lastName + std::to_string(i);
|
40 | 39 | writer.StartEntry(lastName.c_str(), ZipWriter::kCompress);
|
41 |
| - writer.WriteBytes("helo", 4); |
| 40 | + while (size > 0) { |
| 41 | + writer.WriteBytes("helo", 4); |
| 42 | + size -= 4; |
| 43 | + } |
42 | 44 | writer.FinishEntry();
|
43 | 45 | }
|
44 | 46 | writer.Finish();
|
@@ -106,5 +108,28 @@ static void StartAlignedEntry(benchmark::State& state) {
|
106 | 108 | }
|
107 | 109 | BENCHMARK(StartAlignedEntry)->Arg(2)->Arg(16)->Arg(1024)->Arg(4096);
|
108 | 110 |
|
| 111 | +static void ExtractEntry(benchmark::State& state) { |
| 112 | + std::unique_ptr<TemporaryFile> temp_file(CreateZip(1024 * 1024, 1)); |
| 113 | + |
| 114 | + ZipArchiveHandle handle; |
| 115 | + ZipEntry data; |
| 116 | + if (OpenArchive(temp_file->path, &handle)) { |
| 117 | + state.SkipWithError("Failed to open archive"); |
| 118 | + } |
| 119 | + if (FindEntry(handle, "file0", &data)) { |
| 120 | + state.SkipWithError("Failed to find archive entry"); |
| 121 | + } |
| 122 | + |
| 123 | + std::vector<uint8_t> buffer(1024 * 1024); |
| 124 | + for (auto _ : state) { |
| 125 | + if (ExtractToMemory(handle, &data, buffer.data(), uint32_t(buffer.size()))) { |
| 126 | + state.SkipWithError("Failed to extract archive entry"); |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + CloseArchive(handle); |
| 131 | +} |
| 132 | + |
| 133 | +BENCHMARK(ExtractEntry)->Arg(2)->Arg(16)->Arg(1024); |
109 | 134 |
|
110 | 135 | BENCHMARK_MAIN();
|
0 commit comments