Guest User

Untitled

a guest
Nov 20th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.23 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 (3000000)
  43. #define SIZE2 (50)
  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<3 ; 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 = "DROP TABLE IF EXISTS testcollection";
  91.             pqxx::work W(C);
  92.             W.exec( sql );
  93.             W.commit();
  94.         }
  95.  
  96.         {
  97.             std::string sql = "CREATE TABLE testcollection("
  98.                 "id BIGSERIAL PRIMARY KEY     NOT NULL,"
  99.                 "name           TEXT    NOT NULL,"
  100.                 "sname          TEXT    NOT NULL,"
  101.                 "age            INT     NOT NULL"
  102.                 ")";
  103.             pqxx::work W(C);
  104.             W.exec( sql );
  105.             W.commit();
  106.             std::cout << "Table created successfully" << std::endl;
  107.         }
  108.  
  109.  
  110.         {
  111.             std::string sql = "delete from testcollection";
  112.             pqxx::work W(C);
  113.             W.exec( sql );
  114.             W.commit();
  115.         }
  116.  
  117.  
  118.         std::vector<TrioF> trios;
  119.  
  120.         {
  121.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  122.  
  123.             pqxx::work W(C);
  124.             for( int i=0 ; i<SIZE1 ; i++ ) {
  125.                 const std::string name  = rndStr( mt );
  126.                 const std::string sname = rndStr( mt );
  127.                 const int age   = mt() % 1000000 ;
  128.                 if( i % SIZE2 == 0 ) {
  129.                     trios.push_back( TrioF(name,sname,age) );
  130.                 }
  131.                 std::string sql = "insert into testcollection (name,sname,age) values ('" ;
  132.                 sql += name;
  133.                 sql += "','";
  134.                 sql += sname;
  135.                 sql += "',";
  136.                 sql += std::to_string( age );
  137.                 sql += ")";
  138.                 W.exec( sql );
  139.             }
  140.             W.commit();
  141.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  142.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  143.         }
  144.  
  145.  
  146.         {
  147.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  148.  
  149.             std::string sql = "CREATE INDEX idx_name ON testcollection (name,sname)";
  150.             pqxx::work W(C);
  151.             W.exec( sql );
  152.             W.commit();
  153.  
  154.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  155.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  156.         }
  157.  
  158. //        show(trios);
  159.  
  160.         {
  161.             long long sum_age = 0;
  162.             std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  163.             pqxx::nontransaction N(C);
  164.  
  165.             for( int i=0 ; i<trios.size() ; i++ ) {
  166.                 std::string sql = "select name, sname, age from testcollection where name = '" ;
  167.                 sql += trios[i].name;
  168.                 sql += "'";
  169.                 pqxx::result R( N.exec( sql ) );
  170.                 for (pqxx::result::const_iterator c = R.begin(); c != R.end(); ++c) {
  171.                     sum_age += c[2].as<int>();
  172. //                    std::cout  << trios[i].name << " " << c[0].as<std::string>() << " "  << c[1].as<std::string>() << " " << c[2].as<int>() << std::endl;
  173.                 }
  174.             }
  175.             std::cout << "sum_age = " << sum_age << std::endl;
  176.             std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  177.             std::cout << elapsed_seconds.count() << "s" << std::endl;
  178.         }
  179.  
  180.  
  181.         C.disconnect ();
  182.     } catch (const std::exception &e) {
  183.         std::cerr << e.what() << std::endl;
  184.     }
  185.  
  186. }
  187.  
  188. void mongoTest( const unsigned int seed ) {
  189.     std::cout << "test mong start..." << std::endl;
  190.  
  191.     std::mt19937_64 mt( seed );
  192.  
  193. //    mongocxx::instance inst{};
  194.     mongocxx::client conn{ mongocxx::uri() };
  195.     auto collection = conn["test"]["testcollection"];
  196.     collection.drop();
  197.  
  198.  
  199.     bsoncxx::builder::stream::document document{};
  200.  
  201.     std::vector<TrioF> trios;
  202.  
  203.     {
  204.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  205.  
  206.         for( int i=0 ; i<SIZE1 ; i++ ) {
  207.             const std::string name  = rndStr( mt );
  208.             const std::string sname = rndStr( mt );
  209.             const int age   = mt() % 1000000 ;
  210.  
  211.             if( i % SIZE2 == 0 ) {
  212.                 trios.push_back( TrioF(name,sname,age) );
  213.             }
  214.  
  215.             document.clear();
  216.             document << "name"  << name;
  217.             document << "sname" << sname;
  218.             document << "age"   << age;
  219.  
  220.             collection.insert_one( document.view() );
  221.         }
  222.  
  223.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  224.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  225.     }
  226.  
  227. //    show(trios);
  228.  
  229.     {
  230.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  231.  
  232.         auto index_specification = bsoncxx::builder::stream::document() << "name" << 1 << "sname" << 1 << bsoncxx::builder::stream::finalize;
  233.         collection.create_index(std::move(index_specification));
  234.  
  235.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  236.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  237.     }
  238.  
  239.     {
  240.         std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
  241.  
  242.         long long sum_age = 0;
  243.         for( int i=0 ; i<trios.size() ; i++ ) {
  244.             mongocxx::cursor cursor = collection.find(
  245.                 bsoncxx::builder::stream::document()   <<
  246.                 "name"                                 <<
  247.                 trios[i].name                          <<
  248.                 bsoncxx::builder::stream::finalize
  249.             );
  250.             for( auto doc : cursor ) {
  251. //                sum_age += stoi( doc["age"].get_utf8().value.to_string() );
  252.                 sum_age += doc["age"].get_int32();
  253.     //            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;
  254.             }
  255.         }
  256.  
  257.         std::cout << "sum_age = " << sum_age << std::endl;
  258.         std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
  259.         std::cout << elapsed_seconds.count() << "s" << std::endl;
  260.  
  261.     }
  262.  
  263. }
  264.  
  265.  
  266. int main(int, char**) {
  267.  
  268. //    srand( 1234 );
  269. //    for( int i=0 ; i<10 ; i++ )
  270. //        std::cout << rand() << " ";
  271. //    std::cout << std::endl;
  272.  
  273. //    srand( 1234 );
  274. //    for( int i=0 ; i<10 ; i++ )
  275. //        std::cout << rand() << " ";
  276. //    std::cout << std::endl;
  277.  
  278.  
  279.     mongoTest( 1234 );
  280.     pgTest( 1234 );
  281.  
  282.     return 0;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment