DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Ultimate Chaos Testing Guide
  • Chaos Engineering for Microservices
  • Shift-Right Testing: Smart Automation Through AI and Observability
  • Cost-Aware Resilience: Implementing Chaos Engineering Without Breaking the Budget

Trending

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • Internal Developer Portals: Modern DevOps's Missing Piece
  • My LLM Journey as a Software Engineer Exploring a New Domain
  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Chaos Engineering: Simulating CPU Spikes

Chaos Engineering: Simulating CPU Spikes

Let's discuss how to simulate CPU consumption to spike up to 100% on a host (or container). CPU consumption will spike up whenever a thread goes on an infinite loop.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Mar. 16, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.9K Views

Join the DZone community and get the full member experience.

Join For Free

In this series of chaos engineering articles, let's discuss how to simulate CPU consumption to spike up to 100% on a host (or container). CPU consumption will spike up whenever a thread goes on an infinite loop. Here is a sample program from the open-source BuggyApp application, which would cause the CPU to spike up.

public class CPUSpikeDemo {   

  public static void start() {    
     new CPUSpikerThread().start();    
     new CPUSpikerThread().start();    
     new CPUSpikerThread().start();    
     new CPUSpikerThread().start();    
     new CPUSpikerThread().start();    
     new CPUSpikerThread().start();    
    System.out.println("6 threads launched!");  
 } 
}
 public class CPUSpikerThread extends Thread {   
  
  @Override  
  public void run() {             

    while (true) {                  

    // Just looping infinitely    
   }  
  } 
}


In the above Java program, you will notice the 'CPUSpikeDemo' class. In this class, 6 threads with the name 'CPUSpikerThread' are launched. If you notice the 'CPUSpikerThread' class code, there is a 'while (true)' loop without any code in it. This condition will cause the thread to go on an infinite loop. Since 6 threads are executing this code, all the 6 threads will go on an infinite loop. When this program is executed, CPU consumption will skyrocket on the machine.

We launched the above BuggyApp program on a 't3a.medium' EC2 instance, which has 2 CPUs. Below is the output from the UNIX performance monitoring tool 'top'. You can notice the overall CPU % reaching up to 100%.

Fig: Top tool showing CPU consumption spiking up to 100%

Fig: Top tool showing CPU consumption spiking up to 100%.

How to Diagnose CPU Spikes?

As highlighted in this article, you can use a manual approach to do root cause analysis:

  1. Capture thread dump from the application.
  2. Capture 'top -H -p {PID}' output.
  3. Marry these #a and #b and identify the root cause of the CPU spike problem.

On the other hand, you can use automated root cause analysis tool like yCrash — which automatically captures application-level data (thread dump, heap dump, Garbage Collection log), system-level data (netstat, vmstat, iostat, top, top -H, dmesg, …) and marries these two datasets to generate instant root cause analysis report instantly. Below is the report generated by the yCrash tool when the above sample program is executed:


List of Threads causing CPU Spikes

CPU | Memory Graph

Details of Code causing CPU Spikes

Fig: yCrash tool point outlines of code causing the CPU spike.

From the report, you can observe the yCrash is pointing out that 6 threads are causing the CPU to spike up. In the 'CPU | Memory' section of this report, you will notice that the CPU consumption of each thread (which is > 30%) is being reported. You can also notice that tool is pointing out exact lines of code, i.e., com.buggyapp.cpuspike.CPUSpikerThread.run(CPUSpikerThread.java:12) that are causing the infinite loop. Equipped with this information, one can easily go ahead and fix the problematic code.

Spike (software development) Chaos engineering

Opinions expressed by DZone contributors are their own.

Related

  • The Ultimate Chaos Testing Guide
  • Chaos Engineering for Microservices
  • Shift-Right Testing: Smart Automation Through AI and Observability
  • Cost-Aware Resilience: Implementing Chaos Engineering Without Breaking the Budget

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!