Search code examples
pythonpandasamazon-web-servicesopensearchamazon-opensearch

OpenSearch Getting Run Execution Times


I am doing research on AWS OpenSearch and one of the things I'm trying to measure is run times or execution times for different queries andindex commands. For example how long does it take to perform an action such as a query search, create index, delete index?

Right now I am using the awswrangler Python library for interacting with OpenSearch.API for that library here

Read Index Code I currently have:

awswrangler.opensearch.search(client=self.client, index="index_name", search_body=any_dsl_query,size=100)

awswrangler.opensearch.search_by_sql(client=self.client, sql_query="SELECT * from index_name limit 100")

Delete Index Code:

awswrangler.opensearch.delete_index(client=self.client, index="index_name")

Create Index Code (this one actually returns Elapsed time as desired):

awswrangler.opensearch.index_csv(client=self.client, path=csv_file_path, index="index_name")

Unfortunately none of these except Create Index return the runtime out of the box.

I know that I can create my own timer script to get the runtime but I don't want to do this client side, because then that would include my network, latency in the execution time. Is there anyway to do this with OpenSearch?

I couldn't find a way in the awswrangler python library I was using, or with any other method so far.


Solution

  • I was able to resolve this by using the python requests library and looking at the "took" value in the response, which is the time it took to run the query in ms. Here is the code I used to get this working:

    sample_sql_query = "SELECT * FROM  <index_name> LIMIT 5"
    
    sql_result = requests.post("<opensearch_domain>/_plugins/_sql?format=json", auth=(username, password), data=json.dumps({"query":sample_sql_query}), headers=headers).json()
    
    print (sql_result)