Guest User

X font rendering performance measurer

a guest
Feb 4th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. // https://bbs.archlinux.org/viewtopic.php?id=135062
  2.  
  3. #define _GNU_SOURCE
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/time.h>
  10. #include <sys/select.h>
  11. #include <termios.h>
  12.  
  13. static struct termios orig_termios;
  14.  
  15. void tty_raw(void)
  16. {
  17.     struct termios raw;
  18.     raw = orig_termios;
  19.     raw.c_lflag &= ~(ECHO | ICANON);
  20.     raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0;
  21.     if (tcsetattr(0, TCSAFLUSH, &raw) < 0) exit(1);
  22. }
  23.  
  24. void tty_reset(void)
  25. {
  26.     tcsetattr(0, TCSAFLUSH, &orig_termios);
  27. }
  28.  
  29. char str[2000] = "abcdefghijklmnopqrstuvwxyz 0123456789 ~!@#$%^&*():";
  30. char buf[64];
  31.  
  32. void usage(void)
  33. {
  34.     puts("");
  35.     puts("usage: hold enter until you get the results back (it should take less than 10 seconds)");
  36.     puts("recommendation: run 'xset r rate 300 40' before starting in order to compare with my results");
  37.     puts("");
  38.     puts("press and hold enter to start");
  39.     read(1, buf, 64);
  40.     read(1, buf, 64);
  41. }
  42.  
  43. int rows;
  44.  
  45. int get_micros_in_x(int print_string)
  46. {
  47.     int i;
  48.     struct timeval start, end, delta;
  49.     int micros = 0;
  50.     for (i = 0; i < 3*rows; ++i) {
  51.         // Not an exact science but seems to correlate a tiny bit
  52.         gettimeofday(&start, NULL);
  53.         read(1, buf, 64);
  54.         gettimeofday(&end, NULL);
  55.         timersub(&end, &start, &delta);
  56.         micros += delta.tv_sec*1000000 + delta.tv_usec;
  57.         if (buf[0] == 27) {
  58.             exit(0);
  59.         }
  60.         if (print_string)
  61.             printf(" %s\r\n", strfry(str));
  62.         else
  63.             printf(" .\n");
  64.         // This shouldn't matter, right? RIGHT???
  65.         usleep(40000);
  66.     }
  67.     return micros;
  68. }
  69.  
  70. int main(void)
  71. {
  72.     struct winsize w;
  73.     struct timeval tv;
  74.     fd_set set;
  75.  
  76.     if (!isatty(0)) exit(1);
  77.     if (tcgetattr(0, &orig_termios) < 0) exit(3);
  78.     tty_raw();
  79.     atexit(tty_reset);
  80.  
  81.     ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  82.     rows = w.ws_row;
  83.  
  84.     while (strlen(str) < 1000) memcpy(str+strlen(str), str, strlen(str));
  85.     str[w.ws_col - 2] = 0;
  86.  
  87.     usage();
  88.  
  89.     write(1, "\e[H", 3);
  90.     int slow = get_micros_in_x(1);
  91.     write(1, "\e[H", 3);
  92.     int fast = get_micros_in_x(0);
  93.  
  94.     printf("%3d.%06d seconds have elapsed in the printing setup\n", slow/1000000, slow%1000000);
  95.     printf("%3d.%06d seconds have elapsed in the empty setup\n", fast/1000000, fast%1000000);
  96.     puts("you can release enter now");
  97.  
  98.     // wait until the user releases enter
  99.     FD_ZERO(&set);
  100.     FD_SET(0, &set);
  101.     tv.tv_sec = 0;
  102.     tv.tv_usec = 250000;
  103.     while (select(1, &set, NULL, NULL, &tv) == 1) {
  104.         read(1, buf, 64);
  105.         tv.tv_sec = 0;
  106.         tv.tv_usec = 250000;
  107.     }
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment