Commits

Anonymous committed c859d6c

Added test of distributed version control systems - first: git and Mercurial.

Comments (0)

Files changed (4)

distributed_version_control_system_test.py

+#!/usr/bin/env python
+# encoding: utf-8
+
+"""Test the speed of different distributed version control systems. 
+
+First targets: git and Mercurial
+
+Proceeding: Just call them via the bash and check their speed with the general_test_function. 
+
+Ideas: Include bazaar, too. 
+"""
+
+# First get the call function, so we can call the systems via the bash
+from subprocess import call
+
+# for convenience, we create a call, which uses the shell by default
+def shellcall(command): 
+    """Call a string on the shell."""
+    # print "shellcall", command
+    return call(command, shell=True)
+
+# also get some small useful functions and the genral test function
+# We'll use many random tuples to create files with content
+from small_useful_functions import create_many_random_tuples
+# and the general test function and the mean to get and evaluate the data. 
+from general_test_function import general_test_function, multiple_tests, mean
+
+def test_dvcs(number_of_files=10000): 
+    """Test one dvcs.
+    
+    For this we 
+        - create a directory
+        - initialize the repository
+        - create many files with random content,  
+        - commit the first version
+        - append additional random data to each file and
+        - commit the second version. 
+    
+    All this will be times. 
+    """
+    # First we need a dict to hold our results. 
+    test_results = {} #: The results of our tests. 
+    
+    # Then we create the directory and then change into it
+    from os import mkdir, chdir
+    DIR="dir"
+    try: 
+        mkdir(DIR)
+    except: pass
+    chdir(DIR)
+    
+    # Now we get the names and data to write into the files. 
+    data = create_many_random_tuples(number_of_files)
+    
+    # Then we remove all files within the dir
+    shellcall("rm -Rf *")
+    
+    # For each of the tuples, we create a file with the first item as name and teh second as data
+    for i in data: 
+        f = open(str(i[0]), "w")
+        f.write(str(i[1]))
+        f.close()
+    
+    # Now we initialize the repository over the general_test_function. 
+    results = multiple_tests(10, [init_git, init_hg])
+    # Add it to our test results
+    test_results["init"] = mean(results)
+    # And print it
+    print "Init results: [git, hg]", mean(results)
+    
+    # Then we commit all files
+    results = multiple_tests(10, [init_add_and_commit_git, init_add_and_commit_hg])
+    # Add it to our test results
+    test_results["initial_commit"] = mean(results)
+    # And print it
+    print "Initial commit results: [git, hg]", mean(results)
+    
+    # Now we need to append stuff to the files. We just use the filename + the same data . 
+    for i in data: 
+        f = open(str(i[0]), "a")
+        f.write(str(i[0]) + str(i[1]))
+        f.close()
+        
+    # We do only one test. TODO: Copy the repo, then move it back each time and commit. 
+    results = multiple_tests(1, [commit_git, commit_hg])
+    # Add it to our test results
+    test_results["commit_after_append"] = mean(results)
+    # And print it
+    print "Commit results after append: [git, hg]", mean(results)
+    
+    # Now we test what happens if we add the same data to each file, but lots of it. 
+    for i in data: 
+        f = open(str(i[0]), "a")
+        f.write(str(data))
+        f.close
+    
+    # We do only one test. TODO: Copy the repo, then move it back each time and commit. 
+    results = multiple_tests(1, [commit_git, commit_hg])
+    # Add it to our test results
+    test_results["commit_after_append_of_same_big_data"] = mean(results)
+    # And print it
+    print "Commit results after append of same big data: [git, hg]", mean(results)
+    
+    
+    return test_results
+
+def init_git(): 
+    """Initialize a git repository."""
+    # remove an old repo
+    shellcall("rm -Rf .git")
+    # create a new one
+    shellcall("git init")
+
+def init_add_and_commit_git(): 
+    """Commit to the git repository."""
+    # Init the repo
+    init_git()
+    # And commit
+    commit_git()
+
+def commit_git(): 
+    """Commit to the git repository."""
+    # Add all files
+    shellcall("git add *")
+    # And commit
+    shellcall("git commit -m 'test'")
+    
+
+def init_hg(): 
+    """Initialize a Mercurial repository."""
+    # remove an old repository
+    shellcall("rm -Rf .hg")
+    # create a new one. 
+    shellcall("hg init")
+
+def init_add_and_commit_hg(): 
+    """Commit to the Mercurial repository."""
+    # Init the repo
+    init_hg()
+    # Add all files
+    shellcall("hg add *")
+    # And commit
+    commit_hg()
+
+def commit_hg(): 
+    """Commit to the Mercurial repository."""
+    # And commit
+    shellcall("hg commit -m 'test'")
+
+
+#### Self-Test ####
+if __name__ == "__main__": 
+    # Get yaml to output the results nicely
+    from yaml import dump
+    # Get the command line args to get the number of files to test
+    from sys import argv
+    try: 
+        print dump(test_dvcs(number_of_files=int(argv[1])))
+    except: 
+        print "Please provide the number of files to test."

