github.com/developest/gtm-core@v1.0.4-0.20220111132249-cc80a3372c3f/test/commit_test.py (about)

     1  """Test script."""
     2  
     3  import os
     4  import stat
     5  import shutil
     6  import subprocess
     7  import time
     8  
     9  from matplotlib import pyplot as plt
    10  
    11  TEST_PATH = './gtm_tests/'
    12  GTM_PATH = '../build/go_build_github_com_kilpkonn_gtm_enhanced'
    13  TEST_FILE_NAME = 'test.txt'
    14  
    15  
    16  def setup():
    17      """Setup tests."""
    18      shutil.copyfile(GTM_PATH, 'gtm')
    19      st = os.stat('gtm')
    20      os.chmod('gtm', st.st_mode | stat.S_IEXEC)
    21      subprocess.call(['git', 'init'])
    22      with open(TEST_FILE_NAME, 'w') as f:
    23          f.write("0")
    24      subprocess.call(['git', 'add', '.'])
    25      subprocess.call(['git', 'commit', '.', '-m "test"'])
    26  
    27  
    28  def cleanup():
    29      """Clean tests"""
    30      os.chdir('..')
    31      shutil.rmtree(TEST_PATH, ignore_errors=True)
    32      os.makedirs(TEST_PATH, exist_ok=True)
    33      os.chdir(TEST_PATH)
    34  
    35  
    36  def get_size(start_path: str = '.'):
    37      total_size = 0
    38      for dir_path, dir_names, filenames in os.walk(start_path):
    39          for f in filenames:
    40              fp = os.path.join(dir_path, f)
    41              # skip if it is symbolic link
    42              if not os.path.islink(fp):
    43                  total_size += os.path.getsize(fp)
    44  
    45      return total_size
    46  
    47  
    48  def test_commit_benchmark(n: int):
    49      """Test commit 10000 times"""
    50      for i in range(n):
    51          with open(TEST_FILE_NAME, 'w') as f:
    52              f.write(str(i))
    53          subprocess.call(['git', 'commit', '.', '-m "test"'], )
    54  
    55  
    56  def test_commit_gtm(n: int):
    57      """Test commit 10000 times"""
    58      for i in range(n):
    59          with open(TEST_FILE_NAME, 'w') as f:
    60              f.write(str(i))
    61          subprocess.call(['./gtm', 'record', TEST_FILE_NAME])
    62          subprocess.call(['git', 'commit', '.', '-m "test"'])
    63  
    64  
    65  def test_commit_increasing_size_benchmark(n: int, increase_multiplier: float, offset: int = 0):
    66      """Test commit 10000 times with increasing file size."""
    67      for i in range(n):
    68          with open(TEST_FILE_NAME, 'a') as f:
    69              new_text = ''.join([f'{n + offset} - {x}\n' for x in range(round((offset + i) * increase_multiplier))])
    70              f.write(new_text)
    71          subprocess.call(['git', 'commit', '.', '-m "test"'])
    72  
    73  
    74  def test_commit_increasing_size_gtm(n: int, increase_multiplier: float, offset: int = 0):
    75      """Test commit 10000 times with increasing file size."""
    76      for i in range(n):
    77          with open(TEST_FILE_NAME, 'a') as f:
    78              new_text = ''.join([f'{offset + n} - {x}\n' for x in range(round((offset + i) * increase_multiplier))])
    79              f.write(new_text)
    80  
    81          for _ in range(round((offset + i) * increase_multiplier)):
    82              subprocess.call(['./gtm', 'record', TEST_FILE_NAME])
    83          subprocess.call(['git', 'commit', '.', '-m "test"'])
    84  
    85  
    86  def test_commit_record_gtm(n: int):
    87      """Test commit 10000 times"""
    88      for i in range(n):
    89          subprocess.call(['./gtm', 'record', TEST_FILE_NAME])
    90  
    91  
    92  if __name__ == '__main__':
    93      os.makedirs(TEST_PATH, exist_ok=True)
    94      os.chdir(TEST_PATH)
    95  
    96      results = ['Results:']
    97  
    98      commit_times_benchmark = []
    99      commit_times_gtm = []
   100      git_size_benchmark = []
   101      git_size_gtm = []
   102      n = 10000
   103      x_tick_len = 25
   104  
   105      setup()
   106      for _ in range(n // x_tick_len):
   107          start_benchmark = time.time()
   108          test_commit_benchmark(x_tick_len)
   109          end_benchmark = time.time()
   110          commit_times_benchmark.append(round(end_benchmark - start_benchmark, 3))
   111          git_size_benchmark.append(get_size('.git'))
   112      size_benchmark = get_size('.git')
   113      cleanup()
   114  
   115      setup()
   116      for _ in range(n // x_tick_len):
   117          start_gtm = time.time()
   118          test_commit_gtm(x_tick_len)
   119          end_gtm = time.time()
   120          commit_times_gtm.append(round(end_gtm - start_gtm, 3))
   121          git_size_gtm.append(get_size('.git'))
   122      size_gtm = get_size('.git')
   123      cleanup()
   124  
   125      results.append('-' * 50)
   126      results.append(f'{n} commits Benchmark time: {round(sum(commit_times_benchmark), 2)}s')
   127      results.append(f'{n} commits Gtm time: {round(sum(commit_times_gtm), 2)}s')
   128      results.append(f'Benchmark .git folder size: {round(size_benchmark / 1024, 2)} kB')
   129      results.append(f'Gtm .git folder size: {round(size_gtm / 1024, 2)} kB')
   130  
   131      plt.plot([x for x in range(round(n / x_tick_len))], commit_times_benchmark, label='Benchmark')
   132      plt.plot([x for x in range(round(n / x_tick_len))], commit_times_gtm, label='Gtm')
   133      plt.legend()
   134      plt.xlabel('Run number')
   135      plt.ylabel(f'{x_tick_len} commit time')
   136      plt.savefig('../commit_times.png')
   137      plt.clf()
   138  
   139      plt.plot([x for x in range(round(n / x_tick_len))], git_size_benchmark, label='Benchmark')
   140      plt.plot([x for x in range(round(n / x_tick_len))], git_size_gtm, label='Gtm')
   141      plt.legend()
   142      plt.xlabel('Run number')
   143      plt.ylabel(f'{x_tick_len} .git folder size')
   144      plt.savefig('../git_size.png')
   145      plt.clf()
   146  
   147      commit_times_benchmark = []
   148      commit_times_gtm = []
   149      git_size_benchmark = []
   150      git_size_gtm = []
   151      n = 250
   152      x_tick_len = 5
   153  
   154      setup()
   155      for a in range(n // x_tick_len):
   156          start_benchmark = time.time()
   157          test_commit_increasing_size_benchmark(x_tick_len, 1.5, a * x_tick_len)
   158          end_benchmark = time.time()
   159          commit_times_benchmark.append(round(end_benchmark - start_benchmark, 3))
   160          git_size_benchmark.append(get_size('.git'))
   161      size_benchmark = get_size('.git')
   162      cleanup()
   163  
   164      setup()
   165      for a in range(n // x_tick_len):
   166          start_gtm = time.time()
   167          test_commit_increasing_size_gtm(x_tick_len, 1.5, a * x_tick_len)
   168          end_gtm = time.time()
   169          commit_times_gtm.append(round(end_gtm - start_gtm, 3))
   170          git_size_gtm.append(get_size('.git'))
   171      size_gtm = get_size('.git')
   172      cleanup()
   173  
   174      results.append('-' * 50)
   175      results.append(f'{n} commits inc size Benchmark time: {round(sum(commit_times_benchmark), 2)}s')
   176      results.append(f'{n} commits inc size ({n**2 * 1.5 / 2} record events) Gtm time: {round(sum(commit_times_gtm), 2)}s')
   177      results.append(f'Benchmark .git folder size: {round(size_benchmark / 1024, 2)} kB')
   178      results.append(f'Gtm .git folder size: {round(size_gtm / 1024, 2)} kB')
   179  
   180      record_times = []
   181      setup()
   182      for a in range(n // x_tick_len):
   183          start_gtm = time.time()
   184          test_commit_record_gtm(round(a * 1.5 * x_tick_len))
   185          end_gtm = time.time()
   186          record_times.append(round(end_gtm - start_gtm, 3))
   187      cleanup()
   188  
   189      commit_times_benchmark = [x - y for x, y in zip(commit_times_gtm, record_times)]
   190      plt.plot([x for x in range(round(n / x_tick_len))], commit_times_benchmark, label='Benchmark')
   191      plt.plot([x for x in range(round(n / x_tick_len))], commit_times_gtm, label='Gtm')
   192      plt.legend()
   193      plt.xlabel('Run number')
   194      plt.ylabel(f'{x_tick_len} commit time')
   195      plt.savefig('../commit_times_inc.png')
   196      plt.clf()
   197  
   198      plt.plot([x for x in range(round(n / x_tick_len))], git_size_benchmark, label='Benchmark')
   199      plt.plot([x for x in range(round(n / x_tick_len))], git_size_gtm, label='Gtm')
   200      plt.legend()
   201      plt.xlabel('Run number')
   202      plt.ylabel(f'{x_tick_len} .git folder size (kb)')
   203      plt.yscale('log')
   204      plt.savefig('../git_size_inc.png')
   205      plt.clf()
   206  
   207      results.append(f'{n * 1.5 * n / 2} record event time: {round(sum(record_times), 2)}s')
   208  
   209      for line in results:
   210          print(line)