about summary refs log tree commit diff stats
path: root/archive/2025/summer/bsc_karidas/benchmarks/BenchmarkUtils.cpp
blob: 070d0672ceb7e6b514555b788fd4a45e71be3338 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "BenchmarkUtils.hpp"

LatencyCollector appendLogEntries(LoggingManager &loggingManager, const std::vector<BatchWithDestination> &batches)
{
    LatencyCollector localCollector;
    // Pre-allocate to avoid reallocations during measurement
    localCollector.reserve(batches.size());

    auto token = loggingManager.createProducerToken();

    for (const auto &batchWithDest : batches)
    {
        // Measure latency for each appendBatch call
        auto startTime = std::chrono::high_resolution_clock::now();

        bool success = loggingManager.appendBatch(batchWithDest.first, token, batchWithDest.second);

        auto endTime = std::chrono::high_resolution_clock::now();
        auto latency = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime);

        // Record the latency measurement in thread-local collector
        localCollector.addMeasurement(latency);

        if (!success)
        {
            std::cerr << "Failed to append batch of " << batchWithDest.first.size() << " entries to "
                      << (batchWithDest.second ? *batchWithDest.second : "default") << std::endl;
        }
    }

    return localCollector;
}

void cleanupLogDirectory(const std::string &logDir)
{
    try
    {
        if (std::filesystem::exists(logDir))
        {
            std::filesystem::remove_all(logDir);
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << "Error cleaning log directory: " << e.what() << std::endl;
    }
}

size_t calculateTotalDataSize(const std::vector<BatchWithDestination> &batches, int numProducers)
{
    size_t totalSize = 0;

    for (const auto &batchWithDest : batches)
    {
        for (const auto &entry : batchWithDest.first)
        {
            totalSize += entry.serialize().size();
        }
    }

    return totalSize * numProducers;
}

size_t calculateDirectorySize(const std::string &dirPath)
{
    size_t totalSize = 0;
    for (const auto &entry : std::filesystem::recursive_directory_iterator(dirPath))
    {
        if (entry.is_regular_file())
        {
            totalSize += std::filesystem::file_size(entry.path());
        }
    }
    return totalSize;
}

std::vector<BatchWithDestination> generateBatches(
    int numEntries,
    int numSpecificFiles,
    int batchSize,
    int payloadSize)
{
    std::vector<BatchWithDestination> batches;

    // Generate specific filenames
    std::vector<std::string> specificFilenames;
    for (int i = 0; i < numSpecificFiles; i++)
    {
        specificFilenames.push_back("specific_log_file" + std::to_string(i + 1) + ".log");
    }

    int totalChoices = numSpecificFiles + 1; // +1 for default (std::nullopt)
    int generated = 0;
    int destinationIndex = 0;

    // Random number generation setup
    std::random_device rd;
    std::mt19937 rng(rd());

    // Define pools similar to compressionRatio.cpp
    std::vector<std::string> userIds;
    for (int i = 1; i <= 1000; ++i)
    {
        userIds.push_back("user_" + std::to_string(i));
    }

    std::vector<std::string> attributes = {
        "profile", "settings", "history", "preferences", "contacts",
        "messages", "photos", "documents", "videos", "audio"};

    std::vector<std::string> controllerIds;
    for (int i = 1; i <= 10; ++i)
    {
        controllerIds.push_back("controller_" + std::to_string(i));
    }

    std::vector<std::string> processorIds;
    for (int i = 1; i <= 20; ++i)
    {
        processorIds.push_back("processor_" + std::to_string(i));
    }

    std::vector<std::string> wordList = {
        "the", "data", //"to", "and", "user","is", "in", "for", "of", "access",
        //"system", "time", "log", "with", "on", "from", "request", "error", "file", "server",
        //"update", "status", "by", "at", "process", "information", "new", "this", "connection", "failed",
        //"success", "operation", "id", "network", "event", "application", "check", "value", "into", "service",
        //"query", "response", "get", "set", "action", "report", "now", "client", "device", "start"
    };

    // Zipfian distribution for payload words
    std::vector<double> weights;
    for (size_t k = 0; k < wordList.size(); ++k)
    {
        weights.push_back(1.0 / (k + 1.0));
    }
    std::discrete_distribution<size_t> wordDist(weights.begin(), weights.end());

    // Generate power-of-2 sizes for variable payload
    std::vector<size_t> powerOf2Sizes;
    int minPowerOf2 = 5; // 2^5 = 32
    int maxPowerOf2 = static_cast<int>(std::log2(payloadSize));
    for (int power = minPowerOf2; power <= maxPowerOf2; power++)
    {
        powerOf2Sizes.push_back(1 << power); // 2^power
    }

    // Distributions for random selections
    std::uniform_int_distribution<int> actionDist(0, 3); // CREATE, READ, UPDATE, DELETE
    std::uniform_int_distribution<size_t> userDist(0, userIds.size() - 1);
    std::uniform_int_distribution<size_t> attrDist(0, attributes.size() - 1);
    std::uniform_int_distribution<size_t> controllerDist(0, controllerIds.size() - 1);
    std::uniform_int_distribution<size_t> processorDist(0, processorIds.size() - 1);
    std::uniform_int_distribution<size_t> powerOf2SizeDist(0, powerOf2Sizes.size() - 1);

    while (generated < numEntries)
    {
        int currentBatchSize = std::min(batchSize, numEntries - generated);

        // Assign destination in round-robin manner
        std::optional<std::string> targetFilename = std::nullopt;
        if (destinationIndex % totalChoices > 0)
        {
            targetFilename = specificFilenames[(destinationIndex % totalChoices) - 1];
        }

        // Generate the batch
        std::vector<LogEntry> batch;
        batch.reserve(currentBatchSize);
        for (int i = 0; i < currentBatchSize; i++)
        {
            // Generate realistic log entry
            auto action = static_cast<LogEntry::ActionType>(actionDist(rng));
            std::string user_id = userIds[userDist(rng)];
            std::string attribute = attributes[attrDist(rng)];
            std::string dataLocation = "user/" + user_id + "/" + attribute;
            std::string dataSubjectId = user_id;
            std::string dataControllerId = controllerIds[controllerDist(rng)];
            std::string dataProcessorId = processorIds[processorDist(rng)];

            // Determine targetSize
            size_t targetSize = static_cast<size_t>(payloadSize);

            // Build payload
            std::string payloadStr;
            while (payloadStr.size() < targetSize)
            {
                if (!payloadStr.empty())
                    payloadStr += " ";
                size_t wordIndex = wordDist(rng);
                payloadStr += wordList[wordIndex];
            }
            if (payloadStr.size() > targetSize)
            {
                payloadStr = payloadStr.substr(0, targetSize);
            }
            std::vector<uint8_t> payload(payloadStr.begin(), payloadStr.end());

            LogEntry entry(action,
                           dataLocation,
                           dataControllerId,
                           dataProcessorId,
                           dataSubjectId,
                           std::move(payload));
            batch.push_back(std::move(entry));
        }

        batches.push_back({std::move(batch), targetFilename});
        generated += currentBatchSize;
        destinationIndex++; // Move to the next destination
    }

    return batches;
}

LatencyStats calculateLatencyStats(const LatencyCollector &collector)
{
    const auto &latencies = collector.getMeasurements();

    if (latencies.empty())
    {
        return {0.0, 0.0, 0.0, 0};
    }

    // Convert to milliseconds for easier reading
    std::vector<double> latenciesMs;
    latenciesMs.reserve(latencies.size());
    for (const auto &lat : latencies)
    {
        latenciesMs.push_back(static_cast<double>(lat.count()) / 1e6); // ns to ms
    }

    // Sort for percentile calculations
    std::sort(latenciesMs.begin(), latenciesMs.end());

    LatencyStats stats;
    stats.count = latenciesMs.size();
    stats.maxMs = latenciesMs.back();
    stats.avgMs = std::accumulate(latenciesMs.begin(), latenciesMs.end(), 0.0) / latenciesMs.size();

    // Median
    size_t medianIdx = latenciesMs.size() / 2;
    if (latenciesMs.size() % 2 == 0)
    {
        stats.medianMs = (latenciesMs[medianIdx - 1] + latenciesMs[medianIdx]) / 2.0;
    }
    else
    {
        stats.medianMs = latenciesMs[medianIdx];
    }

    return stats;
}

void printLatencyStats(const LatencyStats &stats)
{
    std::cout << "============== Latency Statistics ==============" << std::endl;
    std::cout << "Total append operations: " << stats.count << std::endl;
    std::cout << "Max latency: " << stats.maxMs << " ms" << std::endl;
    std::cout << "Average latency: " << stats.avgMs << " ms" << std::endl;
    std::cout << "Median latency: " << stats.medianMs << " ms" << std::endl;
    std::cout << "===============================================" << std::endl;
}