1e639663ce0ec075bddfc9be442a1cedfaef6d5a
[platform/core/security/cynara.git] / test / storage / performance / bucket.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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 /**
17  * @file        test/storage/performance/bucket.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Performance tests for Cynara::PolicyBucket
21  */
22
23 #include <algorithm>
24 #include <chrono>
25 #include <cstdlib>
26 #include <memory>
27
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30
31 #include <types/Policy.h>
32 #include <types/PolicyBucket.h>
33 #include <types/PolicyKey.h>
34 #include <types/PolicyType.h>
35
36 #include "../../Benchmark.h"
37
38 using namespace Cynara;
39
40 class Benchmark {
41 public:
42
43 };
44
45 class PolicyKeyGenerator {
46 public:
47     typedef std::vector<std::string> Features;
48     typedef std::reference_wrapper<Features> FeaturesRef;
49
50     PolicyKeyGenerator(size_t featuresCount, size_t featureLength) {
51         const std::vector<FeaturesRef> toGenerate = { m_clients, m_users, m_privileges };
52         for (auto featuresRef : toGenerate) {
53             auto &features = featuresRef.get();
54             features.resize(featuresCount);
55             std::generate(std::begin(features), std::end(features),
56                 std::bind(&PolicyKeyGenerator::randomKeyFeature, this, featureLength));
57         }
58     }
59
60     PolicyKey randomKey(void) const {
61         return { m_clients.at(rand() % m_clients.size()),
62                  m_users.at(rand() % m_users.size()),
63                  m_privileges.at(rand() % m_privileges.size())
64         };
65     }
66
67     char randomChar(void) const {
68         return (std::rand() % ('z' - 'a')) + 'a';
69     }
70
71     std::string randomKeyFeature(size_t length) const {
72         std::string str(length, 0);
73         std::generate_n( str.begin(), length, std::bind(&PolicyKeyGenerator::randomChar, this));
74         return str;
75     }
76
77 private:
78     Features m_clients;
79     Features m_users;
80     Features m_privileges;
81 };
82
83 TEST(Performance, bucket_filtered_100000) {
84     using std::chrono::microseconds;
85
86     PolicyBucket bucket;
87
88     PolicyKeyGenerator generator(100, 10);
89
90     const std::size_t policyNumber = 100000;
91     for (std::size_t i = 0; i < policyNumber; ++i) {
92         bucket.insertPolicy(std::make_shared<Policy>(generator.randomKey(),
93                             PredefinedPolicyType::ALLOW));
94     }
95
96     const unsigned int measureRepeats = 1000;
97     auto result = Benchmark::measure<microseconds>([&bucket, &generator, measureRepeats] () {
98         for (auto i = 0u; i < measureRepeats; ++i) {
99             bucket.filtered(generator.randomKey());
100         }
101     });
102
103     auto key = std::string("performance_" + std::to_string(policyNumber));
104     auto value = std::to_string(result.count() / measureRepeats) + " [us]";
105     RecordProperty(key, value);
106 }