Advertisement
Guest User

Untitled

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