Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
对CPU进行压力测试的小程序
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sched.h>
#define PIECE 1
unsigned GetTickCount()
{
struct timeval tv;
if(gettimeofday(&tv, NULL) != 0)
return 0;
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
int main(int argc, char * argv[])
{
unsigned starttime = 0;
if (argc != 2)
{
printf("usuage: %s stress.(0 < stress < 100)\n", argv[0]);
return 0;
}
int stress = atoi(argv[1]);
if (stress < 0)
stress = 0;
stress += (stress/40); // 补偿
if (stress > 100)
stress = 100;
while(1)
{
starttime = GetTickCount();
while((GetTickCount() - starttime) <= stress * PIECE)
;
usleep((100 - stress)*PIECE * 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.