Skip to content

Instantly share code, notes, and snippets.

@plaa
Created April 11, 2013 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plaa/5366129 to your computer and use it in GitHub Desktop.
Save plaa/5366129 to your computer and use it in GitHub Desktop.
Short program for generating load to test AWS micro instances
#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