?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2020, Dmitry Tarakanov
  3. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  4. // Copyright 2007, Nathan Reed
  5. // SPDX-License-Identifier: MIT
  6.  
  7. #include "TestBase.h"
  8.  
  9. #include <cstdio>
  10. #include <ctime>
  11. #include <iomanip>
  12. #include <iostream>
  13. #include <string>
  14. #include <vector>
  15.  
  16. using namespace std;
  17. using namespace luabridge;
  18.  
  19. //------------------------------------------------------------------------------
  20. /**
  21.   Simple stopwatch for measuring elapsed time.
  22. */
  23. class Stopwatch
  24. {
  25. private:
  26.     clock_t m_start;
  27.  
  28. public:
  29.     Stopwatch() { start(); }
  30.  
  31.     void start() { m_start = clock(); }
  32.  
  33.     double getElapsedSeconds()
  34.     {
  35.         clock_t now;
  36.  
  37.         now = clock();
  38.  
  39.         return (double(now - m_start)) / CLOCKS_PER_SEC;
  40.     }
  41. };
  42.  
  43. //------------------------------------------------------------------------------
  44. /**
  45.   Classes used for performance tests.
  46. */
  47.  
  48. struct A
  49. {
  50.     A() : data(0), prop(0) {}
  51.  
  52.     void mf1() {}
  53.  
  54.     void mf2(A*) {}
  55.  
  56.     void mf3(A&) {}
  57.  
  58.     virtual void vf1() {}
  59.  
  60.     int data;
  61.  
  62.     int prop;
  63.     int getprop() const { return prop; }
  64.     void setprop(int v) { prop = v; }
  65. };
  66.  
  67. //------------------------------------------------------------------------------
  68.  
  69. void addToState(lua_State* L)
  70. {
  71.     getGlobalNamespace(L)
  72.         .beginClass<A>("A")
  73.         .addConstructor<void (*)(void)>()
  74.         .addFunction("mf1", &A::mf1)
  75.         .addFunction("mf2", &A::mf2)
  76.         .addFunction("mf3", &A::mf3)
  77.         .addFunction("vf1", &A::vf1)
  78.         .addData("data", &A::data)
  79.         .addProperty("prop", &A::getprop, &A::setprop)
  80.         .endClass();
  81. }
  82.  
  83. void runTests(lua_State* L)
  84. {
  85.     cout.precision(4);
  86.  
  87.     int result;
  88.  
  89.     luaL_dostring(L, "a = A()");
  90.  
  91.     int const trials = 5;
  92.  
  93.     for (int trial = 0; trial < trials; ++trial)
  94.     {
  95.         result = luaL_loadstring(L, "a:mf1 ()");
  96.         if (result != 0)
  97.             lua_error(L);
  98.  
  99.         int const N = 10000000;
  100.  
  101.         Stopwatch sw;
  102.  
  103.         sw.start();
  104.         for (int i = 0; i < N; ++i)
  105.         {
  106.             lua_pushvalue(L, -1);
  107.             lua_call(L, 0, 0);
  108.         }
  109.  
  110.         double const seconds = sw.getElapsedSeconds();
  111.  
  112.         cout << "Elapsed time: " << seconds << endl;
  113.     }
  114. }
  115.  
  116. void runPerformanceTests()
  117. {
  118.     lua_State* L = luaL_newstate();
  119.     luaL_openlibs(L);
  120.  
  121.     addToState(L);
  122.     runTests(L);
  123.  
  124.     lua_close(L);
  125. }
  126.  
  127. struct PerformanceTests : TestBase
  128. {
  129. };
  130.  
  131. TEST_F(PerformanceTests, AllTests)
  132. {
  133.     addToState(L);
  134.     runTests(L);
  135. }
  136.