for_loops_geschwindigkeitstest.py

 The general_test_function can be used to test the execution time of any number of functions. 
 """
 
-# First get the general test function
-from general_test_function import general_test_function
+# First get the general test function and the mean calculation for it. 
+from general_test_function import general_test_function, mean
+
+# Also we get some useful small functions
+from small_useful_functions import create_many_random_tuples
 
 def test_speed_of_for_loop_with_tuples(): 
     """Test the speed of two different for loops to see, if there's a difference.
 TODO: Test, if a test with Strings comes to the same result.
 
 """
-    # We need random numbers, so we can create unoptimizeable lists. 
-    from random import random
-    # Now we need an empty list. 
-    many_tuples = []
-    # Which we fill with 100.000 tuples of random numbers. 
-    for i in range(100000): 
-        many_tuples.append((random(), random()))
+    # We need a list of tuples with random data. 
+    many_tuples = create_many_random_tuples(number=100000)
     
     # Now we get to the real test. 
     # We just call the functions in our test function :) 
     results = []
     for i in range(10): 
         results.append(general_test_function(list_of_functions, many_tuples))
-    
-    # Calculate the mean
-    mean_time = []
-    # Work on each result
-    for i in results: 
-        # Get the times of this result as list. 
-        times_list = []
-        # Work on each function inside the single run to gets its time
-        for j in i: 
-            # Append the time to the times_list. 
-            times_list.append(j[0])
-        # Now add the times in the times list to the mean_time. 
-        # If the times_list has a different length than the mean_time list, this should be the first run. We need to append the times. 
-        if len(times_list) != len(mean_time): 
-            for i in times_list: 
-                mean_time.append(i)
-        # Else, this is a following run, so we can just add to each element. 
-        else: 
-            for i in range(len(times_list)): 
-                mean_time[i] += times_list[i]
-    # Now we divide each element inside the mean_time by the number of runs (the len of the results. 
-    for i in range(len(mean_time)): 
-        mean_time[i] = float(mean_time[i]) / len(results)
-    return mean_time # TODO: Calculate the standard deviation, so we know how certain the result is. 
 
+    # Now get the mean time
+    mean_time = mean(results)
+    # and return it. 
+    return mean_time
 
 
 def loop_which_directly_calls_each_tuple_item(many_tuples): 

general_test_function.py

     # Return the list of execution times, sorted by functions. 
     return execution_times
 
+def multiple_tests(number_of_tests, functions, *args): 
+    """run multiple tests."""
+    # First create an empty result list
+    results = []
+    # Then fill it with results :) 
+    for i in range(number_of_tests): 
+        print "run", i
+        results.append(general_test_function(functions, *args))
+    return results
+
+
+def mean(data): 
+    """Calculate the mean value from a list of ilists of tuples which the general_test_function returns."""
+    # Calculate the mean
+    mean_time = []
+    # Work on each result
+    for i in data: 
+        # Get the times of this result as list. 
+        times_list = []
+        # Work on each function inside the single run to gets its time
+        for j in i: 
+            # Append the time to the times_list. 
+            times_list.append(j[0])
+        # Now add the times in the times list to the mean_time. 
+        # If the times_list has a different length than the mean_time list, this should be the first run. We need to append the times. 
+        if len(times_list) != len(mean_time): 
+            for i in times_list: 
+                mean_time.append(i)
+        # Else, this is a following run, so we can just add to each element. 
+        else: 
+            for i in range(len(times_list)): 
+                mean_time[i] += times_list[i]
+    # Now we divide each element inside the mean_time by the number of runs (the len of the results. 
+    for i in range(len(mean_time)): 
+        mean_time[i] = float(mean_time[i]) / len(data)
+    return mean_time # TODO: Calculate the standard deviation, so we know how certain the result is. 
+
+
 
 
 ### Self-Test ###

small_useful_functions.py

+#!/usr/bin/env python
+# encoding: utf-8
+
+def create_many_random_tuples(number=10000): 
+    """Create number tuples containing random data."""
+    # First need random numbers, so we can create unoptimizeable lists. 
+    from random import random
+    # Now we need an empty list. 
+    many_tuples = []
+    # Which we fill with 100.000 tuples of random numbers. 
+    for i in range(number): 
+        many_tuples.append((random(), random()))
+    
+    return many_tuples
+