在Java中生成CPU负载 [英] generate CPU load in Java

查看:136
本文介绍了在Java中生成CPU负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行一些吞吐量测试。我的应用程序必须


  1. 从JMS中读取

  2. 做一些处理

  3. 写入JMS

我的目标是模拟#2,一些处理。也就是说,在转发事件之前,给定时间(例如500ms)引入延迟并占用CPU。



天真的方法是了Thread.sleep(500)。这将引入正确的执行延迟,但不会执行CPU。



计算斐波纳契数是一个选项。
有没有人使用任何有趣的技术来保持CPU在一段时间内忙?



理想的特点是:




  • 执行各种说明,而不是(例如)循环旋转


  • 有一个简单的方法来调整处理周期的上升或下降(完成时间将根据硬件明显变化)


解决方案

通过调用Cipher.update()加密一个字符串(循环)。根据定义,加密算法很难优化。唯一的问题是您需要执行一些不寻常的安装。我将这个答案标记为社区wiki,以便最近写的人可以填写。


I am conducting some throughput testing. My application has to

  1. read from JMS
  2. do some processing
  3. write to JMS

My goal here is to simulate #2, 'some processing'. That is, introduce a delay and occupy the CPU for a given time (e.g. 500ms) before forwarding the event.

The naive approach would be to Thread.sleep(500). This would introduce the right delay in execution, but would not exercise the CPU.

Calculating Fibonacci numbers is one option. Has anyone used any interesting techniques just to keep CPU(s) busy for a given time?

Ideal characteristics would be:

  • Performs a variety of instructions, rather than (for example) just spinning on a loop
  • Not something the HotSpot VM is going to optimise away to nothing
  • Has an easy way to adjust the processing period up or down (time to complete will clearly vary given the hardware)

解决方案

Encrypt a string (in a loop) by calling Cipher.update(). Encryption algorithms are by definition very difficult to optimize. The only problem is that there is some non-trivial setup you need to perform. I'm marking this answer as community wiki, so that somebody who's written it recently can fill it in.

这篇关于在Java中生成CPU负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