"Fossies" - the Fresh Open Source Software Archive 
Member "snort3_extra-3.1.53.0/container_comparisons/include/common_key.h" (20 Dec 2022, 2952 Bytes) of package /linux/misc/snort3_extra-3.1.53.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "common_key.h" see the
Fossies "Dox" file reference documentation.
1 //--------------------------------------------------------------------------
2 // Copyright (C) 2019-2022 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation. You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 //--------------------------------------------------------------------------
18
19 // common_key.h author Devendra Dahiphale <ddahipha@cisco.com>
20 #ifndef COMMON_KEY_H
21 #define COMMON_KEY_H
22
23 #include <iostream>
24 #include <iomanip>
25 #include <vector>
26 #include <chrono>
27 #include <stdexcept>
28 #include <sstream>
29
30 using namespace std;
31
32 #define UNIQUE_KEYS_MAX 13
33
34 // the number of keys we will generate (the size of the test)
35 // 10 million cumulative operations on each container for every test
36 #define KEYS_MAX 10000000
37
38 #define TOTAL_TEST_RUNS 100
39
40 // For avoiding overflow for multiple run of the test
41 #define SCALE_DOWN_FACTOR 10000
42
43 template<class F, class KEY_TYPE>
44 auto time_test(F&& f, vector<int> function_ids, const vector<KEY_TYPE> keys)
45 {
46 int index = 0;
47 auto start_time = chrono::system_clock::now();
48
49 for (auto const& key : keys)
50 {
51 f(key, function_ids[index]);
52 index++;
53 }
54
55 auto stop_time = chrono::system_clock::now();
56 auto diff = stop_time - start_time;
57 return diff;
58 }
59
60 struct ReportKey
61 {
62 size_t total_keys;
63 int miss_chance;
64 };
65
66 std::ostream& operator<<(std::ostream& os, const ReportKey& key)
67 {
68 return os << "miss=" << setw(4) << key.miss_chance << "%";
69 }
70
71
72 // use a virtual method to prevent the optimizer from detecting that our sink function actually does nothing. otherwise it might skew the test
73 struct DataUser
74 {
75 virtual void sink(const int &) = 0;
76 virtual ~DataUser() = default;
77 };
78
79 struct RealDataUser : DataUser
80 {
81 virtual void sink(const int &) override
82 {
83 }
84 };
85
86 struct RealDataUserPrint : DataUser
87 {
88 virtual void sink(const int &data) override
89 {
90 cout << data << endl;
91 }
92 };
93
94 // this is a runtime operation and therefore prevents the optimizer from realizing that the sink does nothing
95 std::unique_ptr<DataUser> make_sink(const int &id)
96 {
97 if (id == 1)
98 return make_unique<RealDataUserPrint>();
99 if (id == 2)
100 return make_unique<RealDataUser>();
101 throw logic_error("wrong sink type");
102 }
103 #endif