From 009e290880347119be07e49973a3b72f0634caf3 Mon Sep 17 00:00:00 2001
From: Davide Aprea <davide@suma-ev.de>
Date: Tue, 1 Dec 2020 11:20:09 +0100
Subject: [PATCH] add resultpage to stress test

---
 app/Http/Controllers/MetaGerSearch.php |  10 ++-
 app/Http/Controllers/Stresstest.php    | 107 +++----------------------
 app/MetaGer.php                        |  83 ++++---------------
 routes/web.php                         |   1 +
 4 files changed, 33 insertions(+), 168 deletions(-)

diff --git a/app/Http/Controllers/MetaGerSearch.php b/app/Http/Controllers/MetaGerSearch.php
index e1aaf1ba5..114768abd 100644
--- a/app/Http/Controllers/MetaGerSearch.php
+++ b/app/Http/Controllers/MetaGerSearch.php
@@ -88,9 +88,13 @@ class MetaGerSearch extends Controller
         $metager->startSearch($timings);
 
         # Versuchen die Ergebnisse der Quicktips zu laden
-        $quicktipResults = $quicktips->loadResults();
-        if (!empty($timings)) {
-            $timings["Loaded Quicktip Results"] = microtime(true) - $time;
+        if($quicktips !== null) {
+            $quicktipResults = $quicktips->loadResults();
+            if (!empty($timings)) {
+                $timings["Loaded Quicktip Results"] = microtime(true) - $time;
+            }
+        } else {
+            $quicktipResults = [];
         }
 
         $metager->waitForMainResults();
diff --git a/app/Http/Controllers/Stresstest.php b/app/Http/Controllers/Stresstest.php
index bbead2759..883223845 100644
--- a/app/Http/Controllers/Stresstest.php
+++ b/app/Http/Controllers/Stresstest.php
@@ -10,106 +10,19 @@ use LaravelLocalization;
 use Log;
 use View;
 
-class Stresstest extends Controller
+class Stresstest extends MetaGerSearch
 {
-    public function index(Metager $metager){
-        $this->dummySearch($metager);
+    public function index(Metager $metager)
+    {
+        return redirect("admin/stress/search?eingabe=test");
     }
 
-    private function dummySearch(Metager $metager){
-
-        $timings = null;
-        $timings = ['starttime' => microtime(true)];
-        $time = microtime(true);
-        $spamEntries = [];
-        $spamEntry = null;
-        if (file_exists(config_path('spam.txt'))) {
-            $spamEntries = file(config_path('spam.txt'));
-        }
-
-        # If there is no query parameter we redirect to the startpage
-        $eingabe = "test";
-
-        # Mit gelieferte Formulardaten parsen und abspeichern:
-        $metager->insertDummyFormData();
-        if (!empty($timings)) {
-            $timings["insertDummyFormData"] = microtime(true) - $time;
-        }
-
-        # Suche für alle zu verwendenden Suchmaschinen als Job erstellen,
-        # auf Ergebnisse warten und die Ergebnisse laden
-        $metager->startSearch($timings);
-
-        $metager->waitForMainResults();
-        if (!empty($timings)) {
-            $timings["waitForMainResults"] = microtime(true) - $time;
-        }
-
-        $metager->retrieveResults();
-        if (!empty($timings)) {
-            $timings["retrieveResults"] = microtime(true) - $time;
-        }
-
-        # Alle Ergebnisse vor der Zusammenführung ranken:
-        $metager->rankAll();
-        if (!empty($timings)) {
-            $timings["rankAll"] = microtime(true) - $time;
-        }
-
-        # Ergebnisse der Suchmaschinen kombinieren:
-        $metager->prepareResults($timings);
-
-        $finished = true;
-        foreach ($metager->getEngines() as $engine) {
-            if ($engine->loaded) {
-                $engine->setNew(false);
-                $engine->markNew();
-            }
-        }
-
-        try {
-            Cache::put("loader_" . $metager->getSearchUid(), [
-                "metager" => [
-                    "apiAuthorized" => $metager->isApiAuthorized(),
-                ],
-                "adgoal" => [
-                    "loaded" => $metager->isAdgoalLoaded(),
-                    "adgoalHash" => $metager->getAdgoalHash(),
-                ],
-                "engines" => $metager->getEngines(),
-            ], 60 * 60);
-        } catch (\Exception $e) {
-            Log::error($e->getMessage());
-        }
-        if (!empty($timings)) {
-            $timings["Filled resultloader Cache"] = microtime(true) - $time;
-        }
-
-        # Die Ausgabe erstellen:
-        //$resultpage = $metager->createView([]);
-
-        if (!empty($timings)) {
-            $timings["createView"] = microtime(true) - $time;
-        }
-
-        if ($timings) {
-            dd($timings);
-        }
-
-        $registry = \Prometheus\CollectorRegistry::getDefault();
-        $counter = $registry->getOrRegisterCounter('metager', 'result_counter', 'counts total number of returned results', []);
-        $counter->incBy(sizeof($metager->getResults()));
-        $counter = $registry->getOrRegisterCounter('metager', 'query_counter', 'counts total number of search queries', []);
-        $counter->inc();
-
-        // Splitting the response return into multiple parts.
-        // This might speed up page view time for users with slow network
-        $responseArray = str_split($resultpage->render(), 1024);
-        foreach ($responseArray as $responsePart) {
-            echo($responsePart);
-            flush();
+    public function search(Request $request, MetaGer $metager, $timing = false)
+    {
+        if(empty($request->input('eingabe'))) {
+            return redirect("admin/stress/search?eingabe=test");
         }
-        $requestTime = microtime(true) - $time;
-        \App\PrometheusExporter::Duration($requestTime, "request_time");
+        $metager->setDummy(true);
+        parent::search($request, $metager, $timing);
     }
 }
\ No newline at end of file
diff --git a/app/MetaGer.php b/app/MetaGer.php
index 921e4adbe..5581be75e 100644
--- a/app/MetaGer.php
+++ b/app/MetaGer.php
@@ -78,6 +78,7 @@ class MetaGer
     protected $redisEngineResult;
     protected $redisCurrentResultList;
     public $starttime;
+    protected $dummy = false;
 
     public function __construct($hash = "")
     {
@@ -300,7 +301,7 @@ class MetaGer
         if (empty($this->adgoalLoaded)) {
             $this->adgoalLoaded = false;
         }
-        if (!$this->apiAuthorized && !$this->adgoalLoaded) {
+        if (!$this->apiAuthorized && !$this->adgoalLoaded && !$this->dummy) {
             if (empty($this->adgoalHash)) {
                 if (!empty($this->jskey)) {
                     $js = Redis::connection('cache')->lpop("js" . $this->jskey);
@@ -1023,69 +1024,6 @@ class MetaGer
      * Ende Suchmaschinenerstellung und Ergebniserhalt
      */
 
-    public function insertDummyFormData(){
-
-        $this->headerPrinted = false;
-        $this->javascript = false;
-        $this->jskey = '';
-        $this->url = 'https://metager.de/meta/meta.ger3';
-        $this->fullUrl = 'https://metager.de/meta/meta.ger3?eingabe=test';
-        $this->fokus = 'web';
-        $this->sumaFile = config_path() . "/stress.json";
-
-        if (!file_exists($this->sumaFile)) {
-            die(trans('metaGer.formdata.cantLoad'));
-        } else {
-            $this->sumaFile = json_decode(file_get_contents($this->sumaFile));
-        }
-
-        $this->eingabe = 'test';
-        $this->q = $this->eingabe;
-        $this->framed = false;
-        $this->ip = '127.0.0.1';
-        $this->useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0';
-
-        if (isset($_SERVER['HTTP_LANGUAGE'])) {
-            $this->language = $_SERVER['HTTP_LANGUAGE'];
-        } else {
-            $this->language = "";
-        }
-
-        $this->page = 1;
-        $this->lang = "all";
-        $this->agent = new Agent();
-        $this->mobile = $this->agent->isMobile();
-        $this->sprueche = 'off';
-        $this->newtab = 'off';
-
-        $this->theme = 'default';
-        $this->resultCount = '20';
-
-        if ($this->resultCount <= 0 || $this->resultCount > 200) {
-            $this->resultCount = 1000;
-        }
-
-        $this->quicktips = false;
-        $this->queryFilter = [];
-        $this->verificationId = null;
-        $this->verificationCount = 0;
-        $this->apiKey = "";
-        $this->out = "html";
-        $this->canCache = false;
-        $this->shouldLog = false;
-
-        $this->fokus = 'web';
-
-        $sumaNames = $this->sumaFile->foki->{$this->fokus}->sumas;
-
-        $sumas = [];
-        foreach ($sumaNames as $sumaName) {
-            $sumas[$sumaName] = $this->sumaFile->sumas->{$sumaName};
-        }
-
-        $this->actuallyCreateSearchEngines($this->enabledSearchengines);
-    }
-
     public function parseFormData(Request $request, $auth = true)
     {
         # Sichert, dass der request in UTF-8 formatiert ist
@@ -1117,9 +1055,9 @@ class MetaGer
         $this->fokus = $request->input('focus', 'web');
         # Suma-File
         if (App::isLocale("en")) {
-            $this->sumaFile = config_path() . "/sumasEn.json";
+            $this->sumaFile = config_path() . ($this->dummy ? "/stress.json" : "/sumasEn.json");
         } else {
-            $this->sumaFile = config_path() . "/sumas.json";
+            $this->sumaFile = config_path() . ($this->dummy ? "/stress.json" : "/sumas.json");
         }
         if (!file_exists($this->sumaFile)) {
             die(trans('metaGer.formdata.cantLoad'));
@@ -1298,8 +1236,12 @@ class MetaGer
     public function createQuicktips()
     {
         # Die quicktips werden als job erstellt und zur Abarbeitung freigegeben
-        $quicktips = new \App\Models\Quicktips\Quicktips($this->q, LaravelLocalization::getCurrentLocale(), $this->getTime(), $this->sprueche);
-        return $quicktips;
+        if(!$this->dummy) {
+            $quicktips = new \App\Models\Quicktips\Quicktips($this->q, LaravelLocalization::getCurrentLocale(), $this->getTime(), $this->sprueche);
+            return $quicktips;
+        }else {
+            return null;
+        }
     }
 
 
@@ -2099,4 +2041,9 @@ class MetaGer
     {
         $this->engines = $engines;
     }
+
+    public function setDummy($dummy)
+    {
+        $this->dummy = $dummy;
+    }
 }
diff --git a/routes/web.php b/routes/web.php
index 75116d3bb..7d3d9741e 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -193,6 +193,7 @@ Route::group(
             });
             Route::post('service-desk', 'ServiceDesk@webhook');
             Route::get('stress', 'Stresstest@index');
+            Route::get('stress/search', 'Stresstest@search');
         });
 
         Route::get('settings', function () {
-- 
GitLab