LCOV - code coverage report
Current view: top level - src/libs/tools/benchmarks - benchmark_plugins.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 73 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief benchmark for getenv
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <backendbuilder.hpp>
      11             : #include <kdbconfig.h>
      12             : #include <kdbtimer.hpp>
      13             : 
      14             : #include <unistd.h>
      15             : 
      16             : #include <fstream>
      17             : #include <iostream>
      18             : 
      19             : extern "C" char ** environ;
      20             : 
      21             : 
      22             : const long long nr_keys = 1000LL;
      23             : long long iterations = 100000LL;
      24             : 
      25             : // not needed in benchmarks:
      26           0 : long long iterations1 = iterations / 100;
      27             : 
      28             : const int benchmarkIterations = 11; // is a good number to not need mean values for median
      29             : 
      30           0 : kdb::KeySet toWrite;
      31             : 
      32             : 
      33           0 : __attribute__ ((noinline)) void benchmark_nothing ()
      34             : {
      35           0 :         static Timer t ("nothing");
      36             : 
      37           0 :         t.start ();
      38           0 :         unlink ("/etc/kdb/default.ecf");
      39           0 :         t.stop ();
      40           0 :         std::cout << t;
      41           0 : }
      42             : 
      43             : template <int PLUGINS>
      44           0 : __attribute__ ((noinline)) void benchmark_backend ()
      45             : {
      46             :         using namespace kdb;
      47             :         using namespace kdb::tools;
      48           0 :         static Timer t (std::to_string (PLUGINS) + " mountpoint(s)");
      49             : 
      50           0 :         Key mp ("system/iterate/" + std::to_string (PLUGINS), KEY_END);
      51           0 :         std::string cf = "/tmp/file" + std::to_string (PLUGINS) + ".ecf";
      52           0 :         unlink (cf.c_str ());
      53             : 
      54             :         {
      55           0 :                 KDB kdb;
      56           0 :                 KeySet mountConfig;
      57           0 :                 kdb.get (mountConfig, "system/elektra/mountpoints");
      58             : 
      59           0 :                 Backend b;
      60           0 :                 b.setMountpoint (mp, KeySet (0, KS_END));
      61           0 :                 b.addPlugin (PluginSpec ("resolver"));
      62           0 :                 b.addPlugin (PluginSpec ("dump"));
      63           0 :                 b.useConfigFile (cf);
      64           0 :                 b.validated ();
      65             : 
      66           0 :                 b.serialize (mountConfig);
      67             : 
      68           0 :                 kdb.set (mountConfig, "system/elektra/mountpoints");
      69             :         }
      70             : 
      71             :         {
      72           0 :                 KeySet ks;
      73           0 :                 KDB kdb;
      74           0 :                 kdb.get (ks, mp);
      75           0 :                 for (int i = 0; i < nr_keys; ++i)
      76             :                 {
      77             :                         // clang-format off
      78           0 :                         ks.append (Key ("system/iterate/" + std::to_string (i%10) + "/" + std::to_string (i/10),
      79             :                                         KEY_VALUE, "value",
      80             :                                         KEY_META, "iterate", "value",
      81             :                                         KEY_META, "iterate0", "value",
      82             :                                         KEY_META, "iterate1", "value",
      83             :                                         KEY_META, "iterate2", "value",
      84             :                                         KEY_META, "iterate3", "value",
      85             :                                         KEY_META, "iterate4", "value",
      86             :                                         KEY_META, "iterate5", "value",
      87             :                                         KEY_META, "iterate6", "value",
      88             :                                         KEY_META, "iterate7", "value",
      89             :                                         KEY_META, "iterate8", "value",
      90             :                                         KEY_META, "iterate9", "value",
      91             :                                         KEY_END));
      92             :                         // clang-format on
      93             :                 }
      94           0 :                 kdb.set (ks, mp);
      95             :         }
      96             : 
      97           0 :         KDB kdb;
      98           0 :         t.start ();
      99           0 :         for (int i = 0; i < iterations; ++i)
     100             :         {
     101           0 :                 KeySet ks;
     102           0 :                 kdb.get (ks, mp);
     103           0 :                 kdb.set (ks, mp);
     104             :         }
     105           0 :         t.stop ();
     106             : 
     107           0 :         std::cout << t;
     108           0 : }
     109             : 
     110             : 
     111           0 : void computer_info ()
     112             : {
     113           0 :         std::cout << std::endl;
     114           0 :         std::cout << std::endl;
     115             : #ifndef _WIN32
     116             :         char hostname[1024];
     117           0 :         gethostname (hostname, 1023);
     118           0 :         std::cout << "hostname " << hostname << std::endl;
     119             : #endif
     120             : #ifdef __GNUC__
     121           0 :         std::cout << "gcc: " << __GNUC__ << std::endl;
     122             : #endif
     123             : #ifdef __INTEL_COMPILER
     124             :         std::cout << "icc: " << __INTEL_COMPILER << std::endl;
     125             : #endif
     126             : #ifdef __clang__
     127             :         std::cout << "clang: " << __clang__ << std::endl;
     128             : #endif
     129           0 :         std::cout << "sizeof(int) " << sizeof (int) << std::endl;
     130           0 :         std::cout << "sizeof(long) " << sizeof (long) << std::endl;
     131           0 :         std::cout << "sizeof(long long) " << sizeof (long long) << std::endl;
     132           0 :         std::cout << "iterations " << iterations << std::endl;
     133           0 :         std::cout << std::endl;
     134           0 : }
     135             : 
     136           0 : int main (int argc, char ** argv)
     137             : {
     138           0 :         computer_info ();
     139             : 
     140             : #ifdef HAVE_CLEARENV
     141           0 :         clearenv ();
     142             : #endif
     143             : 
     144           0 :         if (argc == 2)
     145             :         {
     146           0 :                 iterations = atoll (argv[1]);
     147           0 :                 iterations1 = iterations / 100;
     148             :         }
     149             : 
     150           0 :         for (int i = 0; i < benchmarkIterations; ++i)
     151             :         {
     152           0 :                 std::cout << i << std::endl;
     153             : 
     154           0 :                 benchmark_nothing ();
     155           0 :                 benchmark_backend<0> ();
     156           0 :                 benchmark_backend<1> ();
     157           0 :                 benchmark_backend<2> ();
     158           0 :                 benchmark_backend<3> ();
     159           0 :                 benchmark_backend<4> ();
     160           0 :                 benchmark_backend<5> ();
     161           0 :                 benchmark_backend<6> ();
     162           0 :                 benchmark_backend<7> ();
     163           0 :                 benchmark_backend<8> ();
     164           0 :                 benchmark_backend<9> ();
     165             :         }
     166           0 :         std::cerr << "value,benchmark" << std::endl;
     167           0 : }

Generated by: LCOV version 1.13