Skip to content

Commit

Permalink
opensearch: added performance test button to admin gui
Browse files Browse the repository at this point in the history
  • Loading branch information
drfho committed Jul 4, 2024
1 parent d7b002a commit 2ed56bb
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ class Attrs:
,"repetitive":0
,"type":"External Method"}

manage_opensearch_test = {"default":""
,"id":"manage_opensearch_test"
,"keys":[]
,"mandatory":0
,"multilang":0
,"name":"Test: OpenSearch connection"
,"repetitive":0
,"type":"External Method"}

opensearch_query = {"default":""
,"id":"opensearch_query"
,"keys":[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from opensearchpy import OpenSearch
from urllib.parse import urlparse
import json
import traceback
import time

def manage_opensearch_test(self):
client = get_opensearch_client(self)
request = self.REQUEST
call_amount = int(request.get('calls', 10))
request.set('q', request.get('q', 'Universität'))
prettify = bool(int(request.get('prettify',0)))
response = {}
try:
response['status'] = 200
response['clusterInfo'] = client.info()
tests = {}
tests['options'] = {}
tests['options']['query'] = request.get('q')
tests['options']['callAmount'] = call_amount
response['tests'] = tests
# Call OpenSearch query multiple times
query_result = {}
query_result['performance'] = {}
query_result_took = query_result['performance']['took'] = {}
query_result_took['sum'] = 0
query_start = time.time()
for i in range(call_amount):
stringified_query_response = self.opensearch_query()
query_response = json.loads(stringified_query_response)
query_result_took['sum'] += query_response['took']
if i == 0:
query_result['response'] = query_response
query_end = time.time()
query_result_took['average'] = query_result_took['sum'] / call_amount
query_result_elapsed_time = query_result['performance']['ellapsedTime'] = {}
query_result_elapsed_time['sum'] = (query_end - query_start) * 1000
query_result_elapsed_time['average'] = query_result_elapsed_time['sum'] / call_amount
tests['opensearchQuery'] = query_result

# Call OpenSearch suggest multiple times
suggest_result = {}
suggest_result['performance'] = {}
suggest_start = time.time()
for i in range(call_amount):
stringified_suggest_response = self.opensearch_suggest()
suggest_response = json.loads(stringified_suggest_response)
if i == 0:
suggest_result['response'] = suggest_response
suggest_end = time.time()
suggest_result_elapsed_time = suggest_result['performance']['ellapsedTime'] = {}
suggest_result_elapsed_time['sum'] = (suggest_end - suggest_start) * 1000
suggest_result_elapsed_time['average'] = suggest_result_elapsed_time['sum'] / call_amount
tests['opensearchSuggest'] = suggest_result
except Exception as e:
response['status'] = 500
response['error'] = str(e)
response['trace'] = traceback.format_exc()

if prettify:
resp_text = json.dumps(response, indent=4)
else:
resp_text = json.dumps(response)

return resp_text


def get_opensearch_client(self):
# ${opensearch.url:https://localhost:9200, https://localhost:9201}
# ${opensearch.username:admin}
# ${opensearch.password:admin}
# ${opensearch.ssl.verify:}
url_string = self.getConfProperty('opensearch.url')
urls = [url.strip().rstrip('/') for url in url_string.split(',')]
hosts = []
use_ssl = False
# Process (multiple) url(s) (host, port, ssl)
if not urls:
return None
else:
for url in urls:
hosts.append( { \
'host':urlparse(url).hostname, \
'port':urlparse(url).port } \
)
if urlparse(url).scheme=='https':
use_ssl = True
verify = bool(self.getConfProperty('opensearch.ssl.verify', False))
username = self.getConfProperty('opensearch.username', 'admin')
password = self.getConfProperty('opensearch.password', 'admin')
auth = (username,password)

client = OpenSearch(
hosts = hosts,
http_compress = False, # enables gzip compression for request bodies
http_auth = auth,
use_ssl = use_ssl,
verify_certs = verify,
ssl_assert_hostname = verify,
ssl_show_warn = False,
)
return client
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def opensearch_query( self, REQUEST=None):
home_id = request.get('home_id', '')
multisite_search = int(request.get('multisite_search', 1))
multisite_exclusions = request.get('multisite_exclusions', '').split(',')
prettify = bool(int(request.get('prettify',0)))
index_names = []
# Search in a specific index given by Request-parameter facet
if request.get('facet') not in ['all','undefined', None, '']:
Expand Down Expand Up @@ -176,7 +177,10 @@ def opensearch_query( self, REQUEST=None):
resp_text = ''
try:
response = client.search(body = json.dumps(query), index = index_names)
resp_text = json.dumps(response)
if prettify:
resp_text = json.dumps(response, indent=4)
else:
resp_text = json.dumps(response)
except opensearchpy.exceptions.RequestError as e:
resp_text = '//%s'%(e.error)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def opensearch_suggest(self, REQUEST=None):
q = request.get('q','Lorem')
qsize = request.get('qsize', 12)
debug = bool(int(request.get('debug',0)))
prettify = bool(int(request.get('prettify',0)))

# Get configured or default fieldsets that are used for extracting suggest terms
fieldsets = get_suggest_fieldsets(self)
Expand All @@ -150,5 +151,8 @@ def opensearch_suggest(self, REQUEST=None):

# Finally sort all terms and return as JSON-textTestBan
suggest_terms = sorted(set(suggest_terms), key=lambda x: x.lower())
resp_text = json.dumps(suggest_terms, indent=2)
if prettify:
resp_text = json.dumps(suggest_terms, indent=4)
else:
resp_text = json.dumps(suggest_terms)
return resp_text
23 changes: 21 additions & 2 deletions Products/zms/zpt/ZMSZCatalogAdapter/manage_zcatalog_connector.zpt
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,33 @@
</tal:block>
</tal:block>
</tal:block>
<div class="form-group row" tal:condition="python:here.getId().split('_')[0]=='opensearch'">
<label class="col-sm-2 control-label" for="zcatalog_test"><span>Performance</span></label>
<div class="col-sm-10">
<div class="input-group" id="zcatalog_test">
<input type="text" class="form-control" id="zcatalog_test_q" name="q"
placeholder="Universität" title="Enter query, e.g. Universität"
tal:attributes="value python:request.get('q','Universität')">
<input type="number" class="form-control" id="zcatalog_test_calls" name="calls:int"
placeholder="10" title="Enter number of repetive calls, e.g. 10"
tal:attributes="value python:request.get('calls','10')">
<div class="input-group-append">
<button class="btn btn-primary" title="Execute Performance Test (JSON shown in new window, Browser-Plugin JSON-Formatter recommended) "
onclick="javascript:window.open('./manage_opensearch_test?prettify=1&calls='+$('#zcatalog_test_calls').val()+'&q='+$('#zcatalog_test_q').val(),'Performance Test','width=800,height=600,scrollbars=yes');return false"
><i class="fas fa-external-link-alt"></i>
</button>
</div>
</div>
</div>
</div><!-- .form-group -->
<div class="form-group row">
<label class="col-sm-2 control-label" for="zcatalog_qsearch"><span>Query</span></label>
<div class="col-sm-10">
<div class="input-group">
<input type="text" class="form-control" id="zcatalog_qsearch" name="q" tal:attributes="value python:request.get('q','')">
<div class="input-group-append">
<button class="btn btn-primary"
tal:attributes="onclick python:'javascript:$ZMI.iframe(\'%s/search_json\',{q:$(\'#zcatalog_qsearch\').val(),debug:0,pretty:1},{id:\'connector_results\',json:true,title:\'Query: <code>\' + $(\'#zcatalog_qsearch\').val() + \'</code>\' });;return false'%(here.absolute_url())"
onclick="javascript:window.open('./search_json?prettify=1&q='+$('#zcatalog_qsearch').val(),'Query Test','width=800,height=600,scrollbars=yes');return false"
><i class="fas fa-external-link-alt"></i>
</button>
</div>
Expand All @@ -231,7 +250,7 @@
<input type="text" class="form-control" id="zcatalog_qsuggest" name="q" tal:attributes="value python:request.get('q','')">
<div class="input-group-append">
<button class="btn btn-primary"
tal:attributes="onclick python:'javascript:$ZMI.iframe(\'%s/suggest_json\',{q:$(\'#zcatalog_qsuggest\').val(),debug:0,pretty:1},{id:\'connector_results\',json:true,title:\'Suggest: <code>\' + $(\'#zcatalog_qsuggest\').val() + \'</code>\' });;return false'%(here.absolute_url())"
onclick="javascript:window.open('./suggest_json?prettify=1&q='+$('#zcatalog_qsuggest').val(),'Suggest','width=800,height=600,scrollbars=yes');return false"
><i class="fas fa-external-link-alt"></i>
</button>
</div>
Expand Down

0 comments on commit 2ed56bb

Please sign in to comment.