{"id":12046,"date":"2023-03-21T09:19:32","date_gmt":"2023-03-21T13:19:32","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=12046"},"modified":"2024-10-28T09:17:36","modified_gmt":"2024-10-28T13:17:36","slug":"generate-cpu-load","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/generate-cpu-load\/","title":{"rendered":"Generate CPU load using Python"},"content":{"rendered":"<p><strong>Problem:<\/strong> How do I spike up my CPU load?<\/p>\n<p>Recently at one of our client engagement, for one of our tests, we needed to spike up CPU usage in a controlled manner on some of our Unix servers. One way to do this was to generate a specific amount of load for a specific amount of time. This was a quick and dirty way of producing an approximate load on the CPU. This solution assumes that the CPU load from all other background processes are negligible. We came up with a simple Python script to do this and thought of sharing it with people who would be looking at something similar.<\/p>\n<hr>\n<h3>Solution<\/h3>\n<p>To generate a specific amount of load we will perform an arithmetic operation for that fraction of a second and then sleep for the rest. So if you want 20% utilization, the script would run the arithmetic operation for 0.2 seconds and then sleep for 0.8 seconds. Since most servers we use have multiple cores, we will automatically get the number of CPU cores and use Python&#8217;s multiprocessing module to generate load on all the cores in parallel.<\/p>\n<pre lang=\"python\">\r\n\"\"\"\r\nScript to generate a specific amount of load for a specific amount of time.\r\nWe will check the number of CPU cores and using multiprocessing run the generate load function on all the cores in parallel\r\nTo generate a load, we will perform an arithmetic operation for a fraction of a second and then sleep for the rest\r\nSo if you want 20% utilization, the script would run the arithmetic operation for 0.2 seconds and then sleep for 0.8 seconds.\r\n\"\"\"\r\nimport time\r\nimport math \r\nimport sys \r\nimport multiprocessing\r\n\r\ndef generate_cpu_load(interval=int(sys.argv[1]),utilization=int(sys.argv[2])):\r\n    \"Generate a utilization % for a duration of interval seconds\"\r\n    start_time = time.time()\r\n    for i in range(0,int(interval)):\r\n        print(\"About to do some arithmetic\")\r\n        while time.time()-start_time < utilization\/100.0:\r\n            a = math.sqrt(64*64*64*64*64)\r\n        print(str(i) + \". About to sleep\")\r\n        time.sleep(1-utilization\/100.0)\r\n        start_time += 1\r\n\r\n#----START OF SCRIPT\r\nif __name__=='__main__':\r\n    print(\"No of cpu:\", multiprocessing.cpu_count())\r\n    if len(sys.argv)>2:\r\n        processes = []\r\n        for _ in range (multiprocessing.cpu_count()):\r\n            p = multiprocessing.Process(target =generate_cpu_load)\r\n            p.start()\r\n            processes.append(p)\r\n        for process in processes:\r\n            process.join()        \r\n    else:\r\n        print(\"Usage:\\n python %s interval utilization\"%__file__)\r\n    \r\n<\/pre>\n<h3>Running the Script<\/h3>\n<p>To run the script you need to pass the time interval you want to run the script for and the percentage of CPU utilization you want to achieve.<br \/>\nEg: In case you want to increase your CPU utilization for 30 seconds by 30%, you can run the script as below<\/p>\n<pre lang=\"python\">\r\npython generate_cpu_load.py 30 30\r\n<\/pre>\n<p>That was a quick way to increase your CPU usage. As a bonus, you also figured out how to use <a href=\"https:\/\/docs.python.org\/2\/library\/multiprocessing.html\">python multiprocessing<\/a>. Spike up your knowledge and enjoy testing.<\/p>\n<hr>\n<h3>Technical testing for startups by Qxf2<\/h3>\n<p>Startups face unique QA challenges, and Qxf2 is equipped to solve them. We offer everything from fractional QA to specialized roles, ensuring startups get what they need, when they need it. Learn more about our <a href=\"https:\/\/qxf2.com\/?utm_source=cpu-load&#038;utm_medium=click&#038;utm_campaign=From%20blog\">fractional QA services for startups<\/a> that provide flexibility and expertise.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>Problem: How do I spike up my CPU load? Recently at one of our client engagement, for one of our tests, we needed to spike up CPU usage in a controlled manner on some of our Unix servers. One way to do this was to generate a specific amount of load for a specific amount of time. This was a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211,18],"tags":[],"class_list":["post-12046","post","type-post","status-publish","format-standard","hentry","category-multiprocessing","category-python"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=12046"}],"version-history":[{"count":16,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12046\/revisions"}],"predecessor-version":[{"id":22990,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12046\/revisions\/22990"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=12046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=12046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=12046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}