hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 1 | #!/usr/bin/python2.7 |
| 2 | |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 3 | import os |
| 4 | import sys |
onqtam | 778e94b | 2017-05-10 23:19:50 +0300 | [diff] [blame] | 5 | import pprint |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 6 | import argparse |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 7 | import urllib |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 8 | from datetime import datetime |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 9 | import shutil |
| 10 | from time import sleep |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 11 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 12 | # ============================================================================== |
| 13 | # == ARGUMENTS ================================================================= |
| 14 | # ============================================================================== |
onqtam | c6555cf | 2016-09-20 15:07:28 +0300 | [diff] [blame] | 15 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 16 | def addCommonFlags(parser): |
onqtam | 7d8ccec | 2017-05-11 19:31:15 +0300 | [diff] [blame] | 17 | parser.add_argument("compiler", choices=['msvc', 'gcc', 'clang'], default='msvc', help = "compiler to use") |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 18 | parser.add_argument("--debug", action = "store_true", help = "build in debug") |
onqtam | dc92a3c | 2017-05-11 00:08:50 +0300 | [diff] [blame] | 19 | parser.add_argument("--catch", action = "store_true", help = "use Catch instead of doctest") |
onqtam | 87e80e5 | 2017-09-07 12:50:53 +0300 | [diff] [blame^] | 20 | parser.add_argument("--disabled", action = "store_true", help = "DOCTEST_CONFIG_DISABLE / CATCH_CONFIG_DISABLE") |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 21 | parser.add_argument("--fast", action = "store_true", help = "define the doctest/Catch fast config identifier") |
onqtam | 778e94b | 2017-05-10 23:19:50 +0300 | [diff] [blame] | 22 | parser.add_argument("--files", type=int, default=1, help = "number of source files (besides the implementation)") |
| 23 | parser.add_argument("--tests", type=int, default=1, help = "number of test cases per source file") |
| 24 | parser.add_argument("--checks", type=int, default=1, help = "number of asserts per test case") |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 25 | parser.add_argument("--asserts", choices=['normal', 'binary', 'fast'], default="normal", |
| 26 | help = "<doctest> type of assert used - Catch: only normal") |
onqtam | 956d4ec | 2016-09-18 13:42:33 +0300 | [diff] [blame] | 27 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 28 | parser = argparse.ArgumentParser() |
| 29 | subparsers = parser.add_subparsers() |
| 30 | parser_c = subparsers.add_parser('compile', help='benchmark compile times') |
| 31 | addCommonFlags(parser_c) |
| 32 | parser_c.add_argument("--implement", action = "store_true", help = "implement the framework test runner") |
| 33 | parser_c.add_argument("--header", action = "store_true", help = "include the framework header everywhere") |
| 34 | parser_r = subparsers.add_parser('runtime', help='benchmark runtime') |
| 35 | addCommonFlags(parser_r) |
| 36 | parser_r.add_argument("--loop-iters", type=int, default=1000, help = "loop N times all asserts in each test case") |
onqtam | 7d8ccec | 2017-05-11 19:31:15 +0300 | [diff] [blame] | 37 | parser_r.add_argument("--info", action = "store_true", help = "log the loop variable with INFO()") |
onqtam | df09f51 | 2016-09-15 13:43:50 +0300 | [diff] [blame] | 38 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 39 | def compile(args): args.compile = True; args.runtime = False |
| 40 | def runtime(args): args.compile = False; args.runtime = True |
| 41 | parser_c.set_defaults(func=compile) |
| 42 | parser_r.set_defaults(func=runtime) |
| 43 | args = parser.parse_args() |
| 44 | args.func(args) |
| 45 | |
onqtam | 778e94b | 2017-05-10 23:19:50 +0300 | [diff] [blame] | 46 | print("== PASSED OPTIONS TO BENCHMARK SCRIPT:") |
| 47 | pprint.pprint(vars(args), width = 1) |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 48 | |
| 49 | # ============================================================================== |
| 50 | # == SETUP ENVIRONMENT ========================================================= |
| 51 | # ============================================================================== |
| 52 | |
| 53 | # catch version |
onqtam | c8692c4 | 2017-09-05 18:47:55 +0300 | [diff] [blame] | 54 | catch_ver = "2.0.0-develop.3" |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 55 | catch_header = "catch." + catch_ver + ".hpp" |
| 56 | |
| 57 | # get the catch header |
| 58 | if not os.path.exists("catch." + catch_ver + ".hpp"): |
| 59 | urllib.urlretrieve ("https://raw.githubusercontent.com/philsquared/Catch/v" + catch_ver + "/single_include/catch.hpp", catch_header) |
| 60 | |
| 61 | # folder with generated code |
| 62 | the_folder = 'project' |
| 63 | |
| 64 | # delete the folder |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 65 | if os.path.exists(the_folder): |
| 66 | shutil.rmtree(the_folder) |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 67 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 68 | # wait a bit or the script might fail... |
| 69 | sleep(2) |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 70 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 71 | # create the folder |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 72 | if not os.path.exists(the_folder): |
| 73 | os.makedirs(the_folder) |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 74 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 75 | # enter folder |
| 76 | os.chdir(the_folder); |
| 77 | |
| 78 | # ============================================================================== |
| 79 | # == DO STUFF ================================================================== |
| 80 | # ============================================================================== |
| 81 | |
| 82 | # setup defines used |
| 83 | defines = "" |
onqtam | 87e80e5 | 2017-09-07 12:50:53 +0300 | [diff] [blame^] | 84 | if args.catch and args.disabled: |
| 85 | defines += "#define CATCH_CONFIG_DISABLE\n" |
| 86 | if not args.catch and args.disabled: |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 87 | defines += "#define DOCTEST_CONFIG_DISABLE\n" |
| 88 | if args.catch and args.fast: |
| 89 | defines += "#define CATCH_CONFIG_FAST_COMPILE\n" |
| 90 | if not args.catch and args.fast: |
| 91 | defines += "#define DOCTEST_CONFIG_SUPER_FAST_ASSERTS\n" |
| 92 | |
| 93 | define_implement = "#define DOCTEST_CONFIG_IMPLEMENT\n" |
| 94 | if args.catch: |
| 95 | define_implement = "#define CATCH_CONFIG_RUNNER\n" |
| 96 | |
| 97 | # setup the macros used |
| 98 | macro = " CHECK(a == b);\n" |
onqtam | 7f2c0ba | 2017-05-12 03:10:01 +0300 | [diff] [blame] | 99 | if args.runtime: |
| 100 | macro = " CHECK(i == i);\n" |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 101 | if not args.catch and args.asserts == "binary": |
| 102 | macro = " CHECK_EQ(a, b);\n" |
| 103 | if not args.catch and args.asserts == "fast": |
| 104 | macro = " FAST_CHECK_EQ(a, b);\n" |
| 105 | |
| 106 | # setup the header used |
| 107 | include = '#include "doctest.h"\n' |
| 108 | if args.catch: |
| 109 | include = '#include "' + catch_header + '"\n' |
| 110 | |
| 111 | # ============================================================================== |
| 112 | # == GENERATE SOURCE CODE ====================================================== |
| 113 | # ============================================================================== |
| 114 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 115 | # make the source files |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 116 | for i in range(0, args.files): |
| 117 | f = open(str(i) + '.cpp', 'w') |
| 118 | if args.runtime or args.header: |
| 119 | f.write(defines) |
| 120 | f.write(include) |
| 121 | for t in range(0, args.tests): |
onqtam | 200e888 | 2016-09-20 11:01:02 +0300 | [diff] [blame] | 122 | f.write('TEST_CASE("") {\n') |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 123 | f.write(' int a = 5;\n') |
| 124 | f.write(' int b = 5;\n') |
| 125 | if args.runtime and args.loop_iters > 0: |
| 126 | f.write(' for(int i = 0; i < ' + str(args.loop_iters) + '; ++i) {\n') |
onqtam | 778e94b | 2017-05-10 23:19:50 +0300 | [diff] [blame] | 127 | if args.runtime and args.info: |
| 128 | f.write(' INFO(i);\n') |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 129 | for a in range(0, args.checks): |
| 130 | if args.runtime and args.loop_iters > 0: |
| 131 | f.write(' ') |
| 132 | f.write(macro) |
| 133 | if args.runtime and args.loop_iters > 0: |
| 134 | f.write(' }\n') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 135 | f.write('}\n\n') |
| 136 | f.write('int f' + str(i) + '() { return ' + str(i) + '; }\n\n') |
| 137 | f.close() |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 138 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 139 | # the main file |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 140 | f = open('main.cpp', 'w') |
onqtam | dc92a3c | 2017-05-11 00:08:50 +0300 | [diff] [blame] | 141 | if args.runtime or args.implement or args.header: |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 142 | f.write(defines) |
| 143 | f.write(define_implement) |
| 144 | f.write(include) |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 145 | f.write('int main(int argc, char** argv) {\n') |
onqtam | dc92a3c | 2017-05-11 00:08:50 +0300 | [diff] [blame] | 146 | if args.runtime or args.implement or args.header: |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 147 | if not args.catch: f.write(' int res = doctest::Context(argc, argv).run();\n') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 148 | else: f.write(' int res = Catch::Session().run(argc, argv);\n') |
| 149 | else: |
| 150 | f.write(' int res = 0;\n') |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 151 | for i in range(0, args.files): |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 152 | f.write(' int f' + str(i) + '(); res += f' + str(i) + '();\n') |
| 153 | f.write(' return res;\n}\n') |
| 154 | f.close() |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 155 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 156 | # the cmake file |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 157 | f = open('CMakeLists.txt', 'w') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 158 | f.write('cmake_minimum_required(VERSION 2.8)\n\n') |
onqtam | 9497821 | 2016-05-22 00:07:30 +0300 | [diff] [blame] | 159 | f.write('project(bench)\n\n') |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 160 | if not args.catch: f.write('include_directories("../../../doctest/")\n\n') |
| 161 | else: f.write('include_directories("../")\n\n') |
onqtam | 9497821 | 2016-05-22 00:07:30 +0300 | [diff] [blame] | 162 | f.write('add_executable(bench main.cpp\n') |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 163 | for i in range(0, args.files): |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 164 | f.write(' ' + str(i) + '.cpp\n') |
| 165 | f.write(')\n') |
| 166 | f.close() |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 167 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 168 | # ============================================================================== |
| 169 | # == INVOKE CMAKE ============================================================== |
| 170 | # ============================================================================== |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 171 | |
onqtam | dc92a3c | 2017-05-11 00:08:50 +0300 | [diff] [blame] | 172 | compiler = "" |
onqtam | 7d8ccec | 2017-05-11 19:31:15 +0300 | [diff] [blame] | 173 | if args.compiler == 'clang': |
onqtam | 7f2c0ba | 2017-05-12 03:10:01 +0300 | [diff] [blame] | 174 | compiler = " -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS=-w" |
onqtam | 7d8ccec | 2017-05-11 19:31:15 +0300 | [diff] [blame] | 175 | if args.compiler == 'gcc': |
onqtam | 7f2c0ba | 2017-05-12 03:10:01 +0300 | [diff] [blame] | 176 | compiler = " -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS=-w" |
onqtam | dc92a3c | 2017-05-11 00:08:50 +0300 | [diff] [blame] | 177 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 178 | # setup cmake command |
| 179 | cmake_command = 'cmake . -G "Visual Studio 15 Win64"' # MSVC 2017 |
onqtam | 7d8ccec | 2017-05-11 19:31:15 +0300 | [diff] [blame] | 180 | if args.compiler != 'msvc': |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 181 | cmake_command = 'cmake . -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=' + ('Debug' if args.debug else 'Release') |
| 182 | if os.name != "nt": |
| 183 | cmake_command = 'cmake . -DCMAKE_BUILD_TYPE=' + ('Debug' if args.debug else 'Release') |
| 184 | |
onqtam | dc92a3c | 2017-05-11 00:08:50 +0300 | [diff] [blame] | 185 | os.system(cmake_command + compiler) |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 186 | |
| 187 | # ============================================================================== |
| 188 | # == BUILD PROJECT ============================================================= |
| 189 | # ============================================================================== |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 190 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 191 | the_config = '' |
onqtam | 7d8ccec | 2017-05-11 19:31:15 +0300 | [diff] [blame] | 192 | if args.compiler == 'msvc': |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 193 | if args.debug: the_config = ' --config Debug' |
| 194 | else: the_config = ' --config Release' |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 195 | |
onqtam | d4669c1 | 2016-05-22 00:24:44 +0300 | [diff] [blame] | 196 | # build it |
| 197 | start = datetime.now() |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 198 | os.system('cmake --build .' + the_config) |
| 199 | end = datetime.now() |
onqtam | d4669c1 | 2016-05-22 00:24:44 +0300 | [diff] [blame] | 200 | |
onqtam | 7f2c0ba | 2017-05-12 03:10:01 +0300 | [diff] [blame] | 201 | if not args.runtime: |
| 202 | print("Time running compiler (+ linker) in seconds: " + str((end - start).total_seconds())) |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 203 | |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 204 | # ============================================================================== |
| 205 | # == RUN PROJECT =============================================================== |
| 206 | # ============================================================================== |
| 207 | |
| 208 | if args.runtime: |
| 209 | start = datetime.now() |
onqtam | 7d8ccec | 2017-05-11 19:31:15 +0300 | [diff] [blame] | 210 | if args.compiler == 'msvc': |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 211 | os.system(('Debug' if args.debug else 'Release') + '\\bench.exe') |
| 212 | elif os.name == "nt": |
| 213 | os.system('bench.exe') |
| 214 | else: |
| 215 | os.system('./bench') |
| 216 | end = datetime.now() |
| 217 | |
onqtam | 706eb4a | 2017-05-11 12:43:23 +0300 | [diff] [blame] | 218 | print("Time running the tests in seconds: " + str((end - start).total_seconds())) |
onqtam | fea36c5 | 2017-05-10 22:52:57 +0300 | [diff] [blame] | 219 | |
| 220 | # leave folder |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 221 | os.chdir("../"); |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |