Last active
January 14, 2017 06:58
-
-
Save YoungForest/04080ae9ad932aa1fd7211c05e93b197 to your computer and use it in GitHub Desktop.
对CPU进行压力测试的小程序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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