1 /*
2  ai_benchmark.cpp     MindForger markdown test
3 
4  Copyright (C) 2016-2020 Martin Dvorak <martin.dvorak@mindforger.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <string>
21 #include <iostream>
22 
23 #include <gtest/gtest.h>
24 
25 #include "../../src/mind/mind.h"
26 
27 using namespace std;
28 using namespace m8r;
29 
30 extern char* getMindforgerGitHomePath();
31 
32 /*
33  * Performance improvements ideas:
34  *   - IF |notes|>1000 THEN split aaMatrix rows to launch(CPU-1) tasks > start thread for each task > join threads (thread x down ~ 80% down)
35  *   - compute above aaMatrix diagonal values only (2x faster ~ 50% down)
36  *   - use trie instead of map in lexicon (10% faster ~ 10% down)
37  *   - heuristics:
38  *     - codereview calculateSimilarityByWords() > WordFrequencyList::evalUnion/Intersection must be MUCH faster
39  *     - skip Ns w/ empty description
40  *
41  * Conclusion:
42  *   - can be made 10x faster
43  *   - 6' (5k Ns) to 36s ... which is still slow
44  */
45 /*
46  * Measurements
47  *
48  * 2018/03/31 ...  46s (<1') , 5.000 Ns, 12.560.072 rankings in aaMatrix (5k^2 / 2) ... added stemmer (less words), only 10 relevant words compared
49  * 2018/03/31 ... 129s (2')  , 5.000 Ns, 12.560.072 rankings in aaMatrix (5k^2 / 2) ... 1/2 of matrix, simplified vectors weight computation
50  * 2018/03/31 ... 338s (5'30), 5.000 Ns, 12.560.072 rankings in aaMatrix (5k^2 / 2)
51  */
TEST(AiBenchmark,DISABLED_AaMatrix)52 TEST(AiBenchmark, DISABLED_AaMatrix)
53 {
54     string repositoryPath{"/lib/test/resources/benchmark-repository"};
55     repositoryPath.insert(0, getMindforgerGitHomePath());
56     m8r::Configuration& config = m8r::Configuration::getInstance();
57     config.clear();
58     config.setConfigFilePath("/tmp/cfg-aib-am.md");
59     config.setActiveRepository(config.addRepository(m8r::RepositoryIndexer::getRepositoryForPath(repositoryPath)));
60     m8r::Mind mind(config);
61     mind.think();
62     cout << "Statistics:" << endl
63     << "  Outlines: " << mind.remind().getOutlinesCount() << endl
64     << "  Bytes   : " << mind.remind().getOutlineMarkdownsSize() << endl;
65 
66     ASSERT_LE(1, mind.remind().getOutlinesCount());
67 
68     /*
69      * Tokenize repository > make AI to think > find the most similar Notes pair
70      */
71 
72     auto beginDream = chrono::high_resolution_clock::now();
73 
74     // TODO to be rewritten mind.dream();
75 
76     auto endDream = chrono::high_resolution_clock::now();
77     MF_DEBUG(endl << "Dream DONE in " << chrono::duration_cast<chrono::microseconds>(endDream-beginDream).count()/1000.0 << "ms" << endl);
78 
79     // get the best associations of N
80     m8r::Note* n=mind.remind().getOutlines()[0]->getNotes()[0];
81     UNUSED_ARG(n);
82     std::vector<std::pair<m8r::Note*,float>> lb{};
83     // TODO to be rewritten mind.getAssociationsLeaderboard(n, lb);
84     // TODO to be rewritten m8r::Ai::print(n,lb);
85 }
86