Skip to content

Instantly share code, notes, and snippets.

@larsch
Created September 9, 2018 10:23

Revisions

  1. larsch created this gist Sep 9, 2018.
    55 changes: 55 additions & 0 deletions termbench.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #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;
    }