Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #Instalacja developerskiej wersji biblioteki C (bez plusów)
- // sudo apt-get install libmongo-client-dev
- //
- // #Instalacja nowej wersji cmake
- // sudo apt-get install software-properties-common
- // sudo add-apt-repository ppa:george-edison55/cmake-3.x
- // sudo apt-get update
- // sudo apt-get install cmake
- //
- // #pobranie biblioteki C++
- // curl -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.0.3.tar.gz
- // tar -xzf r3.0.3.tar.gz
- // cd mongo-cxx-driver-r3.0.3/build
- //
- // #kompilacja i instlacja biblioteki C++
- // cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
- // sudo make EP_mnmlstc_core
- // sudo make install
- //
- // #Kompilacja i linkowanie
- //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
- //g++ -m64 -Wl,-O1 -o test01 main.o -L/usr/local/lib -lmongocxx -lbsoncxx -lssl -lcrypto -lrt -lpqxx -lpq -lpthread
- #include <cstdlib>
- #include <vector>
- #include <ctime>
- #include <random>
- #include <chrono>
- #include <new>
- #include <iostream>
- #include <bsoncxx/builder/stream/document.hpp>
- #include <bsoncxx/json.hpp>
- #include <mongocxx/client.hpp>
- #include <mongocxx/instance.hpp>
- #include <pqxx/pqxx>
- #define SIZE1 (500000)
- #define SIZE2 (200)
- struct TrioF {
- TrioF( const std::string& name, const std::string& sname, const int age ) :
- name(name), sname(sname), age(age) {
- }
- void show() {
- std::cout << "test: " << name << " " << sname << " " << age << std::endl;
- }
- void operator = (const TrioF& other) {
- new(this)TrioF(other.name,other.sname,other.age);
- }
- const std::string name;
- const std::string sname;
- const int age;
- };
- char rndChar( std::mt19937_64 &mt ) {
- static const char c[] = "1234567890qwertyuiopasdfghjklzxcvbnm";
- return c[ mt() % (sizeof(c)-1) ];
- }
- std::string rndStr( std::mt19937_64 &mt ) {
- std::string s;
- for( int i=0 ; i<20 ; i++ )
- s.push_back( rndChar( mt ) );
- return s;
- }
- void show( std::vector<TrioF> &v ) {
- for( auto i : v ) {
- i.show();
- }
- }
- template <typename T>
- void myShuffle( std::vector<T> &t , std::mt19937_64 &mt ) {
- for( int i=0 ; i<t.size()-1 ; i++ )
- std::swap( t[i] , t[ mt() % (t.size()-i) + i ] );
- }
- void pgTest( const unsigned int seed ) {
- std::cout << "test pg start..." << std::endl;
- std::mt19937_64 mt( seed );
- try {
- pqxx::connection C("dbname=testm user=testm password=testm hostaddr=127.0.0.1 port=5432");
- if (C.is_open()) {
- std::cout << "Opened database successfully: " << C.dbname() << std::endl;
- } else {
- std::cout << "Can't open database" << std::endl;
- return;
- }
- {
- std::string sql = "DROP TABLE IF EXISTS testcollection";
- pqxx::work W(C);
- W.exec( sql );
- W.commit();
- }
- {
- std::string sql = "CREATE TABLE testcollection("
- "id BIGSERIAL PRIMARY KEY NOT NULL,"
- "name TEXT NOT NULL,"
- "sname TEXT NOT NULL,"
- "age INT NOT NULL"
- ")";
- pqxx::work W(C);
- W.exec( sql );
- W.commit();
- std::cout << "Table created successfully" << std::endl;
- }
- {
- std::string sql = "delete from testcollection";
- pqxx::work W(C);
- W.exec( sql );
- W.commit();
- }
- {
- std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
- std::string sql = "CREATE INDEX idx_name ON testcollection (name)";
- pqxx::work W(C);
- W.exec( sql );
- W.commit();
- std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
- std::cout << elapsed_seconds.count() << "s" << std::endl;
- }
- std::vector<TrioF> trios;
- {
- std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
- pqxx::work W(C);
- for( int i=0 ; i<SIZE1 ; i++ ) {
- const std::string name = rndStr( mt );
- const std::string sname = rndStr( mt );
- const int age = mt() % 1000000 ;
- if( i % SIZE2 == 0 ) {
- trios.push_back( TrioF(name,sname,age) );
- }
- std::string sql = "insert into testcollection (name,sname,age) values ('" ;
- sql += name;
- sql += "','";
- sql += sname;
- sql += "',";
- sql += std::to_string( age );
- sql += ")";
- // std::cout << sql << std::endl;
- W.exec( sql );
- }
- W.commit();
- std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
- std::cout << elapsed_seconds.count() << "s" << std::endl;
- }
- //std::random_shuffle( trios.begin() , trios.end() , mt );
- myShuffle<TrioF>( trios , mt );
- // show(trios);
- {
- long long sum_age = 0;
- std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
- pqxx::nontransaction N(C);
- for( int i=0 ; i<trios.size() ; i++ ) {
- std::string sql = "select name, sname, age from testcollection where name <= '" ;
- sql += trios[i].name;
- sql += "' ORDER BY name LIMIT 30 OFFSET 50000";
- // std::cout << sql << std::endl;
- pqxx::result R( N.exec( sql ) );
- for (pqxx::result::const_iterator c = R.begin(); c != R.end(); ++c) {
- sum_age += c[2].as<int>();
- // std::cout << c[0].as<std::string>() << " " << c[1].as<std::string>() << " " << c[2].as<int>() << std::endl;
- }
- // std::cout << std::endl;
- }
- std::cout << "sum_age = " << sum_age << std::endl;
- std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
- std::cout << elapsed_seconds.count() << "s" << std::endl;
- }
- C.disconnect ();
- } catch (const std::exception &e) {
- std::cerr << e.what() << std::endl;
- }
- }
- void mongoTest( const unsigned int seed ) {
- std::cout << "test mong start..." << std::endl;
- std::mt19937_64 mt( seed );
- // mongocxx::instance inst{};
- mongocxx::client conn{ mongocxx::uri() };
- auto collection = conn["test"]["testcollection"];
- collection.drop();
- bsoncxx::builder::stream::document document{};
- std::vector<TrioF> trios;
- {
- std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
- auto index_specification = bsoncxx::builder::stream::document() << "name" << 1 << bsoncxx::builder::stream::finalize;
- collection.create_index(std::move(index_specification));
- std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
- std::cout << elapsed_seconds.count() << "s" << std::endl;
- }
- {
- std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
- for( int i=0 ; i<SIZE1 ; i++ ) {
- const std::string name = rndStr( mt );
- const std::string sname = rndStr( mt );
- const int age = mt() % 1000000 ;
- if( i % SIZE2 == 0 ) {
- trios.push_back( TrioF(name,sname,age) );
- }
- document.clear();
- document << "name" << name;
- document << "sname" << sname;
- document << "age" << age;
- collection.insert_one( document.view() );
- }
- std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
- std::cout << elapsed_seconds.count() << "s" << std::endl;
- }
- myShuffle<TrioF>( trios , mt );
- {
- std::chrono::time_point<std::chrono::system_clock> bench = std::chrono::system_clock::now();
- auto order = bsoncxx::builder::stream::document() << "name" << 1 << bsoncxx::builder::stream::finalize;
- auto opts = mongocxx::options::find();
- opts.sort(order.view());
- opts.limit(30);
- opts.skip(50000);
- long long sum_age = 0;
- for( int i=0 ; i<trios.size() ; i++ ) {
- mongocxx::cursor cursor = collection.find(
- bsoncxx::builder::stream::document() <<
- "name" <<
- bsoncxx::builder::stream::open_document <<
- "$lte" <<
- trios[i].name <<
- bsoncxx::builder::stream::close_document <<
- bsoncxx::builder::stream::finalize
- ,
- opts
- );
- for( auto doc : cursor ) {
- // std::cout << bsoncxx::to_json( doc ) << std::endl;
- // sum_age += stoi( doc["age"].get_utf8().value.to_string() );
- sum_age += doc["age"].get_int32() ;
- // 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;
- }
- // std::cout << std::endl;
- }
- std::cout << "sum_age = " << sum_age << std::endl;
- std::chrono::duration<double> elapsed_seconds = std::chrono::system_clock::now() - bench;
- std::cout << elapsed_seconds.count() << "s" << std::endl;
- }
- }
- int main(int, char**) {
- // srand( 1234 );
- // for( int i=0 ; i<10 ; i++ )
- // std::cout << rand() << " ";
- // std::cout << std::endl;
- // srand( 1234 );
- // for( int i=0 ; i<10 ; i++ )
- // std::cout << rand() << " ";
- // std::cout << std::endl;
- mongoTest( 1234 );
- pgTest( 1234 );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement