xref: /glogg/tests/logfiltereddataPerfTest.cpp (revision 4fb0346e73d7caa82d42531c8c8681b5eb607728)
1 #include <QTest>
2 #include <QSignalSpy>
3 
4 #include "log.h"
5 #include "test_utils.h"
6 
7 #include "data/logdata.h"
8 #include "data/logfiltereddata.h"
9 
10 #include "gmock/gmock.h"
11 
12 #define TMPDIR "/tmp"
13 
14 static const qint64 VBL_NB_LINES = 4999999LL;
15 static const int VBL_LINE_PER_PAGE = 70;
16 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";
17 static const int VBL_LINE_LENGTH = (76+2+7) ; // Without the final '\n' !
18 static const int VBL_VISIBLE_LINE_LENGTH = (76+8+4+7); // Without the final '\n' !
19 
20 class PerfLogFilteredData : public testing::Test {
21   public:
PerfLogFilteredData()22     PerfLogFilteredData()
23         : log_data_(), filtered_data_( log_data_.getNewFilteredData() ),
24           progressSpy( filtered_data_.get(), SIGNAL( searchProgressed( int, int ) ) ) {
25         FILELog::setReportingLevel( logERROR );
26 
27         generateDataFiles();
28 
29         QSignalSpy endSpy( &log_data_, SIGNAL( loadingFinished( LoadingStatus ) ) );
30 
31         log_data_.attachFile( TMPDIR "/verybiglog.txt" );
32         if ( ! endSpy.wait( 999000 ) )
33             std::cerr << "Unable to attach the file!";
34     }
35 
36     LogData log_data_;
37     std::unique_ptr<LogFilteredData> filtered_data_;
38     QSignalSpy progressSpy;
39 
search()40     void search() {
41         int percent = 0;
42         do {
43             if ( progressSpy.wait( 10000 ) )
44                 percent = qvariant_cast<int>(progressSpy.last().at(1));
45             else
46                 std::cout << "Missed...\n";
47             // std::cout << "Progress " << percent << std::endl;
48         } while ( percent < 100 );
49     }
50 
generateDataFiles()51     bool generateDataFiles() {
52         char newLine[90];
53 
54         QFile file( TMPDIR "/verybiglog.txt" );
55         if ( file.open( QIODevice::WriteOnly ) ) {
56             for (int i = 0; i < VBL_NB_LINES; i++) {
57                 snprintf(newLine, 89, vbl_format, i);
58                 file.write( newLine, qstrlen(newLine) );
59             }
60         }
61         else {
62             return false;
63         }
64         file.close();
65 
66         return true;
67     }
68 };
69 
TEST_F(PerfLogFilteredData,allMatchingSearch)70 TEST_F( PerfLogFilteredData, allMatchingSearch ) {
71     {
72         TestTimer t;
73         filtered_data_->runSearch( QRegularExpression( "glogg.*this" ) );
74         search();
75     }
76     ASSERT_THAT( filtered_data_->getNbLine(), VBL_NB_LINES );
77 }
78 
TEST_F(PerfLogFilteredData,someMatchingSearch)79 TEST_F( PerfLogFilteredData, someMatchingSearch ) {
80     {
81         TestTimer t;
82         filtered_data_->runSearch( QRegularExpression( "1?3|34" ) );
83         search();
84     }
85     ASSERT_THAT( filtered_data_->getNbLine(), 2874236 );
86 }
87 
TEST_F(PerfLogFilteredData,noneMatchingSearch)88 TEST_F( PerfLogFilteredData, noneMatchingSearch ) {
89     {
90         TestTimer t;
91         filtered_data_->runSearch( QRegularExpression( "a1?3|(w|f)f34|blah" ) );
92         search();
93     }
94     ASSERT_THAT( filtered_data_->getNbLine(), 0 );
95 }
96 
TEST_F(PerfLogFilteredData,browsingSearchResults)97 TEST_F( PerfLogFilteredData, browsingSearchResults ) {
98     filtered_data_->runSearch( QRegularExpression( "1?3|34" ) );
99     search();
100     ASSERT_THAT( filtered_data_->getNbLine(), 2874236 );
101 
102     // Read page by page from the beginning and the end, using the QStringList
103     // function
104     std::cout << "Start reading..." << std::endl;
105     QStringList list;
106     {
107         TestTimer t;
108 
109         const int nb_results = filtered_data_->getNbLine();
110         for (int page = 0; page < (nb_results/VBL_LINE_PER_PAGE)-1; page++)
111         {
112             list = filtered_data_->getExpandedLines(
113                     page * VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
114             ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
115             int page_from_end = (nb_results/VBL_LINE_PER_PAGE) - page - 1;
116             list = filtered_data_->getExpandedLines(
117                     page_from_end * VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
118             ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
119         }
120     }
121 }
122 
123