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

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <kdbthread.hpp>
      10             : #include <kdbtimer.hpp>
      11             : 
      12             : // long long iterations = 100000000000LL; // elitebenchmark lookup
      13             : long long iterations = 100000LL; // elitebenchmark with/activate
      14             : // long long iterations = 100LL; // valgrind
      15             : 
      16             : // not needed in benchmarks:
      17           0 : long long iterations1 = iterations / 100;
      18             : 
      19             : const int benchmarkIterations = 11; // is a good number to not need mean values for median
      20             : 
      21           0 : const std::string filename = "check.txt";
      22             : 
      23           0 : const std::string csvfilename = "data.csv";
      24             : 
      25           0 : std::ofstream dump (filename);
      26           0 : std::ofstream data (csvfilename);
      27             : 
      28             : 
      29             : const uint32_t value = 55;
      30             : const char * s_value = "55";
      31             : const uint32_t othervalue = 66;
      32             : const char * s_othervalue = "66";
      33             : 
      34             : namespace kdb
      35             : {
      36             : 
      37           0 : class Person : public ThreadString
      38             : {
      39             : public:
      40           0 :         Person (KeySet & ks, ThreadContext & context_)
      41           0 :         : ThreadString (ks, context_, Key ("/%layer1%/person", KEY_CASCADING_NAME, KEY_META, "default", "no name", KEY_END))
      42             :         {
      43           0 :         }
      44             :         using ThreadString::operator= ;
      45             : 
      46             : private:
      47             :         Person () = delete;
      48             :         Person (Person const &) = delete;
      49             :         Person (Person &&) = delete;
      50             : };
      51             : 
      52           0 : class Nested : public ThreadInteger
      53             : {
      54             : public:
      55           0 :         Nested (KeySet & ks, ThreadContext & context_)
      56           0 :         : ThreadInteger (ks, context_,
      57           0 :                          Key ("/test/%layer1%/%thread%/%layer2%/nested", KEY_CASCADING_NAME, KEY_META, "default", s_value, KEY_END))
      58             :         {
      59           0 :         }
      60             :         using ThreadInteger::operator= ;
      61             : };
      62             : 
      63           0 : class Environment : public ThreadBoolean
      64             : {
      65             : public:
      66           0 :         Environment (KeySet & ks, ThreadContext & context_)
      67           0 :         : ThreadBoolean (ks, context_, Key ("/test/%layer1%", KEY_CASCADING_NAME, KEY_META, "default", "1", KEY_END)),
      68             :           nested (ks, context_), person (ks, context_),
      69           0 :           profile (ks, context_, Key ("/%layer1%/profile", KEY_CASCADING_NAME, KEY_META, "default", "default", KEY_END)),
      70             :           bm (ks, context_,
      71           0 :               Key ("/%layer1%/%layer2%/%layer3%/%layer4%/%layer5%/%layer6%/%layer7%/%layer8%/%layer9%/", KEY_CASCADING_NAME, KEY_META,
      72           0 :                    "default", s_value, KEY_END))
      73             :         {
      74           0 :         }
      75             : 
      76             :         Nested nested;
      77             :         Person person;
      78             :         ThreadString profile;
      79             :         ThreadInteger bm;
      80             : };
      81             : } // namespace kdb
      82             : 
      83             : 
      84             : // benchmarking
      85             : 
      86           0 : class Layer1 : public kdb::Layer
      87             : {
      88             : public:
      89           0 :         std::string id () const override
      90             :         {
      91           0 :                 return "layer1";
      92             :         }
      93           0 :         std::string operator() () const override
      94             :         {
      95           0 :                 return "1";
      96             :         }
      97             : };
      98           0 : class Layer2 : public kdb::Layer
      99             : {
     100             : public:
     101           0 :         std::string id () const override
     102             :         {
     103           0 :                 return "layer2";
     104             :         }
     105           0 :         std::string operator() () const override
     106             :         {
     107           0 :                 return "2";
     108             :         }
     109             : };
     110           0 : class Layer3 : public kdb::Layer
     111             : {
     112             : public:
     113           0 :         std::string id () const override
     114             :         {
     115           0 :                 return "layer3";
     116             :         }
     117           0 :         std::string operator() () const override
     118             :         {
     119           0 :                 return "3";
     120             :         }
     121             : };
     122           0 : class Layer4 : public kdb::Layer
     123             : {
     124             : public:
     125           0 :         std::string id () const override
     126             :         {
     127           0 :                 return "layer4";
     128             :         }
     129           0 :         std::string operator() () const override
     130             :         {
     131           0 :                 return "4";
     132             :         }
     133             : };
     134           0 : class Layer5 : public kdb::Layer
     135             : {
     136             : public:
     137           0 :         std::string id () const override
     138             :         {
     139           0 :                 return "layer5";
     140             :         }
     141           0 :         std::string operator() () const override
     142             :         {
     143           0 :                 return "5";
     144             :         }
     145             : };
     146           0 : class Layer6 : public kdb::Layer
     147             : {
     148             : public:
     149           0 :         std::string id () const override
     150             :         {
     151           0 :                 return "layer6";
     152             :         }
     153           0 :         std::string operator() () const override
     154             :         {
     155           0 :                 return "6";
     156             :         }
     157             : };
     158           0 : class Layer7 : public kdb::Layer
     159             : {
     160             : public:
     161           0 :         std::string id () const override
     162             :         {
     163           0 :                 return "layer7";
     164             :         }
     165           0 :         std::string operator() () const override
     166             :         {
     167           0 :                 return "7";
     168             :         }
     169             : };
     170           0 : class Layer8 : public kdb::Layer
     171             : {
     172             : public:
     173           0 :         std::string id () const override
     174             :         {
     175           0 :                 return "layer8";
     176             :         }
     177           0 :         std::string operator() () const override
     178             :         {
     179           0 :                 return "8";
     180             :         }
     181             : };
     182           0 : class Layer9 : public kdb::Layer
     183             : {
     184             : public:
     185           0 :         std::string id () const override
     186             :         {
     187           0 :                 return "layer9";
     188             :         }
     189           0 :         std::string operator() () const override
     190             :         {
     191           0 :                 return "9";
     192             :         }
     193             : };
     194             : 
     195           0 : kdb::ThreadInteger::type add_native (uint32_t const & i1, uint32_t const & i2)
     196             : {
     197           0 :         return i1 + i2;
     198             : }
     199             : 
     200           0 : kdb::ThreadInteger::type add_contextual (kdb::ThreadInteger const & i1, kdb::ThreadInteger const & i2)
     201             : {
     202           0 :         return i1 + i2;
     203             : }
     204             : 
     205             : 
     206             : // very fast benchmarks without any if:
     207             : 
     208           0 : __attribute__ ((noinline)) void benchmark_nothing ()
     209             : {
     210           0 :         static Timer t ("nothing");
     211             : 
     212           0 :         t.start ();
     213           0 :         t.stop ();
     214           0 :         std::cout << t;
     215           0 : }
     216             : 
     217           0 : __attribute__ ((noinline)) void benchmark_native ()
     218             : {
     219           0 :         kdb::ThreadInteger::type val = value;
     220           0 :         kdb::ThreadInteger::type x = 0;
     221             : 
     222           0 :         static Timer t ("native cmp noif");
     223           0 :         t.start ();
     224             :         //{benchmark/loop}
     225           0 :         for (long long i = 0; i < iterations; ++i)
     226             :         {
     227           0 :                 x ^= add_native (val, val);
     228             :         }
     229             :         //{end}
     230           0 :         t.stop ();
     231           0 :         std::cout << t;
     232           0 :         dump << t.name << x << std::endl;
     233           0 : }
     234             : 
     235           0 : __attribute__ ((noinline)) void benchmark_native_sum ()
     236             : {
     237           0 :         kdb::ThreadInteger::type val = value;
     238           0 :         kdb::ThreadInteger::type x = 0;
     239             : 
     240           0 :         static Timer t ("native noif sum");
     241           0 :         t.start ();
     242             :         //{benchmark/sumloop}
     243           0 :         for (long long i = 0; i < iterations; ++i)
     244             :         {
     245           0 :                 x += add_native (val, val);
     246             :         }
     247             :         //{end}
     248           0 :         t.stop ();
     249           0 :         std::cout << t;
     250           0 :         dump << t.name << x << std::endl;
     251           0 : }
     252             : 
     253           0 : __attribute__ ((noinline)) void benchmark_native_sum_asm ()
     254             : {
     255           0 :         kdb::ThreadInteger::type val = value;
     256           0 :         kdb::ThreadInteger::type x = 0;
     257             : 
     258           0 :         static Timer t ("native sum asm");
     259           0 :         t.start ();
     260             :         //{benchmark/sumloopasm}
     261           0 :         for (long long i = 0; i < iterations; ++i)
     262             :         {
     263           0 :                 x += add_native (val, val);
     264           0 :                 __asm__("");
     265             :         }
     266             :         //{end}
     267           0 :         t.stop ();
     268           0 :         std::cout << t;
     269           0 :         dump << t.name << x << std::endl;
     270           0 : }
     271             : 
     272           0 : __attribute__ ((noinline)) void benchmark_contextual_noif (kdb::Environment & s)
     273             : {
     274           0 :         s.nested = value;
     275           0 :         kdb::ThreadInteger::type x = 0;
     276             : 
     277           0 :         static Timer t ("context cmp noif");
     278           0 :         t.start ();
     279           0 :         for (long long i = 0; i < iterations; ++i)
     280             :         {
     281           0 :                 x ^= add_contextual (s.nested, s.nested);
     282             :         }
     283           0 :         t.stop ();
     284           0 :         std::cout << t;
     285           0 :         dump << t.name << x << std::endl;
     286           0 : }
     287             : 
     288           0 : __attribute__ ((noinline)) void benchmark_contextual_noif_sum_asm (kdb::Environment & s)
     289             : {
     290           0 :         s.nested = value;
     291           0 :         long long x = 0;
     292             : 
     293           0 :         static Timer t ("context sum asm");
     294           0 :         t.start ();
     295           0 :         for (long long i = 0; i < iterations; ++i)
     296             :         {
     297           0 :                 x += add_contextual (s.nested, s.nested);
     298           0 :                 __asm__("");
     299             :         }
     300           0 :         t.stop ();
     301           0 :         std::cout << t;
     302           0 :         dump << t.name << x << std::endl;
     303           0 : }
     304             : 
     305             : 
     306           0 : __attribute__ ((noinline)) void benchmark_contextual_noif_sum (kdb::Environment & s)
     307             : {
     308           0 :         s.nested = value;
     309           0 :         long long x = 0;
     310             : 
     311           0 :         static Timer t ("context noif sum");
     312           0 :         t.start ();
     313           0 :         for (long long i = 0; i < iterations; ++i)
     314             :         {
     315           0 :                 x += add_contextual (s.nested, s.nested);
     316             :         }
     317           0 :         t.stop ();
     318           0 :         std::cout << t;
     319           0 :         dump << t.name << x << std::endl;
     320           0 : }
     321             : 
     322             : 
     323           0 : __attribute__ ((noinline)) void benchmark_hashmap ()
     324             : {
     325           0 :         std::unordered_map<std::string, kdb::ThreadInteger::type> hashmap;
     326           0 :         hashmap["user/abc/test/hello"] = 5;
     327           0 :         hashmap["user/abc/test/h1"] = 6;
     328           0 :         hashmap["user/abc/test/h2"] = 7;
     329           0 :         hashmap["user/abc/test/test"] = 9;
     330           0 :         hashmap["user/abc/nested/hello"] = 12;
     331           0 :         kdb::ThreadInteger::type x = 0;
     332             : 
     333           0 :         static Timer t ("hashmap");
     334           0 :         t.start ();
     335           0 :         for (long long i = 0; i < iterations; ++i)
     336             :         {
     337           0 :                 x ^= hashmap["user/abc/nested/notav"];
     338             :         }
     339           0 :         t.stop ();
     340           0 :         std::cout << t;
     341           0 :         dump << "hashmap " << x << std::endl;
     342           0 : }
     343             : 
     344           0 : __attribute__ ((noinline)) void benchmark_hashmap_find ()
     345             : {
     346           0 :         std::unordered_map<std::string, kdb::ThreadInteger::type> hashmap;
     347           0 :         hashmap["user/abc/test/hello"] = 5;
     348           0 :         hashmap["user/abc/test/h1"] = 6;
     349           0 :         hashmap["user/abc/test/h2"] = 7;
     350           0 :         hashmap["user/abc/test/test"] = 9;
     351           0 :         hashmap["user/abc/nested/hello"] = 12;
     352           0 :         kdb::ThreadInteger::type x = 0;
     353             : 
     354           0 :         static Timer t ("hashmap find");
     355           0 :         t.start ();
     356           0 :         for (long long i = 0; i < iterations; ++i)
     357             :         {
     358           0 :                 auto it = hashmap.find ("user/abc/nested/hello");
     359           0 :                 if (it != hashmap.end ())
     360             :                 {
     361           0 :                         x ^= it->second;
     362             :                 }
     363             :         }
     364           0 :         t.stop ();
     365             : 
     366           0 :         std::cout << t;
     367           0 :         dump << "hashmap " << x << std::endl;
     368           0 : }
     369             : 
     370           0 : __attribute__ ((noinline)) void benchmark_evaluate_no_sub (kdb::Environment & s)
     371             : {
     372           0 :         static Timer t ("evaluate no sub");
     373           0 :         t.start ();
     374           0 :         for (long long i = 0; i < iterations; ++i)
     375             :         {
     376           0 :                 s.context ().evaluate ("some/string/but/%no%/%sub%/%that%/will/work");
     377             :         }
     378           0 :         t.stop ();
     379           0 :         std::cout << t;
     380           0 : }
     381             : 
     382           0 : __attribute__ ((noinline)) void benchmark_evaluate (kdb::Environment & s)
     383             : {
     384           0 :         static Timer t ("evaluate");
     385           0 :         t.start ();
     386           0 :         for (long long i = 0; i < iterations; ++i)
     387             :         {
     388           0 :                 s.context ().evaluate ("some/%application%/%profile%/%at%/will/work");
     389             :         }
     390           0 :         t.stop ();
     391           0 :         std::cout << t;
     392           0 : }
     393             : 
     394           0 : __attribute__ ((noinline)) void benchmark_evaluate0 (kdb::Environment & s)
     395             : {
     396           0 :         static Timer t ("evaluate0");
     397           0 :         t.start ();
     398           0 :         for (long long i = 0; i < iterations; ++i)
     399             :         {
     400           0 :                 s.context ().evaluate ("nolayer/_________________________________________________________/key");
     401             :         }
     402           0 :         t.stop ();
     403           0 :         std::cout << t;
     404           0 : }
     405             : 
     406           0 : __attribute__ ((noinline)) void benchmark_evaluate1 (kdb::Environment & s)
     407             : {
     408           0 :         static Timer t ("evaluate1");
     409           0 :         t.start ();
     410           0 :         for (long long i = 0; i < iterations; ++i)
     411             :         {
     412           0 :                 s.context ().evaluate ("%layer%/_________________________________________________________/key");
     413             :         }
     414           0 :         t.stop ();
     415           0 :         std::cout << t;
     416           0 : }
     417             : 
     418           0 : __attribute__ ((noinline)) void benchmark_evaluate2 (kdb::Environment & s)
     419             : {
     420           0 :         static Timer t ("evaluate2");
     421           0 :         t.start ();
     422           0 :         for (long long i = 0; i < iterations; ++i)
     423             :         {
     424           0 :                 s.context ().evaluate ("%layer%/%layer2%/________________________________________________/key");
     425             :         }
     426           0 :         t.stop ();
     427           0 :         std::cout << t;
     428           0 : }
     429             : 
     430           0 : __attribute__ ((noinline)) void benchmark_evaluate3 (kdb::Environment & s)
     431             : {
     432           0 :         static Timer t ("evaluate3");
     433           0 :         t.start ();
     434           0 :         for (long long i = 0; i < iterations; ++i)
     435             :         {
     436           0 :                 s.context ().evaluate ("%layer%/%layer2%/%layer3%/_______________________________________/key");
     437             :         }
     438           0 :         t.stop ();
     439           0 :         std::cout << t;
     440           0 : }
     441             : 
     442           0 : __attribute__ ((noinline)) void benchmark_evaluate4 (kdb::Environment & s)
     443             : {
     444           0 :         static Timer t ("evaluate4");
     445           0 :         t.start ();
     446           0 :         for (long long i = 0; i < iterations; ++i)
     447             :         {
     448           0 :                 s.context ().evaluate ("%layer%/%layer2%/%layer3%/%layer4%/______________________________/key");
     449             :         }
     450           0 :         t.stop ();
     451           0 :         std::cout << t;
     452           0 : }
     453             : 
     454           0 : __attribute__ ((noinline)) void benchmark_evaluate5 (kdb::Environment & s)
     455             : {
     456           0 :         static Timer t ("evaluate5");
     457           0 :         t.start ();
     458           0 :         for (long long i = 0; i < iterations; ++i)
     459             :         {
     460           0 :                 s.context ().evaluate ("%layer%/%layer2%/%layer3%/%layer4%/%layer5%/_____________________/key");
     461             :         }
     462           0 :         t.stop ();
     463           0 :         std::cout << t;
     464           0 : }
     465             : 
     466           0 : __attribute__ ((noinline)) void benchmark_kslookup ()
     467             : {
     468           0 :         static Timer t ("kslookup");
     469             :         using namespace kdb; // needed for KS_END
     470           0 :         kdb::KeySet ks (100, *kdb::Key ("user/hello/some", KEY_END), *kdb::Key ("user/hello/a/key", KEY_END),
     471           0 :                         *kdb::Key ("user/hello/b/key", KEY_END), *kdb::Key ("user/hello/c/key", KEY_END),
     472           0 :                         *kdb::Key ("user/hello/d/key", KEY_END), *kdb::Key ("user/other", KEY_END), KS_END);
     473           0 :         t.start ();
     474           0 :         for (long long i = 0; i < iterations; ++i)
     475             :         {
     476           0 :                 ks.lookup ("user/notfound");
     477             :         }
     478           0 :         t.stop ();
     479           0 :         std::cout << t;
     480           0 : }
     481             : 
     482           0 : __attribute__ ((noinline)) void benchmark_layer_with (kdb::Environment & s)
     483             : {
     484           0 :         s.nested = value;
     485           0 :         s.context ().with<Layer1> () ([&] { s.nested = othervalue; });
     486             : 
     487           0 :         static Timer t ("layer with");
     488           0 :         t.start ();
     489           0 :         for (long long i = 0; i < iterations / 2; ++i)
     490             :         {
     491           0 :                 s.context ().with<Layer1> () ([] {});
     492             :         }
     493           0 :         t.stop ();
     494           0 :         std::cout << t;
     495           0 : }
     496             : 
     497           0 : __attribute__ ((noinline)) void benchmark_layer_withx (kdb::Environment & s)
     498             : {
     499           0 :         s.nested = value;
     500           0 :         s.context ().with<Layer1> () ([&] { s.nested = othervalue; });
     501           0 :         kdb::ThreadInteger::type x = 0;
     502             : 
     503           0 :         static Timer t ("layer withx");
     504           0 :         t.start ();
     505           0 :         for (long long i = 0; i < iterations / 2; ++i)
     506             :         {
     507           0 :                 s.context ().with<Layer1> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     508           0 :                 x ^= add_contextual (s.bm, s.bm);
     509             :         }
     510           0 :         t.stop ();
     511           0 :         std::cout << t;
     512           0 : }
     513             : 
     514           0 : __attribute__ ((noinline)) void benchmark_layer_with1valout (kdb::Environment & s)
     515             : {
     516           0 :         s.bm = value;
     517           0 :         s.context ().with<Layer1> () ([&] { s.bm = othervalue; });
     518           0 :         kdb::ThreadInteger::type x = 0;
     519             : 
     520           0 :         static Timer t ("layer with1valout");
     521           0 :         t.start ();
     522           0 :         for (long long i = 0; i < iterations / 2; ++i)
     523             :         {
     524           0 :                 s.context ().activate<Layer1> ();
     525           0 :                 x ^= add_contextual (s.bm, s.bm);
     526           0 :                 s.context ().deactivate<Layer1> ();
     527           0 :                 x ^= add_contextual (s.bm, s.bm);
     528             :         }
     529           0 :         t.stop ();
     530           0 :         std::cout << t;
     531           0 :         dump << t.name << x << std::endl;
     532           0 : }
     533             : 
     534             : 
     535           0 : __attribute__ ((noinline)) void benchmark_layer_with1val (kdb::Environment & s)
     536             : {
     537           0 :         s.bm = value;
     538           0 :         s.context ().with<Layer1> () ([&] { s.bm = othervalue; });
     539           0 :         kdb::ThreadInteger::type x = 0;
     540             : 
     541           0 :         static Timer t ("layer with1val");
     542           0 :         t.start ();
     543           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     544             :         {
     545           0 :                 s.context ().with<Layer1> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     546           0 :                 x ^= add_contextual (s.bm, s.bm);
     547             :         }
     548           0 :         t.stop ();
     549           0 :         std::cout << t;
     550           0 :         dump << "Layer With1Val " << x << std::endl;
     551           0 : }
     552             : 
     553           0 : __attribute__ ((noinline)) void benchmark_layer_with2val (kdb::Environment & s)
     554             : {
     555           0 :         s.bm = value;
     556           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     557           0 :         kdb::ThreadInteger::type x = 0;
     558             : 
     559           0 :         static Timer t ("layer with2val");
     560           0 :         t.start ();
     561           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     562             :         {
     563           0 :                 s.context ().with<Layer1> ().with<Layer2> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     564           0 :                 x ^= add_contextual (s.bm, s.bm);
     565             :         }
     566           0 :         t.stop ();
     567           0 :         std::cout << t;
     568           0 :         dump << "Layer With2Val " << x << std::endl;
     569           0 : }
     570             : 
     571           0 : __attribute__ ((noinline)) void benchmark_layer_with3val (kdb::Environment & s)
     572             : {
     573           0 :         s.bm = value;
     574           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     575           0 :         kdb::ThreadInteger::type x = 0;
     576             : 
     577           0 :         static Timer t ("layer with3val");
     578           0 :         t.start ();
     579           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     580             :         {
     581           0 :                 s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     582           0 :                 x ^= add_contextual (s.bm, s.bm);
     583             :         }
     584           0 :         t.stop ();
     585           0 :         std::cout << t;
     586           0 :         dump << "Layer With3Val " << x << std::endl;
     587           0 : }
     588             : 
     589           0 : __attribute__ ((noinline)) void benchmark_layer_with4val (kdb::Environment & s)
     590             : {
     591           0 :         s.bm = value;
     592           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     593           0 :         kdb::ThreadInteger::type x = 0;
     594             : 
     595           0 :         static Timer t ("layer with4val");
     596           0 :         t.start ();
     597           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     598             :         {
     599           0 :                 s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> ().with<Layer4> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     600           0 :                 x ^= add_contextual (s.bm, s.bm);
     601             :         }
     602           0 :         t.stop ();
     603           0 :         std::cout << t;
     604           0 :         dump << "Layer With4Val " << x << std::endl;
     605           0 : }
     606             : 
     607           0 : __attribute__ ((noinline)) void benchmark_layer_with5val (kdb::Environment & s)
     608             : {
     609           0 :         s.bm = value;
     610           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     611           0 :         kdb::ThreadInteger::type x = 0;
     612             : 
     613           0 :         static Timer t ("layer with5val");
     614           0 :         t.start ();
     615           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     616             :         {
     617           0 :                 s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> ().with<Layer4> ().with<Layer5> () (
     618           0 :                         [&] { x ^= add_contextual (s.bm, s.bm); });
     619           0 :                 x ^= add_contextual (s.bm, s.bm);
     620             :         }
     621           0 :         t.stop ();
     622           0 :         std::cout << t;
     623           0 :         dump << "Layer With5Val " << x << std::endl;
     624           0 : }
     625             : 
     626           0 : __attribute__ ((noinline)) void benchmark_layer_with6val (kdb::Environment & s)
     627             : {
     628           0 :         s.bm = value;
     629           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     630           0 :         kdb::ThreadInteger::type x = 0;
     631             : 
     632           0 :         static Timer t ("layer with6val");
     633           0 :         t.start ();
     634           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     635             :         {
     636           0 :                 s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> ().with<Layer4> ().with<Layer5> ().with<Layer6> () (
     637           0 :                         [&] { x ^= add_contextual (s.bm, s.bm); });
     638           0 :                 x ^= add_contextual (s.bm, s.bm);
     639             :         }
     640           0 :         t.stop ();
     641           0 :         std::cout << t;
     642           0 :         dump << "Layer With6Val " << x << std::endl;
     643           0 : }
     644             : 
     645           0 : __attribute__ ((noinline)) void benchmark_layer_with7val (kdb::Environment & s)
     646             : {
     647           0 :         s.bm = value;
     648           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     649           0 :         kdb::ThreadInteger::type x = 0;
     650             : 
     651           0 :         static Timer t ("layer with7val");
     652           0 :         t.start ();
     653           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     654             :         {
     655           0 :                 s.context ()
     656           0 :                         .with<Layer1> ()
     657           0 :                         .with<Layer2> ()
     658           0 :                         .with<Layer3> ()
     659           0 :                         .with<Layer4> ()
     660           0 :                         .with<Layer5> ()
     661           0 :                         .with<Layer6> ()
     662           0 :                         .with<Layer7> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     663           0 :                 x ^= add_contextual (s.bm, s.bm);
     664             :         }
     665           0 :         t.stop ();
     666           0 :         std::cout << t;
     667           0 :         dump << "Layer With7Val " << x << std::endl;
     668           0 : }
     669             : 
     670           0 : __attribute__ ((noinline)) void benchmark_layer_with8val (kdb::Environment & s)
     671             : {
     672           0 :         s.bm = value;
     673           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     674           0 :         kdb::ThreadInteger::type x = 0;
     675             : 
     676           0 :         static Timer t ("layer with8val");
     677           0 :         t.start ();
     678           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     679             :         {
     680           0 :                 s.context ()
     681           0 :                         .with<Layer1> ()
     682           0 :                         .with<Layer2> ()
     683           0 :                         .with<Layer3> ()
     684           0 :                         .with<Layer4> ()
     685           0 :                         .with<Layer5> ()
     686           0 :                         .with<Layer6> ()
     687           0 :                         .with<Layer7> ()
     688           0 :                         .with<Layer8> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     689           0 :                 x ^= add_contextual (s.bm, s.bm);
     690             :         }
     691           0 :         t.stop ();
     692           0 :         std::cout << t;
     693           0 :         dump << "Layer With8Val " << x << std::endl;
     694           0 : }
     695             : 
     696           0 : __attribute__ ((noinline)) void benchmark_layer_with9val (kdb::Environment & s)
     697             : {
     698           0 :         s.bm = value;
     699           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     700           0 :         kdb::ThreadInteger::type x = 0;
     701             : 
     702           0 :         static Timer t ("layer with9val");
     703           0 :         t.start ();
     704           0 :         for (long long i = 0; i < iterations1 / 2; ++i)
     705             :         {
     706           0 :                 s.context ()
     707           0 :                         .with<Layer1> ()
     708           0 :                         .with<Layer2> ()
     709           0 :                         .with<Layer3> ()
     710           0 :                         .with<Layer4> ()
     711           0 :                         .with<Layer5> ()
     712           0 :                         .with<Layer6> ()
     713           0 :                         .with<Layer7> ()
     714           0 :                         .with<Layer8> ()
     715           0 :                         .with<Layer9> () ([&] { x ^= add_contextual (s.bm, s.bm); });
     716           0 :                 x ^= add_contextual (s.bm, s.bm);
     717             :         }
     718           0 :         t.stop ();
     719           0 :         std::cout << t;
     720           0 :         dump << "Layer With9Val " << x << std::endl;
     721           0 : }
     722             : 
     723             : kdb::ThreadInteger::type g_x = 0;
     724             : 
     725           0 : __attribute__ ((noinline)) void benchmark_layer_withgval9 (kdb::Environment & s)
     726             : {
     727           0 :         s.bm = value;
     728             : 
     729           0 :         g_x = 0; // avoid reference overhead by lambda capture
     730           0 :         static Timer t ("layer withgval9");
     731           0 :         t.start ();
     732           0 :         for (long long i = 0; i < iterations1; ++i)
     733             :         {
     734           0 :                 s.context ()
     735           0 :                         .with<Layer1> ()
     736           0 :                         .with<Layer2> ()
     737           0 :                         .with<Layer3> ()
     738           0 :                         .with<Layer4> ()
     739           0 :                         .with<Layer5> ()
     740           0 :                         .with<Layer6> ()
     741           0 :                         .with<Layer7> ()
     742           0 :                         .with<Layer8> ()
     743           0 :                         .with<Layer9> () ([&] { g_x ^= add_contextual (s.bm, s.bm); });
     744           0 :                 g_x ^= add_contextual (s.bm, s.bm);
     745             :         }
     746           0 :         t.stop ();
     747           0 :         std::cout << t;
     748           0 :         dump << t.name << g_x << std::endl;
     749           0 : }
     750             : 
     751           0 : __attribute__ ((noinline)) void benchmark_layer_withnoalloc9 (kdb::Environment & s)
     752             : {
     753           0 :         s.bm = value;
     754             : 
     755           0 :         kdb::ThreadInteger::type x = 0;
     756           0 :         static Timer t ("layer withnoalloc");
     757           0 :         t.start ();
     758           0 :         std::shared_ptr<kdb::Layer> l1 = std::make_shared<Layer1> ();
     759           0 :         std::shared_ptr<kdb::Layer> l2 = std::make_shared<Layer2> ();
     760           0 :         std::shared_ptr<kdb::Layer> l3 = std::make_shared<Layer3> ();
     761           0 :         std::shared_ptr<kdb::Layer> l4 = std::make_shared<Layer4> ();
     762           0 :         std::shared_ptr<kdb::Layer> l5 = std::make_shared<Layer5> ();
     763           0 :         std::shared_ptr<kdb::Layer> l6 = std::make_shared<Layer6> ();
     764           0 :         std::shared_ptr<kdb::Layer> l7 = std::make_shared<Layer7> ();
     765           0 :         std::shared_ptr<kdb::Layer> l8 = std::make_shared<Layer8> ();
     766           0 :         std::shared_ptr<kdb::Layer> l9 = std::make_shared<Layer9> ();
     767           0 :         for (long long i = 0; i < iterations1; ++i)
     768             :         {
     769           0 :                 s.context ().withl (l1).withl (l2).withl (l3).withl (l4).withl (l5).withl (l6).withl (l7).withl (l8).withl (
     770           0 :                         l9, [&]() { x ^= add_contextual (s.bm, s.bm); });
     771           0 :                 x ^= add_contextual (s.bm, s.bm);
     772             :         }
     773           0 :         t.stop ();
     774           0 :         std::cout << t;
     775           0 :         dump << t.name << g_x << std::endl;
     776           0 : }
     777             : 
     778           0 : __attribute__ ((noinline)) void benchmark_layer_with1act (kdb::Environment & s)
     779             : {
     780           0 :         kdb::ThreadInteger::type x = 0;
     781             : 
     782           0 :         static Timer t ("layer with1act");
     783           0 :         t.start ();
     784           0 :         s.context ().activate<Layer1> ();
     785           0 :         for (long long i = 0; i < iterations; ++i)
     786             :         {
     787           0 :                 x ^= add_contextual (s.bm, s.bm);
     788             :         }
     789           0 :         s.context ().deactivate<Layer1> ();
     790           0 :         t.stop ();
     791           0 :         std::cout << t;
     792           0 :         dump << t.name << x << std::endl;
     793           0 : }
     794             : 
     795           0 : __attribute__ ((noinline)) void benchmark_layer_with1outin (kdb::Environment & s)
     796             : {
     797           0 :         s.bm = value;
     798           0 :         s.context ().with<Layer2> () ([&] { s.bm = othervalue; });
     799             : 
     800           0 :         static Timer t ("layer with1outin");
     801           0 :         t.start ();
     802           0 :         s.context ().with<Layer1> () ([&] {
     803           0 :                 kdb::ThreadInteger::type x = 0;
     804           0 :                 for (long long i = 0; i < iterations; ++i)
     805             :                 {
     806           0 :                         x ^= add_contextual (s.bm, s.bm);
     807             :                 }
     808           0 :                 dump << t.name << x << std::endl;
     809           0 :         });
     810           0 :         t.stop ();
     811           0 :         std::cout << t;
     812           0 : }
     813             : 
     814           0 : __attribute__ ((noinline)) void benchmark_layer_with0out (kdb::Environment & s)
     815             : {
     816           0 :         static Timer t ("with 0 layer");
     817           0 :         s.bm = value;
     818             : 
     819           0 :         kdb::ThreadInteger::type x = 0;
     820           0 :         t.start ();
     821           0 :         for (long long i = 0; i < iterations; ++i)
     822             :         {
     823           0 :                 x ^= add_contextual (s.bm, s.bm);
     824             :         }
     825           0 :         t.stop ();
     826           0 :         dump << t.name << x << std::endl;
     827           0 :         std::cout << t;
     828           0 : }
     829             : 
     830             : 
     831           0 : __attribute__ ((noinline)) void benchmark_layer_with1out (kdb::Environment & s)
     832             : {
     833           0 :         static Timer t ("with 1 layer");
     834           0 :         t.start ();
     835           0 :         s.context ().with<Layer1> () ([&] {
     836           0 :                 kdb::ThreadInteger::type x = 0;
     837           0 :                 s.bm = value;
     838             : 
     839           0 :                 for (long long i = 0; i < iterations; ++i)
     840             :                 {
     841           0 :                         x ^= add_contextual (s.bm, s.bm);
     842             :                 }
     843           0 :                 dump << t.name << x << std::endl;
     844           0 :         });
     845           0 :         t.stop ();
     846           0 :         std::cout << t;
     847           0 : }
     848             : 
     849             : 
     850           0 : __attribute__ ((noinline)) void benchmark_layer_with2out (kdb::Environment & s)
     851             : {
     852           0 :         static Timer t ("with 2 layer");
     853           0 :         t.start ();
     854             :         //{benchmark/layer2}
     855           0 :         s.context ().with<Layer1> ().with<Layer2> () ([&] {
     856           0 :                 s.bm = value;
     857           0 :                 kdb::ThreadInteger::type x = 0;
     858           0 :                 for (long long i = 0; i < iterations; ++i)
     859             :                 {
     860           0 :                         x ^= add_contextual (s.bm, s.bm);
     861             :                 }
     862           0 :                 dump << x << std::endl;
     863           0 :         });
     864             :         //{end}
     865           0 :         t.stop ();
     866           0 :         std::cout << t;
     867           0 : }
     868             : 
     869             : 
     870           0 : __attribute__ ((noinline)) void benchmark_layer_with3out (kdb::Environment & s)
     871             : {
     872           0 :         static Timer t ("with 3 layer");
     873           0 :         t.start ();
     874           0 :         s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> () ([&] {
     875           0 :                 s.bm = value;
     876             : 
     877           0 :                 kdb::ThreadInteger::type x = 0;
     878           0 :                 for (long long i = 0; i < iterations; ++i)
     879             :                 {
     880           0 :                         x ^= add_contextual (s.bm, s.bm);
     881             :                 }
     882           0 :                 dump << t.name << x << std::endl;
     883           0 :         });
     884           0 :         t.stop ();
     885           0 :         std::cout << t;
     886           0 : }
     887             : 
     888             : 
     889           0 : __attribute__ ((noinline)) void benchmark_layer_with4out (kdb::Environment & s)
     890             : {
     891           0 :         static Timer t ("with 4 layer");
     892           0 :         t.start ();
     893           0 :         s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> ().with<Layer4> () ([&] {
     894           0 :                 s.bm = value;
     895             : 
     896           0 :                 kdb::ThreadInteger::type x = 0;
     897           0 :                 for (long long i = 0; i < iterations; ++i)
     898             :                 {
     899           0 :                         x ^= add_contextual (s.bm, s.bm);
     900             :                 }
     901           0 :                 dump << t.name << x << std::endl;
     902           0 :         });
     903           0 :         t.stop ();
     904           0 :         std::cout << t;
     905           0 : }
     906             : 
     907             : 
     908           0 : __attribute__ ((noinline)) void benchmark_layer_with5out (kdb::Environment & s)
     909             : {
     910           0 :         static Timer t ("with 5 layer");
     911           0 :         t.start ();
     912           0 :         s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> ().with<Layer4> ().with<Layer5> () ([&] {
     913           0 :                 s.bm = value;
     914             : 
     915           0 :                 kdb::ThreadInteger::type x = 0;
     916           0 :                 for (long long i = 0; i < iterations; ++i)
     917             :                 {
     918           0 :                         x ^= add_contextual (s.bm, s.bm);
     919             :                 }
     920           0 :                 dump << t.name << x << std::endl;
     921           0 :         });
     922           0 :         t.stop ();
     923           0 :         std::cout << t;
     924           0 : }
     925             : 
     926             : 
     927           0 : __attribute__ ((noinline)) void benchmark_layer_with6out (kdb::Environment & s)
     928             : {
     929           0 :         static Timer t ("with 6 layer");
     930           0 :         t.start ();
     931           0 :         s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> ().with<Layer4> ().with<Layer5> ().with<Layer6> () ([&] {
     932           0 :                 s.bm = value;
     933             : 
     934           0 :                 kdb::ThreadInteger::type x = 0;
     935           0 :                 for (long long i = 0; i < iterations; ++i)
     936             :                 {
     937           0 :                         x ^= add_contextual (s.bm, s.bm);
     938             :                 }
     939           0 :                 dump << t.name << x << std::endl;
     940           0 :         });
     941           0 :         t.stop ();
     942           0 :         std::cout << t;
     943           0 : }
     944             : 
     945             : 
     946           0 : __attribute__ ((noinline)) void benchmark_layer_with7out (kdb::Environment & s)
     947             : {
     948           0 :         static Timer t ("with 7 layer");
     949           0 :         t.start ();
     950           0 :         s.context ().with<Layer1> ().with<Layer2> ().with<Layer3> ().with<Layer4> ().with<Layer5> ().with<Layer6> ().with<Layer7> () ([&] {
     951           0 :                 s.bm = value;
     952             : 
     953           0 :                 kdb::ThreadInteger::type x = 0;
     954           0 :                 for (long long i = 0; i < iterations; ++i)
     955             :                 {
     956           0 :                         x ^= add_contextual (s.bm, s.bm);
     957             :                 }
     958           0 :                 dump << t.name << x << std::endl;
     959           0 :         });
     960           0 :         t.stop ();
     961           0 :         std::cout << t;
     962           0 : }
     963             : 
     964             : 
     965           0 : __attribute__ ((noinline)) void benchmark_layer_with8out (kdb::Environment & s)
     966             : {
     967           0 :         static Timer t ("with 8 layer");
     968           0 :         t.start ();
     969           0 :         s.context ()
     970           0 :                 .with<Layer1> ()
     971           0 :                 .with<Layer2> ()
     972           0 :                 .with<Layer3> ()
     973           0 :                 .with<Layer4> ()
     974           0 :                 .with<Layer5> ()
     975           0 :                 .with<Layer6> ()
     976           0 :                 .with<Layer7> ()
     977           0 :                 .with<Layer8> () ([&] {
     978           0 :                         s.bm = value;
     979             : 
     980           0 :                         kdb::ThreadInteger::type x = 0;
     981           0 :                         for (long long i = 0; i < iterations; ++i)
     982             :                         {
     983           0 :                                 x ^= add_contextual (s.bm, s.bm);
     984             :                         }
     985           0 :                         dump << t.name << x << std::endl;
     986           0 :                 });
     987           0 :         t.stop ();
     988           0 :         std::cout << t;
     989           0 : }
     990             : 
     991             : 
     992           0 : __attribute__ ((noinline)) void benchmark_layer_with9out (kdb::Environment & s)
     993             : {
     994           0 :         static Timer t ("with 9 layer");
     995           0 :         t.start ();
     996           0 :         s.context ()
     997           0 :                 .with<Layer1> ()
     998           0 :                 .with<Layer2> ()
     999           0 :                 .with<Layer3> ()
    1000           0 :                 .with<Layer4> ()
    1001           0 :                 .with<Layer5> ()
    1002           0 :                 .with<Layer6> ()
    1003           0 :                 .with<Layer7> ()
    1004           0 :                 .with<Layer8> ()
    1005           0 :                 .with<Layer9> () ([&] {
    1006           0 :                         s.bm = value;
    1007             : 
    1008           0 :                         kdb::ThreadInteger::type x = 0;
    1009           0 :                         for (long long i = 0; i < iterations; ++i)
    1010             :                         {
    1011           0 :                                 x ^= add_contextual (s.bm, s.bm);
    1012             :                         }
    1013           0 :                         dump << t.name << x << std::endl;
    1014           0 :                 });
    1015           0 :         t.stop ();
    1016           0 :         std::cout << t;
    1017           0 : }
    1018             : 
    1019           0 : __attribute__ ((noinline)) void benchmark_layer_with9outsum (kdb::Environment & s)
    1020             : {
    1021           0 :         static Timer t ("with 9 layer sum");
    1022           0 :         t.start ();
    1023           0 :         s.context ()
    1024           0 :                 .with<Layer1> ()
    1025           0 :                 .with<Layer2> ()
    1026           0 :                 .with<Layer3> ()
    1027           0 :                 .with<Layer4> ()
    1028           0 :                 .with<Layer5> ()
    1029           0 :                 .with<Layer6> ()
    1030           0 :                 .with<Layer7> ()
    1031           0 :                 .with<Layer8> ()
    1032           0 :                 .with<Layer9> () ([&] {
    1033           0 :                         s.bm = value;
    1034             : 
    1035           0 :                         kdb::ThreadInteger::type x = 0;
    1036           0 :                         for (long long i = 0; i < iterations; ++i)
    1037             :                         {
    1038           0 :                                 x += add_contextual (s.bm, s.bm);
    1039             :                         }
    1040           0 :                         dump << t.name << x << std::endl;
    1041           0 :                 });
    1042           0 :         t.stop ();
    1043           0 :         std::cout << t;
    1044           0 : }
    1045             : 
    1046             : 
    1047           0 : __attribute__ ((noinline)) void benchmark_layer_activation (kdb::Environment & s)
    1048             : {
    1049           0 :         s.nested = value;
    1050           0 :         s.context ().with<Layer2> () ([&] { s.nested = othervalue; });
    1051             : 
    1052           0 :         static Timer t ("layer activation");
    1053           0 :         t.start ();
    1054           0 :         for (long long i = 0; i < iterations / 2; ++i)
    1055             :         {
    1056           0 :                 s.context ().activate<Layer2> ();
    1057           0 :                 s.context ().activate<Layer1> ();
    1058             :         }
    1059           0 :         t.stop ();
    1060           0 :         std::cout << t;
    1061           0 : }
    1062             : 
    1063           0 : __attribute__ ((noinline)) void benchmark_layer_activationval (kdb::Environment & s)
    1064             : {
    1065           0 :         s.nested = value;
    1066           0 :         s.context ().with<Layer2> () ([&] { s.nested = othervalue; });
    1067           0 :         kdb::ThreadInteger::type x = 0;
    1068             : 
    1069           0 :         static Timer t ("layer activationval");
    1070           0 :         t.start ();
    1071           0 :         for (long long i = 0; i < iterations / 2; ++i)
    1072             :         {
    1073           0 :                 s.context ().activate<Layer2> ();
    1074           0 :                 x ^= add_contextual (s.nested, s.nested);
    1075           0 :                 s.context ().activate<Layer1> ();
    1076           0 :                 x ^= add_contextual (s.nested, s.nested);
    1077             :         }
    1078           0 :         t.stop ();
    1079           0 :         std::cout << t;
    1080           0 :         dump << "Layer ActivationVal " << x << std::endl;
    1081           0 : }
    1082             : 
    1083           0 : __attribute__ ((noinline)) void benchmark_layer_withN (kdb::Environment &, long long N)
    1084             : {
    1085           0 :         kdb::Coordinator c;
    1086           0 :         kdb::ThreadContext tc (c);
    1087           0 :         kdb::KeySet ks;
    1088           0 :         kdb::ThreadInteger ti (ks, tc, kdb::Key ("/test/nolayer", KEY_CASCADING_NAME, KEY_META, "default", s_value, KEY_END));
    1089           0 :         ti = 5;
    1090           0 :         kdb::ThreadInteger::type x = ti;
    1091             : 
    1092             :         // N additional integers producing overhead
    1093           0 :         std::vector<std::shared_ptr<kdb::ThreadInteger>> vi;
    1094           0 :         for (long long i = 0; i < N; ++i)
    1095             :         {
    1096           0 :                 std::ostringstream os;
    1097           0 :                 os << "/test/%layer1%/";
    1098           0 :                 os << i;
    1099             :                 // std::cout << os.str().c_str() << std::endl;
    1100           0 :                 vi.push_back (std::make_shared<kdb::ThreadInteger> (
    1101           0 :                         ks, tc, kdb::Key (os.str ().c_str (), KEY_CASCADING_NAME, KEY_META, "default", s_value, KEY_END)));
    1102             :         }
    1103             : 
    1104           0 :         static Timer * ts[10]{ new Timer ("layer with0"), new Timer ("layer with1"), new Timer ("layer with2"), new Timer ("layer with3"),
    1105           0 :                                new Timer ("layer with4"), new Timer ("layer with5"), new Timer ("layer with6"), new Timer ("layer with7"),
    1106           0 :                                new Timer ("layer with8"), new Timer ("layer with9") };
    1107           0 :         Timer & t = *ts[N];
    1108           0 :         t.start ();
    1109           0 :         for (long long i = 0; i < iterations / 2; ++i)
    1110             :         {
    1111           0 :                 tc.with<Layer1> () ([&] { x ^= add_contextual (ti, ti); });
    1112           0 :                 x ^= add_contextual (ti, ti);
    1113             :         }
    1114           0 :         t.stop ();
    1115           0 :         std::cout << t;
    1116           0 :         dump << t.name << x << std::endl;
    1117           0 : }
    1118             : 
    1119             : 
    1120           0 : __attribute__ ((noinline)) void benchmark_layer_switch (kdb::Environment & s)
    1121             : {
    1122           0 :         s.nested = value;
    1123           0 :         kdb::ThreadInteger::type x = 0;
    1124             : 
    1125           0 :         static Timer t ("layer switch");
    1126           0 :         t.start ();
    1127             :         //{benchmark/layerswitch}
    1128           0 :         for (long long i = 0; i < iterations / 2; ++i)
    1129             :         {
    1130           0 :                 s.context ().activate<Layer1> ();
    1131           0 :                 x ^= add_contextual (s.nested, s.nested);
    1132           0 :                 s.context ().deactivate<Layer1> ();
    1133           0 :                 x ^= add_contextual (s.nested, s.nested);
    1134             :         }
    1135             :         //{end}
    1136           0 :         t.stop ();
    1137           0 :         std::cout << t;
    1138           0 :         dump << "layer switch " << x << std::endl;
    1139           0 : }
    1140             : 
    1141             : 
    1142           0 : __attribute__ ((noinline)) void benchmark_layer_switchN (kdb::Environment &, long long N)
    1143             : {
    1144           0 :         kdb::Coordinator c;
    1145           0 :         kdb::ThreadContext tc (c);
    1146           0 :         kdb::KeySet ks;
    1147           0 :         kdb::ThreadInteger ti (ks, tc, kdb::Key ("/test/nolayer", KEY_CASCADING_NAME, KEY_META, "default", s_value, KEY_END));
    1148           0 :         ti = 5;
    1149           0 :         kdb::ThreadInteger::type x = ti;
    1150             : 
    1151             :         // N additional integers producing overhead
    1152           0 :         std::vector<std::shared_ptr<kdb::ThreadInteger>> vi;
    1153           0 :         for (long long i = 0; i < N; ++i)
    1154             :         {
    1155           0 :                 std::ostringstream os;
    1156           0 :                 os << "/test/%layer1%/";
    1157           0 :                 os << i;
    1158             :                 // std::cout << os.str().c_str() << std::endl;
    1159           0 :                 vi.push_back (std::make_shared<kdb::ThreadInteger> (
    1160           0 :                         ks, tc, kdb::Key (os.str ().c_str (), KEY_CASCADING_NAME, KEY_META, "default", s_value, KEY_END)));
    1161             :         }
    1162             : 
    1163           0 :         static Timer * ts[10]{ new Timer ("layer switch0"), new Timer ("layer switch1"), new Timer ("layer switch2"),
    1164           0 :                                new Timer ("layer switch3"), new Timer ("layer switch4"), new Timer ("layer switch5"),
    1165           0 :                                new Timer ("layer switch6"), new Timer ("layer switch7"), new Timer ("layer switch8"),
    1166           0 :                                new Timer ("layer switch9") };
    1167           0 :         Timer & t = *ts[N];
    1168           0 :         t.start ();
    1169           0 :         for (long long i = 0; i < iterations / 2; ++i)
    1170             :         {
    1171           0 :                 tc.activate<Layer1> ();
    1172           0 :                 x ^= add_contextual (ti, ti);
    1173           0 :                 tc.deactivate<Layer1> ();
    1174           0 :                 x ^= add_contextual (ti, ti);
    1175             :         }
    1176           0 :         t.stop ();
    1177           0 :         std::cout << t;
    1178           0 :         dump << t.name << x << std::endl;
    1179           0 : }
    1180             : 
    1181             : 
    1182           0 : __attribute__ ((noinline)) void benchmark_layer_sync (kdb::Environment & s)
    1183             : {
    1184           0 :         s.nested = value;
    1185           0 :         kdb::ThreadInteger::type x = 0;
    1186             : 
    1187           0 :         static Timer t ("sync layers");
    1188           0 :         t.start ();
    1189             :         //{benchmark/layerswitch}
    1190           0 :         for (long long i = 0; i < iterations; ++i)
    1191             :         {
    1192           0 :                 s.context ().syncLayers ();
    1193             :         }
    1194             :         //{end}
    1195           0 :         t.stop ();
    1196           0 :         std::cout << t;
    1197           0 :         dump << "layer switch " << x << std::endl;
    1198           0 : }
    1199             : 
    1200             : #include <unistd.h>
    1201             : #ifdef _WIN32
    1202             : #include <winsock2.h>
    1203             : #endif
    1204             : 
    1205           0 : void computer_info ()
    1206             : {
    1207             :         char hostname[1024];
    1208           0 :         gethostname (hostname, 1023);
    1209           0 :         std::cout << std::endl;
    1210           0 :         std::cout << std::endl;
    1211           0 :         std::cout << "hostname " << hostname << std::endl;
    1212             : #ifdef __GNUC__
    1213           0 :         std::cout << "gcc: " << __GNUC__ << std::endl;
    1214             : #endif
    1215             : #ifdef __INTEL_COMPILER
    1216             :         std::cout << "icc: " << __INTEL_COMPILER << std::endl;
    1217             : #endif
    1218             : #ifdef __clang__
    1219             :         std::cout << "clang: " << __clang__ << std::endl;
    1220             : #endif
    1221           0 :         std::cout << "sizeof(int) " << sizeof (int) << std::endl;
    1222           0 :         std::cout << "sizeof(long) " << sizeof (long) << std::endl;
    1223           0 :         std::cout << "sizeof(long long) " << sizeof (long long) << std::endl;
    1224           0 :         std::cout << "sizeof(Integer::type) " << sizeof (kdb::ThreadInteger::type) << std::endl;
    1225           0 :         std::cout << "value " << value << std::endl;
    1226           0 :         std::cout << "othervalue " << othervalue << std::endl;
    1227           0 :         std::cout << "iterations " << iterations << std::endl;
    1228           0 :         std::cout << "filename " << filename << std::endl;
    1229           0 :         std::cout << std::endl;
    1230           0 : }
    1231             : 
    1232           0 : int main (int argc, char ** argv)
    1233             : {
    1234           0 :         kdb::Coordinator gc;
    1235           0 :         kdb::ThreadContext c (gc);
    1236           0 :         kdb::KeySet ks;
    1237           0 :         kdb::Environment s (ks, c);
    1238           0 :         computer_info ();
    1239           0 :         s.nested = 20;
    1240           0 :         s.bm = 20;
    1241           0 :         std::cout << std::endl;
    1242             : 
    1243           0 :         if (argc == 2)
    1244             :         {
    1245           0 :                 iterations = atoll (argv[1]);
    1246           0 :                 iterations1 = iterations / 100;
    1247             :         }
    1248             : 
    1249           0 :         for (int i = 0; i < benchmarkIterations; ++i)
    1250             :         {
    1251           0 :                 std::cout << i << std::endl;
    1252             : 
    1253             :                 // if:
    1254             :                 /*
    1255             :                 benchmark_nothing();
    1256             :                 benchmark_native();
    1257             :                 benchmark_native_sum();
    1258             :                 benchmark_native_sum_asm();
    1259             :                 benchmark_contextual_noif(s);
    1260             :                 benchmark_contextual_noif_sum(s);
    1261             :                 benchmark_contextual_noif_sum_asm(s); // same as xor
    1262             : 
    1263             : 
    1264             :                 // 1-9 layer activations outside a loop
    1265             :                 benchmark_layer_with0out(s);
    1266             :                 benchmark_layer_with1out(s);
    1267             :                 benchmark_layer_with2out(s);
    1268             :                 benchmark_layer_with3out(s);
    1269             :                 benchmark_layer_with4out(s);
    1270             :                 benchmark_layer_with5out(s);
    1271             :                 benchmark_layer_with6out(s);
    1272             :                 benchmark_layer_with7out(s);
    1273             :                 benchmark_layer_with8out(s);
    1274             :                 benchmark_layer_with9out(s);
    1275             :                 */
    1276             : 
    1277             :                 // 1-9 values with layer activations in a loop
    1278             :                 /*
    1279             :                 benchmark_layer_with1val(s);
    1280             :                 benchmark_layer_with2val(s);
    1281             :                 benchmark_layer_with3val(s);
    1282             :                 benchmark_layer_with4val(s);
    1283             :                 benchmark_layer_with5val(s);
    1284             :                 benchmark_layer_with6val(s);
    1285             :                 benchmark_layer_with7val(s);
    1286             :                 benchmark_layer_with8val(s);
    1287             :                 benchmark_layer_with9val(s);
    1288             : 
    1289             :                 benchmark_layer_with1act(s);
    1290             :                 benchmark_layer_with2act(s);
    1291             : 
    1292             :                 // obsolete benchmarks not needed for paper:
    1293             :                 benchmark_layer_withnoalloc9(s); // not faster
    1294             :                 benchmark_layer_with9outsum(s); // 30 us
    1295             :                 benchmark_contextual_wrongop(s);
    1296             :                 benchmark_layer_activation(s);
    1297             :                 benchmark_layer_activationval(s);
    1298             : 
    1299             :                 benchmark_layer_with1outin(s);
    1300             :                 benchmark_layer_with1valout(s);
    1301             :                 */
    1302           0 :                 benchmark_evaluate_no_sub (s);
    1303             : 
    1304           0 :                 benchmark_layer_sync (s);
    1305           0 :                 benchmark_layer_with (s);
    1306           0 :                 benchmark_layer_withx (s);
    1307           0 :                 for (int j = 0; j < 10; ++j)
    1308             :                 {
    1309           0 :                         benchmark_layer_withN (s, j);
    1310             :                 }
    1311           0 :                 benchmark_layer_switch (s);
    1312           0 :                 for (int j = 0; j < 10; ++j)
    1313             :                 {
    1314           0 :                         benchmark_layer_switchN (s, j);
    1315             :                 }
    1316             : 
    1317           0 :                 benchmark_evaluate (s);
    1318           0 :                 benchmark_evaluate0 (s);
    1319           0 :                 benchmark_evaluate1 (s);
    1320           0 :                 benchmark_evaluate2 (s);
    1321           0 :                 benchmark_evaluate3 (s);
    1322           0 :                 benchmark_evaluate4 (s);
    1323           0 :                 benchmark_evaluate5 (s);
    1324             : 
    1325           0 :                 benchmark_kslookup ();
    1326             : 
    1327           0 :                 benchmark_hashmap ();
    1328           0 :                 benchmark_hashmap_find ();
    1329             :         }
    1330             : 
    1331           0 :         data << "value,benchmark" << std::endl;
    1332           0 : }

Generated by: LCOV version 1.13