xref: /glogg/tests/logfiltereddataPerfTest.cpp (revision 4fb0346e73d7caa82d42531c8c8681b5eb607728)
174d66bf4SNicolas Bonnefon #include <QTest>
274d66bf4SNicolas Bonnefon #include <QSignalSpy>
374d66bf4SNicolas Bonnefon 
474d66bf4SNicolas Bonnefon #include "log.h"
574d66bf4SNicolas Bonnefon #include "test_utils.h"
674d66bf4SNicolas Bonnefon 
774d66bf4SNicolas Bonnefon #include "data/logdata.h"
874d66bf4SNicolas Bonnefon #include "data/logfiltereddata.h"
974d66bf4SNicolas Bonnefon 
1074d66bf4SNicolas Bonnefon #include "gmock/gmock.h"
1174d66bf4SNicolas Bonnefon 
1274d66bf4SNicolas Bonnefon #define TMPDIR "/tmp"
1374d66bf4SNicolas Bonnefon 
1474d66bf4SNicolas Bonnefon static const qint64 VBL_NB_LINES = 4999999LL;
1574d66bf4SNicolas Bonnefon static const int VBL_LINE_PER_PAGE = 70;
1674d66bf4SNicolas Bonnefon static const char* vbl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line\t\t%07d\n";
1774d66bf4SNicolas Bonnefon static const int VBL_LINE_LENGTH = (76+2+7) ; // Without the final '\n' !
1874d66bf4SNicolas Bonnefon static const int VBL_VISIBLE_LINE_LENGTH = (76+8+4+7); // Without the final '\n' !
1974d66bf4SNicolas Bonnefon 
2074d66bf4SNicolas Bonnefon class PerfLogFilteredData : public testing::Test {
2174d66bf4SNicolas Bonnefon   public:
PerfLogFilteredData()2274d66bf4SNicolas Bonnefon     PerfLogFilteredData()
2374d66bf4SNicolas Bonnefon         : log_data_(), filtered_data_( log_data_.getNewFilteredData() ),
2474d66bf4SNicolas Bonnefon           progressSpy( filtered_data_.get(), SIGNAL( searchProgressed( int, int ) ) ) {
2574d66bf4SNicolas Bonnefon         FILELog::setReportingLevel( logERROR );
2674d66bf4SNicolas Bonnefon 
2774d66bf4SNicolas Bonnefon         generateDataFiles();
2874d66bf4SNicolas Bonnefon 
2974d66bf4SNicolas Bonnefon         QSignalSpy endSpy( &log_data_, SIGNAL( loadingFinished( LoadingStatus ) ) );
3074d66bf4SNicolas Bonnefon 
3174d66bf4SNicolas Bonnefon         log_data_.attachFile( TMPDIR "/verybiglog.txt" );
3274d66bf4SNicolas Bonnefon         if ( ! endSpy.wait( 999000 ) )
3374d66bf4SNicolas Bonnefon             std::cerr << "Unable to attach the file!";
3474d66bf4SNicolas Bonnefon     }
3574d66bf4SNicolas Bonnefon 
3674d66bf4SNicolas Bonnefon     LogData log_data_;
3774d66bf4SNicolas Bonnefon     std::unique_ptr<LogFilteredData> filtered_data_;
3874d66bf4SNicolas Bonnefon     QSignalSpy progressSpy;
3974d66bf4SNicolas Bonnefon 
search()4074d66bf4SNicolas Bonnefon     void search() {
4174d66bf4SNicolas Bonnefon         int percent = 0;
4274d66bf4SNicolas Bonnefon         do {
4374d66bf4SNicolas Bonnefon             if ( progressSpy.wait( 10000 ) )
4474d66bf4SNicolas Bonnefon                 percent = qvariant_cast<int>(progressSpy.last().at(1));
4574d66bf4SNicolas Bonnefon             else
4674d66bf4SNicolas Bonnefon                 std::cout << "Missed...\n";
47ced968a9SNicolas Bonnefon             // std::cout << "Progress " << percent << std::endl;
4874d66bf4SNicolas Bonnefon         } while ( percent < 100 );
4974d66bf4SNicolas Bonnefon     }
5074d66bf4SNicolas Bonnefon 
generateDataFiles()5174d66bf4SNicolas Bonnefon     bool generateDataFiles() {
5274d66bf4SNicolas Bonnefon         char newLine[90];
5374d66bf4SNicolas Bonnefon 
5474d66bf4SNicolas Bonnefon         QFile file( TMPDIR "/verybiglog.txt" );
5574d66bf4SNicolas Bonnefon         if ( file.open( QIODevice::WriteOnly ) ) {
5674d66bf4SNicolas Bonnefon             for (int i = 0; i < VBL_NB_LINES; i++) {
5774d66bf4SNicolas Bonnefon                 snprintf(newLine, 89, vbl_format, i);
5874d66bf4SNicolas Bonnefon                 file.write( newLine, qstrlen(newLine) );
5974d66bf4SNicolas Bonnefon             }
6074d66bf4SNicolas Bonnefon         }
6174d66bf4SNicolas Bonnefon         else {
6274d66bf4SNicolas Bonnefon             return false;
6374d66bf4SNicolas Bonnefon         }
6474d66bf4SNicolas Bonnefon         file.close();
6574d66bf4SNicolas Bonnefon 
6674d66bf4SNicolas Bonnefon         return true;
6774d66bf4SNicolas Bonnefon     }
6874d66bf4SNicolas Bonnefon };
6974d66bf4SNicolas Bonnefon 
TEST_F(PerfLogFilteredData,allMatchingSearch)7074d66bf4SNicolas Bonnefon TEST_F( PerfLogFilteredData, allMatchingSearch ) {
7174d66bf4SNicolas Bonnefon     {
7274d66bf4SNicolas Bonnefon         TestTimer t;
73*4fb0346eSAnton Filimonov         filtered_data_->runSearch( QRegularExpression( "glogg.*this" ) );
7474d66bf4SNicolas Bonnefon         search();
7574d66bf4SNicolas Bonnefon     }
7674d66bf4SNicolas Bonnefon     ASSERT_THAT( filtered_data_->getNbLine(), VBL_NB_LINES );
7774d66bf4SNicolas Bonnefon }
7874d66bf4SNicolas Bonnefon 
TEST_F(PerfLogFilteredData,someMatchingSearch)7974d66bf4SNicolas Bonnefon TEST_F( PerfLogFilteredData, someMatchingSearch ) {
8074d66bf4SNicolas Bonnefon     {
8174d66bf4SNicolas Bonnefon         TestTimer t;
82*4fb0346eSAnton Filimonov         filtered_data_->runSearch( QRegularExpression( "1?3|34" ) );
8374d66bf4SNicolas Bonnefon         search();
8474d66bf4SNicolas Bonnefon     }
8574d66bf4SNicolas Bonnefon     ASSERT_THAT( filtered_data_->getNbLine(), 2874236 );
8674d66bf4SNicolas Bonnefon }
8774d66bf4SNicolas Bonnefon 
TEST_F(PerfLogFilteredData,noneMatchingSearch)8874d66bf4SNicolas Bonnefon TEST_F( PerfLogFilteredData, noneMatchingSearch ) {
8974d66bf4SNicolas Bonnefon     {
9074d66bf4SNicolas Bonnefon         TestTimer t;
91*4fb0346eSAnton Filimonov         filtered_data_->runSearch( QRegularExpression( "a1?3|(w|f)f34|blah" ) );
9274d66bf4SNicolas Bonnefon         search();
9374d66bf4SNicolas Bonnefon     }
9474d66bf4SNicolas Bonnefon     ASSERT_THAT( filtered_data_->getNbLine(), 0 );
9574d66bf4SNicolas Bonnefon }
9674d66bf4SNicolas Bonnefon 
TEST_F(PerfLogFilteredData,browsingSearchResults)9774d66bf4SNicolas Bonnefon TEST_F( PerfLogFilteredData, browsingSearchResults ) {
98*4fb0346eSAnton Filimonov     filtered_data_->runSearch( QRegularExpression( "1?3|34" ) );
9974d66bf4SNicolas Bonnefon     search();
10074d66bf4SNicolas Bonnefon     ASSERT_THAT( filtered_data_->getNbLine(), 2874236 );
10174d66bf4SNicolas Bonnefon 
10274d66bf4SNicolas Bonnefon     // Read page by page from the beginning and the end, using the QStringList
10374d66bf4SNicolas Bonnefon     // function
10474d66bf4SNicolas Bonnefon     std::cout << "Start reading..." << std::endl;
10574d66bf4SNicolas Bonnefon     QStringList list;
10674d66bf4SNicolas Bonnefon     {
10774d66bf4SNicolas Bonnefon         TestTimer t;
10874d66bf4SNicolas Bonnefon 
10974d66bf4SNicolas Bonnefon         const int nb_results = filtered_data_->getNbLine();
11074d66bf4SNicolas Bonnefon         for (int page = 0; page < (nb_results/VBL_LINE_PER_PAGE)-1; page++)
11174d66bf4SNicolas Bonnefon         {
11274d66bf4SNicolas Bonnefon             list = filtered_data_->getExpandedLines(
11374d66bf4SNicolas Bonnefon                     page * VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
11474d66bf4SNicolas Bonnefon             ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
11574d66bf4SNicolas Bonnefon             int page_from_end = (nb_results/VBL_LINE_PER_PAGE) - page - 1;
11674d66bf4SNicolas Bonnefon             list = filtered_data_->getExpandedLines(
11774d66bf4SNicolas Bonnefon                     page_from_end * VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
11874d66bf4SNicolas Bonnefon             ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
11974d66bf4SNicolas Bonnefon         }
12074d66bf4SNicolas Bonnefon     }
12174d66bf4SNicolas Bonnefon }
12274d66bf4SNicolas Bonnefon 
123