Guest User

X font rendering performance measurer

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