{"id":3219,"date":"2016-09-20T13:18:18","date_gmt":"2016-09-20T17:18:18","guid":{"rendered":"https:\/\/www.redline13.com\/blog\/?page_id=3219"},"modified":"2022-01-17T23:41:52","modified_gmt":"2022-01-18T04:41:52","slug":"custom-load-test-python","status":"publish","type":"page","link":"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/","title":{"rendered":"Custom Load Test Python"},"content":{"rendered":"<p>Custom Tests can be written in\u00a0<a href=\"https:\/\/www.redline13.com\/blog\/writing-a-custom-load-test\/#phptest\">PHP<\/a> or <a href=\"https:\/\/www.redline13.com\/blog\/writing-a-custom-load-test\/#nodetest\">Node.js<\/a>\u00a0or Python.<\/p>\n<h3>Basics<\/h3>\n<ul>\n<li>There are two ways to submit a custom load test\n<ul>\n<li>Submit the Load Test File ( [ANY_NAME].py ) in the language specified<\/li>\n<li>Submit a compressed file (.tar, .tar.gz, .tgz ) with the custom file specifically named\n<ul>\n<li>Python &#8211; custom_test.py<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Custom Load Tests support CSV, Compressed, and Extra files\n<ul>\n<li>CSV files can be split across servers<\/li>\n<li>Compressed files can be marked to be expanded<\/li>\n<li>All files are placed in the root of the load test<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>A language specific load test is<\/p>\n<ul>\n<li>ANY code\u00a0you want to execute<\/li>\n<li>With some redline13 API calls to report test execution and timing<\/li>\n<\/ul>\n<p>Each Language supports\u00a0a packaging mechanism to install more packages<\/p>\n<ul>\n<li>Python &#8211;\u00a0Not available yet,\u00a0waiting for customer direction and feedback<\/li>\n<\/ul>\n<p>A\u00a0Test Harness is available for all languages, including\u00a0examples<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/redline13\/harness-custom-test-python\">Python Harness\u00a0<\/a><\/li>\n<\/ul>\n<h2 id=\"phptest\">Python Test<\/h2>\n<blockquote><p>Special Thanks to <a href=\"https:\/\/github.com\/ddnomad\">Artem\u00a0Fliunt<\/a> for porting\u00a0the test harness fro PHP\/Node to Python allowing us to incorporate into RedLine13 core.<\/p><\/blockquote>\n<p>To write a custom load test in Python, you need to create a single Python file that contains a class that extends class <code>LoadTestingTest<\/code>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from load_testing_test import LoadTestingTest\n\nclass CustomTest(LoadTestingTest):<\/pre>\n<p>There are several reporting functions that are available for your use, to access these \u00a0a few import statements are required<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># Used to import the required functions\nimport record_helpers\n\n# Used to pass through time\nimport time<\/pre>\n<p>The constructor for <code>CustomTest<\/code> will be called with 2 parameters, a test number and a random string, both of which should be passed to the constructor for <code>LoadTestingTest<\/code>. An instance of this class will be started for each user specified in the &#8220;Number of User&#8221; form field.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def __init__(self, test_num, rand):\n    \"\"\"Constructor\n\n    :param test_num: int Test number\n    :param rand: string Random token for test\n    \"\"\"\n\n    # Call parent constructor\n    super(ExampleTest, self).__init__(test_num, rand)<\/pre>\n<p>An instance of this class will be started for each user specified in the &#8220;Number of User&#8221; form field.<\/p>\n<p>You must override the <code>start_test()<\/code> method of <code>LoadTestingTest<\/code>. This function is called to start the load test.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def start_test(self):\n    \"\"\" Start test \"\"\"\n\n<\/pre>\n<p>Extending \u00a0<code>LoadTestingTest<\/code>\u00a0Gives access to a few functions, including access to configuration data.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">    # Example getting information from Config Dict\n    config = self.get_ini_settings()\n    iterations = 5\n    if config.has_key( 'iterations' ):\n         iterations = int(config.get( 'iterations', 1 ))\n<\/pre>\n<p>Here is an example running some custom code within the test and capturing the time for each iteration<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">    for x in range(1, iterations+1):\n        start_time = time.time()\n        # Here is your Code - do anything, measure anything\n        rand_time = random.randint(2, 5)\n        time.sleep(rand_time)\n        diff = time.time() - start_time\n        # Record your measurement and capture if it was success or fail\n        record_helpers.record_load( \"Iteration\", start_time, diff)\n<\/pre>\n<h3>Recording methods\u00a0available<\/h3>\n<ul>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def record_load(name, ts, time, error=False, kb=0):<br \/>\n<\/code>&#8211; This records and aggregates the total load time for any activity marked as &#8216;name&#8217;, with <code>ts<\/code> being the UNIX timestamp when the request started and <code>time<\/code> being the time to complete the request. This function is used in the UI to report the per page average response time, but does not affect the overall response time that is calculated. The go_to_url and <code>loadResources<\/code> methods of <code>LoadTestingSession<\/code> calls this for you.<\/p>\n<ul>\n<li>name &#8211; string representation of the end point, does not have to be a URL can be any string<\/li>\n<li>ts &#8211; can be specified via time.time()<\/li>\n<li>time &#8211; is an integer<\/li>\n<li>error &#8211; boolean to record if your\u00a0result is fail or success<\/li>\n<li>kb &#8211; Size of response.<\/li>\n<\/ul>\n<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def record_error(error,ts):<\/code>\u00a0&#8211; This records an error that will be displayed in the UI. \u00a0If you are recording consider recording also with record_load\u00a0to show the error count and time errors created.\n<ul>\n<li>Can be used to record general errors<\/li>\n<li>error &#8211; must be a string<\/li>\n<li>ts &#8211; can be specified via time.time()<\/li>\n<\/ul>\n<\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def record_progress(test_num, percent):<\/code>\u00a0&#8211; Optionally records the progress (between 0 and 100) of a single test. This allows you to enable more accurate progress than the default, which is based only on the number completed vs. the test size. \u00a0Not a required API call.<\/li>\n<\/ul>\n<p>If a test fails, you can raise an Exception and the test will be marked as a failure and recorded as an error for the user being simulated.<\/p>\n<h3>Tips<\/h3>\n<ul>\n<li>To simulate the iterations parameter of a simple test, use a loop around your code within start_test().<\/li>\n<li>You can use the random string that is passed in as the second parameter to your <code>CustomTest<\/code> constructor to append to URLs. This can help you to easily identify load testing requests in your server logs.<\/li>\n<li>If you need additional resources for your test, you can download a file (e.g. a .tgz) with the resources in the <code>CustomTest<\/code> constructor. You should include code to ensure that only one test per server downloads the files. If you know the number of tests per server, you can use the test number to decide which test should download the files. Alternatively, you can attempt to create a lock file, with the first process successfully creating the lock file doing the file download.<\/li>\n<\/ul>\n<h3 id=\"nodetest\">Examples<\/h3>\n<ul>\n<li>CustomLoadTest, <a href=\"https:\/\/github.com\/redline13\/harness-custom-test-python\/blob\/master\/custom_load_test.py\">custom_load_test.py<\/a>\n<ul>\n<li>Simple URL which does echo<\/li>\n<\/ul>\n<\/li>\n<li>ExampleTest, <a href=\"https:\/\/github.com\/redline13\/harness-custom-test-python\/blob\/master\/example_test.py\">example_test.py<\/a>\n<ul>\n<li>Loops 100 times with random wait, each loop is a recorded as URL and overall a PAGE<\/li>\n<\/ul>\n<\/li>\n<li>FormLoadTest, <a href=\"https:\/\/github.com\/redline13\/harness-custom-test-python\/blob\/master\/form_load_test.py\">form_load_test.py<\/a>\n<ul>\n<li>Gets data from a webpage, finds a form, and submits that form with data<\/li>\n<\/ul>\n<\/li>\n<li>LoginLoadTest, <a href=\"https:\/\/github.com\/redline13\/harness-custom-test-python\/blob\/master\/login_load_test.py\">login_load_test.py<\/a>\n<ul>\n<li>Submits a login form (to use see code, need to add in data)<\/li>\n<\/ul>\n<\/li>\n<li>SimpleLoadTest, <a href=\"https:\/\/github.com\/redline13\/harness-custom-test-python\/blob\/master\/simple_load_test.py\">simple_load_test.py<\/a>\n<ul>\n<li>Hits a URL, parses for other endpoints (JS, CSS) and times those as well<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Custom Tests can be written in\u00a0PHP or Node.js\u00a0or Python. Basics There are two ways to submit a custom load test Submit the Load Test File ( [ANY_NAME].py ) in the language specified Submit a compressed file (.tar, .tar.gz, .tgz ) with the custom file specifically named Python &#8211; custom_test.py Custom Load Tests support CSV, Compressed, and Extra files CSV files can be split across servers Compressed files can be marked to be expanded All files are<a class=\"more-link\" href=\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":3223,"parent":0,"menu_order":11,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":{"0":"entry","1":"page","2":"publish","3":"author-user","4":"post-3219","6":"has-post-thumbnail"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Custom Load Test Python - RedLine13<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Load Test Python - RedLine13\" \/>\n<meta property=\"og:description\" content=\"Custom Tests can be written in\u00a0PHP or Node.js\u00a0or Python. Basics There are two ways to submit a custom load test Submit the Load Test File ( [ANY_NAME].py ) in the language specified Submit a compressed file (.tar, .tar.gz, .tgz ) with the custom file specifically named Python &#8211; custom_test.py Custom Load Tests support CSV, Compressed, and Extra files CSV files can be split across servers Compressed files can be marked to be expanded All files areRead More &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/\" \/>\n<meta property=\"og:site_name\" content=\"RedLine13\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-18T04:41:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2016\/09\/python-long-logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"580\" \/>\n\t<meta property=\"og:image:height\" content=\"164\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/\",\"name\":\"Custom Load Test Python - RedLine13\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\"},\"datePublished\":\"2016-09-20T17:18:18+00:00\",\"dateModified\":\"2022-01-18T04:41:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.redline13.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Custom Load Test Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"name\":\"RedLine13\",\"description\":\"(Almost) Free Load Testing in the Cloud\",\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.redline13.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\",\"name\":\"RedLine13\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"contentUrl\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"width\":300,\"height\":68,\"caption\":\"RedLine13\"},\"image\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Custom Load Test Python - RedLine13","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/","og_locale":"en_US","og_type":"article","og_title":"Custom Load Test Python - RedLine13","og_description":"Custom Tests can be written in\u00a0PHP or Node.js\u00a0or Python. Basics There are two ways to submit a custom load test Submit the Load Test File ( [ANY_NAME].py ) in the language specified Submit a compressed file (.tar, .tar.gz, .tgz ) with the custom file specifically named Python &#8211; custom_test.py Custom Load Tests support CSV, Compressed, and Extra files CSV files can be split across servers Compressed files can be marked to be expanded All files areRead More &rarr;","og_url":"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/","og_site_name":"RedLine13","article_modified_time":"2022-01-18T04:41:52+00:00","og_image":[{"width":580,"height":164,"url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2016\/09\/python-long-logo.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/","url":"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/","name":"Custom Load Test Python - RedLine13","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/#website"},"datePublished":"2016-09-20T17:18:18+00:00","dateModified":"2022-01-18T04:41:52+00:00","breadcrumb":{"@id":"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.redline13.com\/blog\/custom-load-test-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.redline13.com\/blog\/custom-load-test-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.redline13.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Custom Load Test Python"}]},{"@type":"WebSite","@id":"https:\/\/www.redline13.com\/blog\/#website","url":"https:\/\/www.redline13.com\/blog\/","name":"RedLine13","description":"(Almost) Free Load Testing in the Cloud","publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.redline13.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.redline13.com\/blog\/#organization","name":"RedLine13","url":"https:\/\/www.redline13.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","contentUrl":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","width":300,"height":68,"caption":"RedLine13"},"image":{"@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/pages\/3219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/comments?post=3219"}],"version-history":[{"count":0,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/pages\/3219\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media\/3223"}],"wp:attachment":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media?parent=3219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}