Skip to content

Instantly share code, notes, and snippets.

@YoungForest
Last active January 14, 2017 06:58
Show Gist options
  • Save YoungForest/04080ae9ad932aa1fd7211c05e93b197 to your computer and use it in GitHub Desktop.
Save YoungForest/04080ae9ad932aa1fd7211c05e93b197 to your computer and use it in GitHub Desktop.
对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