From 4bde28bc3f539c647b50d0f629dc6e68198af9f7 Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Tue, 13 Mar 2012 18:48:30 -0400 Subject: [PATCH] Add benchmark example (stress test for prediction overlay) --- src/examples/Makefile.am | 13 +++++- src/examples/benchmark.cc | 96 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/examples/benchmark.cc diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am index 564a4fd..e11cfa6 100644 --- a/src/examples/Makefile.am +++ b/src/examples/Makefile.am @@ -1,7 +1,7 @@ AM_CPPFLAGS = $(protobuf_CFLAGS) AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe -noinst_PROGRAMS = encrypt decrypt ntester parse termemu +noinst_PROGRAMS = encrypt decrypt ntester parse termemu benchmark encrypt_SOURCES = encrypt.cc encrypt_CPPFLAGS = -I$(srcdir)/../crypto @@ -29,3 +29,14 @@ endif ntester_SOURCES = ntester.cc ntester_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I$(srcdir)/../network -I$(srcdir)/../crypto -I../protobufs $(BOOST_CPPFLAGS) ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../protobufs/libmoshprotos.a -lutil -lm $(BOOST_LDFLAGS) $(protobuf_LIBS) + +benchmark_SOURCES = benchmark.cc +benchmark_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I../protobufs -I$(srcdir)/../frontend -I$(srcdir)/../crypto $(BOOST_CPPFLAGS) -I$(srcdir)/../network +benchmark_LDADD = ../frontend/terminaloverlay.o ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../protobufs/libmoshprotos.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a -lutil -lm $(BOOST_LDFLAGS) $(protobuf_LIBS) +if COND_THIRD_LIBSTDDJB + benchmark_CPPFLAGS += -I$(top_srcdir)/third/libstddjb + benchmark_LDADD += $(top_builddir)/third/libstddjb/libstddjb.a +else + benchmark_CPPFLAGS += $(STDDJB_CPPFLAGS) + benchmark_LDADD += $(STDDJB_LDFLAGS) +endif diff --git a/src/examples/benchmark.cc b/src/examples/benchmark.cc new file mode 100644 index 0000000..7946a7d --- /dev/null +++ b/src/examples/benchmark.cc @@ -0,0 +1,96 @@ +/* + 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 . +*/ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if HAVE_PTY_H +#include +#elif HAVE_UTIL_H +#include +#endif + +extern "C" { +#include "selfpipe.h" +} + +#include "swrite.h" +#include "completeterminal.h" +#include "user.h" +#include "terminaloverlay.h" + +const int ITERATIONS = 100000; + +int main( void ) +{ + Terminal::Framebuffer local_framebuffer( 80, 24 ); + Overlay::OverlayManager overlays; + Terminal::Display display( true ); + Terminal::Complete local_terminal( 80, 24 ); + + /* Adopt native locale */ + if ( NULL == setlocale( LC_ALL, "" ) ) { + perror( "setlocale" ); + exit( 1 ); + } + + /* Verify locale calls for UTF-8 */ + if ( strcmp( nl_langinfo( CODESET ), "UTF-8" ) != 0 ) { + fprintf( stderr, "mosh requires a UTF-8 locale.\n" ); + exit( 1 ); + } + + for ( int i = 0; i < ITERATIONS; i++ ) { + /* type a character */ + overlays.get_prediction_engine().new_user_byte( 'x', local_framebuffer ); + + /* fetch target state */ + Terminal::Framebuffer new_state( local_terminal.get_fb() ); + + /* apply local overlays */ + overlays.apply( new_state ); + + /* calculate minimal difference from where we are */ + const string diff( display.new_frame( false, + local_framebuffer, + new_state ) ); + + /* make sure to use diff */ + if ( diff.size() > INT_MAX ) { + exit( 1 ); + } + + local_framebuffer = new_state; + } + + return 0; +}