File indexing completed on 2024-05-19 04:36:54

0001 /*
0002     SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "bench_completion.h"
0008 
0009 #include "helper.h"
0010 #include "../context.h"
0011 #include "../completiondebug.h"
0012 
0013 #include <language/codecompletion/codecompletiontesthelper.h>
0014 
0015 #ifdef USE_VALGRIND
0016   #include <valgrind/callgrind.h>
0017 #endif
0018 
0019 #include <tests/autotestshell.h>
0020 #include <tests/testcore.h>
0021 #include <language/codegen/coderepresentation.h>
0022 
0023 using namespace Php;
0024 using namespace KDevelop;
0025 
0026 QTEST_MAIN(Php::BenchmarkCodeCompletion)
0027 
0028 namespace Php {
0029 
0030 QFile* getFile(const QString& path)
0031 {
0032     QFile* file = new QFile(QFINDTESTDATA(path));
0033     qDebug() << file->fileName();
0034     Q_ASSERT(file->exists());
0035     file->open(QIODevice::ReadOnly);
0036     Q_ASSERT(!file->error());
0037     Q_ASSERT(file->isReadable());
0038     return file;
0039 }
0040 
0041 typedef CodeCompletionItemTester<CodeCompletionContext> PhpCompletionTester;
0042 
0043 void BenchmarkCodeCompletion::initTestCase()
0044 {
0045     AutoTestShell::init();
0046     auto* core = new TestCore();
0047     core->initialize(KDevelop::Core::NoUi);
0048 
0049     DUChain::self()->disablePersistentStorage();
0050 
0051     // make sure we have a valid duchain for the global file
0052     DUChainReadLocker lock(DUChain::lock());
0053     if ( !DUChain::self()->chainForDocument(internalFunctionFile()) ) {
0054         qDebug() << "no internal function file found in DUChain, loading it manually";
0055         QString fileName = internalFunctionFile().str();
0056         QScopedPointer<QIODevice> file(new QFile(fileName));
0057         if ( !file->open(QIODevice::ReadOnly) ) {
0058             qDebug() << "Could not open file" << fileName;
0059             return;
0060         }
0061         lock.unlock();
0062         parseAdditionalFile(internalFunctionFile(), file->readAll());
0063     }
0064 }
0065 
0066 void BenchmarkCodeCompletion::globalCompletion()
0067 {
0068     qDebug() << "benching global completion";
0069     TopDUContext* top = parse("<?php ", DumpNone);
0070     DUChainReleaser releaseTop(top);
0071     DUChainWriteLocker lock(DUChain::lock());
0072 
0073     #ifdef USE_VALGRIND
0074         CALLGRIND_TOGGLE_COLLECT
0075     #endif
0076 
0077     QBENCHMARK {
0078         PhpCompletionTester tester(top, QStringLiteral("<?php "));
0079     }
0080 
0081     #ifdef USE_VALGRIND
0082         CALLGRIND_TOGGLE_COLLECT
0083     #endif
0084 }
0085 
0086 void BenchmarkCodeCompletion::globalCompletionBigFile()
0087 {
0088     QFile* file(getFile(QStringLiteral("../../create_functions.php")));
0089     const QString contents( file->readAll() );
0090     delete file;
0091 
0092     TopDUContext* top = parse(contents.toUtf8(), DumpNone);
0093     DUChainReleaser releaseTop(top);
0094     DUChainWriteLocker lock(DUChain::lock());
0095 
0096     #ifdef USE_VALGRIND
0097         CALLGRIND_TOGGLE_COLLECT
0098     #endif
0099 
0100     QBENCHMARK {
0101         PhpCompletionTester tester(top, contents);
0102     }
0103 
0104     #ifdef USE_VALGRIND
0105         CALLGRIND_TOGGLE_COLLECT
0106     #endif
0107 }
0108 
0109 void BenchmarkCodeCompletion::completionData()
0110 {
0111     qDebug() << "benching global completion";
0112     TopDUContext* top = parse("<?php ", DumpNone);
0113     DUChainReleaser releaseTop(top);
0114     DUChainWriteLocker lock(DUChain::lock());
0115 
0116     PhpCompletionTester tester(top, QStringLiteral("<?php "));
0117 
0118     #ifdef USE_VALGRIND
0119         CALLGRIND_TOGGLE_COLLECT
0120     #endif
0121 
0122     const int size = tester.items.size();
0123     QBENCHMARK {
0124         for ( int i = 0; i < size; ++i ) {
0125             tester.itemData(i, KTextEditor::CodeCompletionModel::Prefix);
0126             tester.itemData(i, KTextEditor::CodeCompletionModel::Name);
0127             tester.itemData(i, KTextEditor::CodeCompletionModel::Postfix);
0128         }
0129     }
0130     #ifdef USE_VALGRIND
0131         CALLGRIND_TOGGLE_COLLECT
0132     #endif
0133 }
0134 
0135 }
0136 
0137 #include "moc_bench_completion.cpp"