PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/test/storage/performance/bucket.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 140 lines | 93 code | 26 blank | 21 comment | 6 complexity | 8c7dd61f786d66025fe116ba15727c2d MD5 | raw file
  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. #include <algorithm>
  23. #include <chrono>
  24. #include <cstdlib>
  25. #include <memory>
  26. #include <gmock/gmock.h>
  27. #include <gtest/gtest.h>
  28. #include <storage/InMemoryStorageBackend.h>
  29. #include <types/Policy.h>
  30. #include <types/PolicyBucket.h>
  31. #include <types/PolicyKey.h>
  32. #include <types/PolicyType.h>
  33. #include "../../Benchmark.h"
  34. using namespace Cynara;
  35. class PolicyKeyGenerator {
  36. public:
  37. typedef std::vector<std::string> Features;
  38. typedef std::reference_wrapper<Features> FeaturesRef;
  39. PolicyKeyGenerator(size_t featuresCount, size_t featureLength) {
  40. const std::vector<FeaturesRef> toGenerate = { m_clients, m_users, m_privileges };
  41. for (auto featuresRef : toGenerate) {
  42. auto &features = featuresRef.get();
  43. features.resize(featuresCount);
  44. std::generate(std::begin(features), std::end(features),
  45. std::bind(&PolicyKeyGenerator::randomKeyFeature, this, featureLength));
  46. }
  47. }
  48. PolicyKey randomKey(void) const {
  49. return { m_clients.at(rand() % m_clients.size()),
  50. m_users.at(rand() % m_users.size()),
  51. m_privileges.at(rand() % m_privileges.size())
  52. };
  53. }
  54. char randomChar(void) const {
  55. return (std::rand() % ('z' - 'a')) + 'a';
  56. }
  57. std::string randomKeyFeature(size_t length) const {
  58. std::string str(length, 0);
  59. std::generate_n( str.begin(), length, std::bind(&PolicyKeyGenerator::randomChar, this));
  60. return str;
  61. }
  62. private:
  63. Features m_clients;
  64. Features m_users;
  65. Features m_privileges;
  66. };
  67. TEST(Performance, bucket_filtered_100000) {
  68. using std::chrono::microseconds;
  69. PolicyBucket bucket("test");
  70. PolicyKeyGenerator generator(100, 10);
  71. const std::size_t policyNumber = 100000;
  72. for (std::size_t i = 0; i < policyNumber; ++i) {
  73. bucket.insertPolicy(std::make_shared<Policy>(generator.randomKey(),
  74. PredefinedPolicyType::ALLOW));
  75. }
  76. const unsigned int measureRepeats = 1000;
  77. auto result = Benchmark::measure<microseconds>([&bucket, &generator, measureRepeats] () {
  78. for (auto i = 0u; i < measureRepeats; ++i) {
  79. bucket.filtered(generator.randomKey());
  80. }
  81. });
  82. auto key = std::string("performance_" + std::to_string(policyNumber));
  83. auto value = std::to_string(result.count() / measureRepeats) + " [us]";
  84. RecordProperty(key, value);
  85. }
  86. TEST(Performance, bucket_hasBucket) {
  87. using std::chrono::microseconds;
  88. InMemoryStorageBackend backend("/some/fake/path"); // don't use load() or save()
  89. for (auto i = 0u; i < 1000; ++i) {
  90. backend.createBucket("b" + std::to_string(i), Cynara::PredefinedPolicyType::DENY);
  91. }
  92. const unsigned int measureRepeats = 5000;
  93. auto result = Benchmark::measure<microseconds>([&backend, measureRepeats] () {
  94. for (auto i = 0u; i < measureRepeats; ++i) {
  95. backend.hasBucket("b" + std::to_string(i));
  96. }
  97. });
  98. auto key = std::string("performance");
  99. auto value = std::to_string(result.count() / measureRepeats) + " [us]";
  100. RecordProperty(key, value);
  101. }
  102. TEST(Performance, bucket_createBucket) {
  103. using std::chrono::microseconds;
  104. InMemoryStorageBackend backend("/some/fake/path"); // don't use load() or save()
  105. const unsigned int measureRepeats = 1000;
  106. auto result = Benchmark::measure<microseconds>([&backend, measureRepeats] () {
  107. for (auto i = 0u; i < measureRepeats; ++i) {
  108. backend.createBucket("b" + std::to_string(i), Cynara::PredefinedPolicyType::DENY);
  109. }
  110. });
  111. auto key = std::string("performance");
  112. auto value = std::to_string(result.count() / measureRepeats) + " [us]";
  113. RecordProperty(key, value);
  114. }