Warning, file /kdevelop/kdev-python/duchain/tests/duchainbench.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2014 Benjamin Kaiser <benjaminjkaiser@gmail.com> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include <QDebug> 0008 #include "duchaindebug.h" 0009 0010 #include "duchainbench.h" 0011 0012 #include <language/duchain/topducontext.h> 0013 #include <language/codegen/coderepresentation.h> 0014 #include <tests/autotestshell.h> 0015 #include <tests/testcore.h> 0016 #include <language/duchain/duchain.h> 0017 #include <QtTest> 0018 #include <language/backgroundparser/backgroundparser.h> 0019 #include <interfaces/ilanguagecontroller.h> 0020 #include <QStandardPaths> 0021 0022 #include "parsesession.h" 0023 0024 QTEST_MAIN(DUChainBench) 0025 0026 using namespace KDevelop; 0027 using namespace Python; 0028 0029 0030 DUChainBench::DUChainBench(QObject* parent): QObject(parent) 0031 { 0032 testDir = QDir(testDirOwner.path()); 0033 0034 initShell(); 0035 } 0036 0037 0038 void DUChainBench::initShell() 0039 { 0040 AutoTestShell::init(); 0041 TestCore* core = new TestCore(); 0042 core->initialize(KDevelop::Core::NoUi); 0043 0044 auto doc_url = QDir::cleanPath(QStandardPaths::locate(QStandardPaths::GenericDataLocation, 0045 "kdevpythonsupport/documentation_files/builtindocumentation.py")); 0046 0047 DUChain::self()->updateContextForUrl(IndexedString(doc_url), KDevelop::TopDUContext::AllDeclarationsContextsAndUses); 0048 ICore::self()->languageController()->backgroundParser()->parseDocuments(); 0049 DUChain::self()->waitForUpdate(IndexedString(doc_url), KDevelop::TopDUContext::AllDeclarationsContextsAndUses); 0050 0051 DUChain::self()->disablePersistentStorage(); 0052 KDevelop::CodeRepresentation::setDiskChangesForbidden(true); 0053 } 0054 0055 ReferencedTopDUContext DUChainBench::parse(const QString& code) 0056 { 0057 TestFile* testfile = new TestFile(code + "\n", "py", nullptr, testDir.absolutePath().append("/")); 0058 createdFiles << testfile; 0059 testfile->parse(TopDUContext::ForceUpdate | TopDUContext::AST); 0060 testfile->waitForParsed(2000); 0061 0062 if ( testfile->isReady() ) { 0063 m_ast = static_cast<Python::ParseSession*>(testfile->topContext()->ast().data())->ast; 0064 return testfile->topContext(); 0065 } 0066 else Q_ASSERT(false && "Timed out waiting for parser results, aborting all tests"); 0067 return nullptr; 0068 } 0069 0070 DUChainBench::~DUChainBench() 0071 { 0072 foreach ( TestFile* f, createdFiles ) { 0073 delete f; 0074 } 0075 testDir.rmdir(testDir.absolutePath()); 0076 } 0077 0078 QString repeat_distinct(const QString& code, int count) { 0079 QString result; 0080 QString line; 0081 for ( int i = 0; i < count; i++ ) { 0082 line = code; 0083 result.append(line.replace(QString("%X"), QString::number(i))); 0084 } 0085 return result; 0086 } 0087 0088 void DUChainBench::benchSimpleStatements_data() 0089 { 0090 QTest::addColumn<QString>("code"); 0091 0092 // test assignment 0093 QTest::newRow("test_nondistinct_assignment_repeated") << QString("a=3\n").repeated(200); 0094 QTest::newRow("test_nondistinct_assignment_looped") << repeat_distinct(QString("a=%X\n"), 200); 0095 QTest::newRow("test_distinct_assignment_repeated") << repeat_distinct(QString("a%X=3\n"), 200); 0096 QTest::newRow("test_distinct_assignment_looped") << repeat_distinct(QString("a%X=%X\n"), 200); 0097 // test function 0098 QTest::newRow("test_bare_function") << repeat_distinct(QString("def main%X():\n pass\n"), 100); 0099 QTest::newRow("test_return_function") << repeat_distinct(QString("def main%X():\n return %X\n"), 100); 0100 QTest::newRow("test_arg_function_return_var") << repeat_distinct(QString("def func%X(arg):\n return arg\na%X = func%X(3)\n"), 200); 0101 QTest::newRow("test_arg_function_return_fixed") << repeat_distinct(QString("def func%X(arg):\n return 3\na%X = func%X()\n"), 200); 0102 // test if statements 0103 QTest::newRow("test_if_statement") << repeat_distinct(QString("if(True):\n pass\n"), 200); 0104 QTest::newRow("test_if_else_statement") << repeat_distinct(QString("if(True):\n pass\nelse:\n pass\n"), 200); 0105 // test for loops 0106 QTest::newRow("test_for_loop") << repeat_distinct(QString("for i in range(20):\n pass\n"), 100); 0107 QTest::newRow("test_for_loop_enum") << repeat_distinct(QString("for key, value in enumerate({1:2, 7:3}):\n pass\n"), 200); 0108 QTest::newRow("test_for_loop_list") << repeat_distinct(QString("for key, value in [(3, 5), (7, 9)]:\n pass\n"), 200); 0109 } 0110 0111 void DUChainBench::benchSimpleStatements() 0112 { 0113 QFETCH(QString, code); 0114 QBENCHMARK { 0115 parse(code); 0116 } 0117 } 0118 0119 #include "moc_duchainbench.cpp"