summaryrefslogtreecommitdiff
path: root/test/utils.hpp
blob: f758f1df0f3e20e3b815652f3cfbed7f8a113e09 (plain)
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
#include <string>
#include <vector>
#include <unordered_map>
#include "fieldstat.h"

const struct fieldstat_tag TEST_TAG_STRING = {"STRING KEY_", TAG_CSTRING, {.value_str = "100.1"}};
const struct fieldstat_tag_list TEST_TAG_LIST_STRING = {(struct fieldstat_tag *)&TEST_TAG_STRING, 1};
const struct fieldstat_tag TEST_TAG_STRING_collided = {"collided", TAG_CSTRING, {.value_str = "2"}};
const struct fieldstat_tag TEST_TAG_INT = {"INT key_", TAG_INTEGER, {.value_longlong = 100}};
const struct fieldstat_tag_list TEST_TAG_LIST_INT = {(struct fieldstat_tag *)&TEST_TAG_INT, 1};
const struct fieldstat_tag TEST_TAG_INT_collided = {"collided", TAG_INTEGER, {.value_longlong = 2}};
const struct fieldstat_tag TEST_TAG_DOUBLE = {"DOUBLE key_", TAG_DOUBLE, {.value_double = 100.1}};
const struct fieldstat_tag_list TEST_TAG_LIST_DOUBLE = {(struct fieldstat_tag *)&TEST_TAG_DOUBLE, 1};
const struct fieldstat_tag TEST_TAG_DOUBLE_collided = {"collided", TAG_DOUBLE, {.value_double = 2.0}};
const struct fieldstat_tag TEST_SHARED_TAG = {"shared", TAG_INTEGER, {.value_longlong = 1}};
const struct timeval TEST_TIMEVAL = {100, 10000};
const long long TEST_TIMEVAL_LONG = 100010; // 100s * 1000 + 10000us / 1000 = 100010ms

std::string gen_rand_string(int len);

class Fieldstat_tag_list_wrapper {
public:
    explicit Fieldstat_tag_list_wrapper(const struct fieldstat_tag_list *tag_list_c);
    explicit Fieldstat_tag_list_wrapper(const char * key, int value);
    explicit Fieldstat_tag_list_wrapper(const char * key, const char *value);
    explicit Fieldstat_tag_list_wrapper(std::uniform_int_distribution<int> &dist, int tag_count);
    std::string to_string() const;
    // std::uniform_int_distribution<int> dist(1,100)
    explicit Fieldstat_tag_list_wrapper();
    Fieldstat_tag_list_wrapper(const Fieldstat_tag_list_wrapper &tag_list_wrapper);
    ~Fieldstat_tag_list_wrapper();
    bool operator==(const Fieldstat_tag_list_wrapper &tag_list_wrapper) const;

    const struct fieldstat_tag *get_tag() const;
    size_t get_tag_count() const;
    const struct fieldstat_tag_list *get_c_struct() const;
    void print_tag_list() const;
    Fieldstat_tag_list_wrapper& sort_tag_list();
private:
    struct fieldstat_tag_list tag_list_c;
};


double test_cal_topk_accuracy(std::vector<struct Fieldstat_tag_list_wrapper *> &test_result, std::unordered_map<std::string, int> &expected_count);

// after we change fieldstat_counter_get return a error code in, all the tests should change correspondingly, so just use a adapter aliasing the old function
long long my_fieldstat_counter_get(const struct fieldstat *instance, int cube_id, int metric_id, int cell_id);


struct Flow {
    std::string src_ip;
    std::string dst_ip;
};

class SpreadSketchZipfGenerator {
private:
    const int MAX_DATA = 1000000;
    std::pair<std::string, std::string> *loadeds;
    unsigned cursor;

public:
    SpreadSketchZipfGenerator(double alpha, int n);
    struct Flow next();
    ~SpreadSketchZipfGenerator();

    double _alpha;
    int _n;
};