Created
April 11, 2013 18:52
-
-
Save plaa/5366129 to your computer and use it in GitHub Desktop.
Short program for generating load to test AWS micro instances
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 <unistd.h> | |
#include <sys/time.h> | |
#include <math.h> | |
long int getMicroseconds(); | |
int main(int argc, char **argv) { | |
int percentage; | |
int dutycycle; | |
int activetime; | |
int testduration = 1000000000; | |
long int nextcycle; | |
long int idle; | |
long int starttime; | |
long int now; | |
long int t = 0; | |
int i; | |
int n=0; | |
double d; | |
double result = 0; | |
if (argc != 3 && argc != 4) { | |
printf("Usage: %s <CPU utilization 0...100%%> <duty cycle ms> [test time s]\n", argv[0]); | |
printf("Example: %s 50 10 180 # 50%% utilization at 10 ms duty cycle for 3 mins\n", argv[0]); | |
exit(1); | |
} | |
percentage = atoi(argv[1]); | |
dutycycle = atoi(argv[2]) * 1000; | |
activetime = (percentage * dutycycle)/100; | |
if (argc >= 4) { | |
testduration = atoi(argv[3]) * 1000000; | |
} | |
starttime = getMicroseconds(); | |
nextcycle = getMicroseconds() + dutycycle; | |
while (t < testduration) { | |
// Active time | |
idle = getMicroseconds() + activetime; | |
n = 0; | |
d = 100; | |
while (getMicroseconds() < idle) { | |
for (i=0; i<10000; i++) { | |
d = sqrt(d) + 100; | |
} | |
n++; | |
} | |
result += d; | |
// Idle time | |
now = getMicroseconds(); | |
if (now < nextcycle) { | |
while (getMicroseconds() < nextcycle) { | |
usleep(nextcycle - getMicroseconds()); | |
} | |
nextcycle = nextcycle + dutycycle; | |
} else { | |
printf("Missed next cycle by %d us\n", (int)(now - nextcycle)); | |
nextcycle = getMicroseconds() + dutycycle; | |
} | |
// Report result | |
t = getMicroseconds() - starttime; | |
printf("t = %d ms: %d rounds\n", (int)(t/1000), n); | |
fflush(stdout); | |
} | |
// Ensure the math operations cannot be optimized away | |
if (d < 0) { | |
printf("%.f\n", d); | |
} | |
return 0; | |
} | |
long int getMicroseconds() { | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
return ((long int)tv.tv_sec)*1000000 + tv.tv_usec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment