Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.83 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. #include <new>
  32.  
  33. #include <iostream>
  34.  
  35. #include <bsoncxx/builder/stream/document.hpp>
  36. #include <bsoncxx/json.hpp>
  37.  
  38. #include <mongocxx/client.hpp>
  39. #include <mongocxx/instance.hpp>
  40.  
  41. #include <pqxx/pqxx>
  42.  
  43. #define SIZE1 (3000000)
  44. #define SIZE2 (50)
  45.  
  46. struct TrioF {
  47.     TrioF( const std::string& name, const std::string& sname, const int age ) :
  48.         name(name), sname(sname), age(age) {
  49.     }
  50.  
  51.     void show() {
  52.         std::cout << "test: " << name << " " << sname << " " << age << std::endl;
  53.     }
  54.  
  55.     void operator = (const TrioF& other) {
  56.         new(this)TrioF(other.name,other.sname,other.age);
  57.     }
  58.  
  59.     const std::string name;
  60.     const std::string sname;
  61.     const int age;
  62. };
  63.  
  64. char rndChar( std::mt19937_64 &mt ) {
  65.     return mt() % ('c' - 'a' + 1) + 'a';
  66. }
  67.  
  68. std::string rndStr( std::mt19937_64 &mt ) {
  69.     std::string s;
  70.     for( int i=0 ; i<8 ; i++ )
  71.         s.push_back( rndChar( mt ) );
  72.     return s;
  73. }
  74.  
  75. void show( std::vector<TrioF> &v ) {
  76.     for( auto i : v ) {
  77.         i.show();
  78.     }
  79. }
  80.  
  81. template <typename T>
  82. void myShuffle( std::vector<T> &t , std::mt19937_64 &mt ) {
  83.     for( int i=0 ; i<t.size()-1 ; i++ )
  84.         std::swap( t[i] , t[ mt() % (t.size()-i) + i ] );
  85. }
  86.  
  87. void pgTest( const unsigned  int seed ) {
  88.     std::cout << "test pg start..." << std::endl;
  89.     std::mt19937_64 mt( seed );
  90.  
  91.     try {
  92.         pqxx::connection C("dbname=testm user=testm password=testm hostaddr=127.0.0.1 port=5432");
  93.         if (C.is_open()) {
  94.           std::cout << "Opened database successfully: " << C.dbname() << std::endl;
  95.         } else {
  96.           std::cout << "Can't open database" << std::endl;
  97.           return;
  98.         }
  99.  
  100.         {
  101.             std::string sql = "DROP TABLE IF EXISTS testcollection";
  102.             pqxx::work W(C);
  103.             W.exec( sql );
  104.             W.commit();
  105.         }
  106.  
  107.         {
  108.             std::string sql = "CREATE TABLE testcollection("
  109.                 "id BIGSERIAL PRIMARY KEY NOT NULL,"
  110.                 "name           TEXT    NOT NULL,"
  111.                 "sname          TEXT    NOT NULL,"
  112.                 "age            INT     NOT NULL"
  113.                 ")";
  114.             pqxx::work W(C);
  115.             W.exec( sql );
  116.             W.commit();
  117.             std::cout << "Table created successfully" << std::endl;
  118.         }
  119.  
  120.  
  121.         {
  122.             std::string sql = "delete from testcollection";
  123.             pqxx::work W(C);
  124.             W.exec( sql );
  125.             W.commit();
  126.         }
  127.  
  128.         {
  129.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  130.  
  131.             std::string sql = "CREATE INDEX idx_name ON testcollection (name)";
  132.             pqxx::work W(C);
  133.             W.exec( sql );
  134.             W.commit();
  135.  
  136.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  137.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  138.         }
  139.  
  140.         std::vector<TrioF> trios;
  141.  
  142.         {
  143.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  144.  
  145.             pqxx::work W(C);
  146.             for( int i=0 ; i<SIZE1 ; i++ ) {
  147.                 const std::string name  = rndStr( mt );
  148.                 const std::string sname = rndStr( mt );
  149.                 const int age   = mt() % 1000000 ;
  150.                 if( i % SIZE2 == 0 ) {
  151.                     trios.push_back( TrioF(name,sname,age) );
  152.                 }
  153.                 std::string sql = "insert into testcollection (name,sname,age) values ('" ;
  154.                 sql += name;
  155.                 sql += "','";
  156.                 sql += sname;
  157.                 sql += "',";
  158.                 sql += std::to_string( age );
  159.                 sql += ")";
  160.                 W.exec( sql );
  161.             }
  162.             W.commit();
  163.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  164.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  165.         }
  166.  
  167.         //std::random_shuffle( trios.begin() , trios.end() , mt );
  168.         myShuffle<TrioF>( trios , mt );
  169.  
  170.  
  171. //        show(trios);
  172.  
  173.         {
  174.             long long sum_age = 0;
  175.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  176.             pqxx::nontransaction N(C);
  177.  
  178.             for( int i=0 ; i<trios.size() ; i++ ) {
  179.                 std::string sql = "select name, sname, age from testcollection where name = '" ;
  180.                 sql += trios[i].name;
  181.                 sql += "' AND sname='";
  182.                 sql += trios[i].sname;
  183.                 sql += "'";
  184.                 pqxx::result R( N.exec( sql ) );
  185.                 for (pqxx::result::const_iterator c = R.begin(); c != R.end(); ++c) {
  186.                     sum_age += c[2].as<int>();
  187. //                    std::cout  << trios[i].name << " " << c[0].as<std::string>() << " "  << c[1].as<std::string>() << " " << c[2].as<int>() << std::endl;
  188.                 }
  189.             }
  190.             std::cout << "sum_age = " << sum_age << std::endl;
  191.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  192.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  193.         }
  194.  
  195.  
  196.         C.disconnect ();
  197.     } catch (const std::exception &e) {
  198.         std::cerr << e.what() << std::endl;
  199.     }
  200.  
  201. }
  202.  
  203. void mongoTest( const unsigned int seed ) {
  204.     std::cout << "test mong start..." << std::endl;
  205.  
  206.     std::mt19937_64 mt( seed );
  207.  
  208. //    mongocxx::instance inst{};
  209.     mongocxx::client conn{ mongocxx::uri() };
  210.     auto collection = conn["test"]["testcollection"];
  211.     collection.drop();
  212.  
  213.  
  214.     bsoncxx::builder::stream::document document{};
  215.  
  216.     std::vector<TrioF> trios;
  217.  
  218.     {
  219.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  220.  
  221.         auto index_specification = bsoncxx::builder::stream::document() << "name" << 1 << bsoncxx::builder::stream::finalize;
  222.         collection.create_index(std::move(index_specification));
  223.  
  224.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  225.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  226.     }
  227.  
  228.     {
  229.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  230.  
  231.         for( int i=0 ; i<SIZE1 ; i++ ) {
  232.             const std::string name  = rndStr( mt );
  233.             const std::string sname = rndStr( mt );
  234.             const int age   = mt() % 1000000 ;
  235.  
  236.             if( i % SIZE2 == 0 ) {
  237.                 trios.push_back( TrioF(name,sname,age) );
  238.             }
  239.  
  240.             document.clear();
  241.             document << "name"  << name;
  242.             document << "sname" << sname;
  243.             document << "age"   << age;
  244.  
  245.             collection.insert_one( document.view() );
  246.         }
  247.  
  248.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  249.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  250.     }
  251.  
  252.     myShuffle<TrioF>( trios , mt );
  253.  
  254.  
  255.     {
  256.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  257.  
  258.         long long sum_age = 0;
  259.         for( int i=0 ; i<trios.size() ; i++ ) {
  260.             mongocxx::cursor cursor = collection.find(
  261.                 bsoncxx::builder::stream::document()   <<
  262.                 "name"                                 <<
  263.                 trios[i].name                          <<
  264.                 "sname"                                <<
  265.                 trios[i].sname                         <<
  266.                 bsoncxx::builder::stream::finalize
  267.             );
  268.             for( auto doc : cursor ) {
  269. //                sum_age += stoi( doc["age"].get_utf8().value.to_string() );
  270.                 sum_age += doc["age"].get_int32()  ;
  271.     //            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;
  272.             }
  273.         }
  274.  
  275.         std::cout << "sum_age = " << sum_age << std::endl;
  276.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  277.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  278.  
  279.     }
  280.  
  281. }
  282.  
  283.  
  284. int main(int, char**) {
  285.  
  286. //    srand( 1234 );
  287. //    for( int i=0 ; i<10 ; i++ )
  288. //        std::cout << rand() << " ";
  289. //    std::cout << std::endl;
  290.  
  291. //    srand( 1234 );
  292. //    for( int i=0 ; i<10 ; i++ )
  293. //        std::cout << rand() << " ";
  294. //    std::cout << std::endl;
  295.  
  296.  
  297.     mongoTest( 1234 );
  298.     pgTest( 1234 );
  299.  
  300.     return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement