Skip to content

Instantly share code, notes, and snippets.

@larsch
Created September 9, 2018 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsch/89dff59c12cf5590d044e27ff2ac5a60 to your computer and use it in GitHub Desktop.
Save larsch/89dff59c12cf5590d044e27ff2ac5a60 to your computer and use it in GitHub Desktop.
Basic terminal benchmark program
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
void writestr(int fd, const char* str) {
write(fd, str, strlen(str));
}
#define LINES_PER_WRITE 128
#define LINE_BUFFER 80
#define TOTAL_BUFFER (LINE_BUFFER*LINES_PER_WRITE)
int main() {
char str[LINES_PER_WRITE];
int fd = fileno(stdout);
memset(str, 'x', LINE_BUFFER);
str[LINE_BUFFER-1] = '\n';
char* buffer = malloc(TOTAL_BUFFER);
for (int i = 0; i < LINES_PER_WRITE; ++i)
memcpy(buffer + i * LINE_BUFFER, str, LINE_BUFFER);
int n = 128;
for (int i = 0; i < 999; ++i) {
struct timespec before;
struct timespec after;
clock_gettime(CLOCK_MONOTONIC, &before);
writestr(fd, "\x1b[?1049h");
for (int i = 0; i < n; ++i) {
write(fd, buffer, TOTAL_BUFFER);
}
fsync(fd);
clock_gettime(CLOCK_MONOTONIC, &after);
writestr(fd, "\x1b[?1049l");
uint64_t delta_secs = after.tv_sec - before.tv_sec;
int64_t delta_nsecs = after.tv_nsec - before.tv_nsec;
if (delta_nsecs < 0) {
delta_nsecs += 1000000000;
delta_secs -= 1;
}
delta_nsecs += delta_secs * 1000000000;
int lines_written = LINES_PER_WRITE * n;
printf("lines=%d nsecs=l%ld lines/sec=%llu\n", n, delta_nsecs, (lines_written * 1000000000ull) / delta_nsecs);
if (delta_nsecs > 1000000000)
break;
n = (n * 1050000000ull) / delta_nsecs;
}
free(buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment