Skip to content

Instantly share code, notes, and snippets.

@AdamMajer
Created September 24, 2021 11:21
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 AdamMajer/89e1402d26024f5ebddb25d19d2f0890 to your computer and use it in GitHub Desktop.
Save AdamMajer/89e1402d26024f5ebddb25d19d2f0890 to your computer and use it in GitHub Desktop.
TIming test
> ./a.out
done
direct: 239.264000ms
alts: 509.709000ms
overhead: 113.03%
> ./a.out
done
direct: 258.248000ms
alts: 511.959000ms
overhead: 98.24%
> ./a.out
done
direct: 252.697000ms
alts: 510.250000ms
overhead: 101.92%
> ./a.out
done
direct: 244.227000ms
alts: 513.805000ms
overhead: 110.38%
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
/*
assumptions:
> cat /usr/share/libalternatives/libalternatives-unit-test-helper/10.conf
binary=/usr/bin/true
man=true.1
> ln -s /usr/bin/alts libalternatives-unit-test-helper
*/
char *a[] = { "/usr/bin/true", 0 };
char *b[] = { "./libalternatives-unit-test-helper", 0 };
int main()
{
struct timeval t1, t2, t3;
int s;
// warm up
for (int i=0; i<1000; i++){
int pid = fork();
if (pid == 0)
execv(a[0], a);
if (wait(&s) == -1 || s != 0) {
printf("something went wrong\n");
return 0;
}
}
// measure
gettimeofday(&t1, NULL);
for (int i=0; i<1000; i++){
int pid = fork();
if (pid == 0)
execv(a[0], a);
if (wait(&s) == -1 || s != 0) {
printf("something went wrong\n");
return 0;
}
}
gettimeofday(&t2, NULL);
for (int i=0; i<1000; i++){
int pid = fork();
if (pid == 0)
execv(b[0], b);
if (wait(&s) == -1 || s != 0) {
printf("something went wrong\n");
return 0;
}
}
gettimeofday(&t3, NULL);
printf("done\n");
double direct = ((double)t2.tv_sec - t1.tv_sec)*1e3 + ((double)t2.tv_usec - t1.tv_usec)/1e3;
double alts = ((double)t3.tv_sec - t2.tv_sec)*1e3 + ((double)t3.tv_usec - t2.tv_usec)/1e3;
printf("direct: %fms\n", direct);
printf("alts: %fms\n", alts);
printf("overhead: %.2f%%\n", (alts - direct)*100.0/direct);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment