summaryrefslogtreecommitdiffstats
path: root/src/examples/benchmark.cc
blob: a52797afaf420a379196c420b996cf91b49dd89c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
    Mosh: the mobile shell
    Copyright 2012 Keith Winstein

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations including
    the two.

    You must obey the GNU General Public License in all respects for all
    of the code used other than OpenSSL. If you modify file(s) with this
    exception, you may extend this exception to your version of the
    file(s), but you are not obligated to do so. If you do not wish to do
    so, delete this exception statement from your version. If you delete
    this exception statement from all source files in the program, then
    also delete it here.
*/

#include "src/include/config.h"

#include <errno.h>
#include <locale.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <pwd.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
#include <exception>

#if HAVE_PTY_H
#include <pty.h>
#elif HAVE_UTIL_H
#include <util.h>
#endif

#include "src/util/swrite.h"
#include "src/statesync/completeterminal.h"
#include "src/statesync/user.h"
#include "src/frontend/terminaloverlay.h"
#include "src/util/locale_utils.h"
#include "src/util/fatal_assert.h"

const int ITERATIONS = 100000;

using namespace Terminal;

int main( int argc, char **argv )
{
  try {
    int fbmod = 0;
    int width = 80, height = 24;
    int iterations = ITERATIONS;
    if (argc > 1) {
      iterations = atoi(argv[1]);
      if (iterations < 1 || iterations > 1000000000) {
	fprintf(stderr, "bogus iteration count\n");
	exit(1);
      }
    }
    if (argc > 3) {
      width = atoi(argv[2]);
      height = atoi(argv[3]);
      if (width < 1 || width > 1000 || height < 1 || height > 1000) {
	fprintf(stderr, "bogus window size\n");
	exit(1);
      }
    }
    Framebuffer local_framebuffers[ 2 ] = { Framebuffer(width,height), Framebuffer(width,height) };
    Framebuffer *local_framebuffer = &(local_framebuffers[ fbmod ]);
    Framebuffer *new_state = &(local_framebuffers[ !fbmod ]);
    Overlay::OverlayManager overlays;
    Display display( true );
    Complete local_terminal( width, height );

    /* Adopt native locale */
    set_native_locale();
    fatal_assert( is_utf8_locale() );

    for ( int i = 0; i < iterations; i++ ) {
      /* type a character */
      overlays.get_prediction_engine().new_user_byte( i + 'x', *local_framebuffer );

      /* fetch target state */
      *new_state = local_terminal.get_fb();

      /* apply local overlays */
      overlays.apply( *new_state );

      /* calculate minimal difference from where we are */
      const std::string diff( display.new_frame( false,
						 *local_framebuffer,
						 *new_state ) );

      /* make sure to use diff */
      if ( diff.size() > INT_MAX ) {
	exit( 1 );
      }

      fbmod = !fbmod;
      local_framebuffer = &(local_framebuffers[ fbmod ]);
      new_state = &(local_framebuffers[ !fbmod ]);
    }
  } catch ( const std::exception &e ) {
    fprintf( stderr, "Exception caught: %s\n", e.what() );
    return 1;
  }
  return 0;
}