Guest User

Untitled

a guest
Nov 20th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.13 KB | None | 0 0
  1. // #Instalacja developerskiej wersji biblioteki C (bez plusów)
  2. // sudo apt-get install libmongo-client-dev
  3. //
  4. // #Instalacja nowej wersji cmake
  5. // sudo apt-get install software-properties-common
  6. // sudo add-apt-repository ppa:george-edison55/cmake-3.x
  7. // sudo apt-get update
  8. // sudo apt-get install cmake
  9. //
  10. // #pobranie biblioteki C++
  11. // curl -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.0.3.tar.gz
  12. // tar -xzf r3.0.3.tar.gz
  13. // cd mongo-cxx-driver-r3.0.3/build
  14. //
  15. // #kompilacja i instlacja biblioteki C++
  16. // cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
  17. // sudo make EP_mnmlstc_core
  18. // sudo make install
  19. //
  20. // #Kompilacja i linkowanie
  21. //g++ -c -m64 -pipe -std=c++11 -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I../test01 -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/bsoncxx/v_noabi -I/usr/include/pqxx -I. -o main.o ../test01/main.cpp
  22. //g++ -m64 -Wl,-O1 -o test01 main.o   -L/usr/local/lib -lmongocxx -lbsoncxx -lssl -lcrypto -lrt -lpqxx -lpq -lpthread
  23.  
  24.  
  25.  
  26. #include <cstdlib>
  27. #include <vector>
  28. #include <ctime>
  29. #include <random>
  30. #include <chrono>
  31.  
  32. #include <iostream>
  33.  
  34. #include <bsoncxx/builder/stream/document.hpp>
  35. #include <bsoncxx/json.hpp>
  36.  
  37. #include <mongocxx/client.hpp>
  38. #include <mongocxx/instance.hpp>
  39.  
  40. #include <pqxx/pqxx>
  41.  
  42. #define SIZE1 (1000000)
  43. #define SIZE2 (1000)
  44.  
  45. struct TrioF {
  46.     TrioF( const std::string& name, const std::string& sname, const int age ) :
  47.         name(name), sname(sname), age(age) {
  48.     }
  49.  
  50.     void show() {
  51.         std::cout << "test: " << name << " " << sname << " " << age << std::endl;
  52.     }
  53.  
  54.     const std::string name;
  55.     const std::string sname;
  56.     const int age;
  57. };
  58.  
  59. char rndChar( std::mt19937_64 &mt ) {
  60.     return mt() % ('z' - 'a' + 1) + 'a';
  61. }
  62.  
  63. std::string rndStr( std::mt19937_64 &mt ) {
  64.     std::string s;
  65.     for( int i=0 ; i<10 ; i++ )
  66.         s.push_back( rndChar( mt ) );
  67.     return s;
  68. }
  69.  
  70. void show( std::vector<TrioF> &v ) {
  71.     for( auto i : v ) {
  72.         i.show();
  73.     }
  74. }
  75.  
  76. void pgTest( const unsigned  int seed ) {
  77.     std::cout << "test pg start..." << std::endl;
  78.     std::mt19937_64 mt( seed );
  79.  
  80.     try {
  81.         pqxx::connection C("dbname=testm user=testm password=testm hostaddr=127.0.0.1 port=5432");
  82.         if (C.is_open()) {
  83.           std::cout << "Opened database successfully: " << C.dbname() << std::endl;
  84.         } else {
  85.           std::cout << "Can't open database" << std::endl;
  86.           return;
  87.         }
  88.  
  89.         {
  90.             std::string sql = "CREATE TABLE IF NOT EXISTS testcollection("
  91.                 "id BIGSERIAL PRIMARY KEY     NOT NULL,"
  92.                 "name           TEXT    NOT NULL,"
  93.                 "sname          TEXT    NOT NULL,"
  94.                 "age            INT     NOT NULL"
  95.                 ")";
  96.             pqxx::work W(C);
  97.             W.exec( sql );
  98.             W.commit();
  99.             std::cout << "Table created successfully" << std::endl;
  100.         }
  101.  
  102.         {
  103.             std::string sql = "delete from testcollection";
  104.             pqxx::work W(C);
  105.             W.exec( sql );
  106.             W.commit();
  107.         }
  108.  
  109.  
  110.         std::vector<TrioF> trios;
  111.  
  112.         {
  113.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  114.  
  115.             pqxx::work W(C);
  116.             for( int i=0 ; i<SIZE1 ; i++ ) {
  117.                 const std::string name  = rndStr( mt );
  118.                 const std::string sname = rndStr( mt );
  119.                 const int age   = mt() % 1000000 ;
  120.                 if( i % SIZE2 == 0 ) {
  121.                     trios.push_back( TrioF(name,sname,age) );
  122.                 }
  123.                 std::string sql = "insert into testcollection (name,sname,age) values ('" ;
  124.                 sql += name;
  125.                 sql += "','";
  126.                 sql += sname;
  127.                 sql += "',";
  128.                 sql += std::to_string( age );
  129.                 sql += ")";
  130.                 W.exec( sql );
  131.             }
  132.             W.commit();
  133.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  134.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  135.         }
  136.  
  137.  
  138. //        show(trios);
  139.  
  140.         {
  141.             long long sum_age = 0;
  142.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  143.             pqxx::nontransaction N(C);
  144.  
  145.             for( int i=0 ; i<trios.size() ; i++ ) {
  146.                 std::string sql = "select name, sname, age from testcollection where age = " ;
  147.                 sql += std::to_string( trios[i].age );
  148.                 sql += "";
  149.                 pqxx::result R( N.exec( sql ) );
  150.                 for (pqxx::result::const_iterator c = R.begin(); c != R.end(); ++c) {
  151.                     sum_age += c[2].as<int>();
  152. //                    std::cout  << trios[i].name << " " << c[0].as<std::string>() << " "  << c[1].as<std::string>() << " " << c[2].as<int>() << std::endl;
  153.                 }
  154.             }
  155.             std::cout << "sum_age = " << sum_age << std::endl;
  156.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  157.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  158.         }
  159.  
  160.  
  161.         C.disconnect ();
  162.     } catch (const std::exception &e) {
  163.         std::cerr << e.what() << std::endl;
  164.     }
  165.  
  166. }
  167.  
  168. void mongoTest( const unsigned int seed ) {
  169.     std::cout << "test mong start..." << std::endl;
  170.  
  171.     std::mt19937_64 mt( seed );
  172.  
  173. //    mongocxx::instance inst{};
  174.     mongocxx::client conn{ mongocxx::uri() };
  175.     auto collection = conn["test"]["testcollection"];
  176.     collection.drop();
  177.  
  178.  
  179.     bsoncxx::builder::stream::document document{};
  180.  
  181.     std::vector<TrioF> trios;
  182.  
  183.     {
  184.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  185.  
  186.         for( int i=0 ; i<SIZE1 ; i++ ) {
  187.             const std::string name  = rndStr( mt );
  188.             const std::string sname = rndStr( mt );
  189.             const int age   = mt() % 1000000 ;
  190.  
  191.             if( i % SIZE2 == 0 ) {
  192.                 trios.push_back( TrioF(name,sname,age) );
  193.             }
  194.  
  195.             document.clear();
  196.             document << "name"  << name;
  197.             document << "sname" << sname;
  198.             document << "age"   << age;
  199.  
  200.             collection.insert_one( document.view() );
  201.         }
  202.  
  203.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  204.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  205.     }
  206.  
  207. //    show(trios);
  208.  
  209.  
  210.     {
  211.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  212.  
  213.         long long sum_age = 0;
  214.         for( int i=0 ; i<trios.size() ; i++ ) {
  215.             mongocxx::cursor cursor = collection.find(
  216.                 bsoncxx::builder::stream::document()  <<
  217.                 "age"                                 <<
  218.                 trios[i].age                          <<
  219.                 bsoncxx::builder::stream::finalize
  220.             );
  221.             for( auto doc : cursor ) {
  222. //                sum_age += stoi( doc["age"].get_utf8().value.to_string() );
  223.                 sum_age += doc["age"].get_int32();
  224.     //            std::cout << trios[i].name << " " << doc["name"].get_utf8().value.to_string() << " " << doc["sname"].get_utf8().value.to_string() << " " << doc["age"].get_utf8().value.to_string() << std::endl;
  225.             }
  226.         }
  227.  
  228.         std::cout << "sum_age = " << sum_age << std::endl;
  229.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  230.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  231.  
  232.     }
  233.  
  234. }
  235.  
  236.  
  237. int main(int, char**) {
  238.  
  239. //    srand( 1234 );
  240. //    for( int i=0 ; i<10 ; i++ )
  241. //        std::cout << rand() << " ";
  242. //    std::cout << std::endl;
  243.  
  244. //    srand( 1234 );
  245. //    for( int i=0 ; i<10 ; i++ )
  246. //        std::cout << rand() << " ";
  247. //    std::cout << std::endl;
  248.  
  249.  
  250.     mongoTest( 1234 );
  251.     pgTest( 1234 );
  252.  
  253.     return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment