-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_test.py
executable file
·100 lines (86 loc) · 3.12 KB
/
script_test.py
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
import string
import random
import time
import subprocess
import os
import sys
import getopt
PYGAL_OK = True
#Pygal is a free package for creating charts. It's not natively installed, so
#if you don't get it no chart will be generated
try:
import pygal #Use to draw charts
except ImportError:
PYGAL_OK = False
print("\nYou don't have the package Pygal to generate chart. Install it or no charts will be generated\n")
pass
PROGRAM = "./words"
WORD_RANGE = [1000,5000,10000,20000,50000,100000]
MAX_SIZE_WORD = 20
# Display help menu
#----------------------------------------------------------------------
def display_help():
print()
print("--------- Algo test script ----------")
print("Semester 2 - Project 1")
print()
print("Author : EMMANUEL Baptiste & GELPE Mathieu")
print("-------------------------------------")
print()
print("Usage : ")
print("-h, --help Display help")
print("-s, --max_size=20 Maximum word's size to test. Equal 20 by default")
print()
#----------------------------------------------------------------------
#----------------- MAIN SCRIPT -----------------------------------------
# Get arguments from command line
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:", ["help","max_size"])
except getopt.GetoptError as err:
print(err)
display_help()
sys.exit(2)
#Analyze arguments from commande line
for o,a in opts:
if o in ("-h", "--help"):
display_help()
sys.exit(2)
elif o in ("-s", "--max_size"):
MAX_SIZE_WORD = int(a)
else:
print("Option {} unknown".format(o))
sys.exit(2)
tab_results = []
for words_number in WORD_RANGE:
tab_times = []
tab_words = [""]*words_number
for i in range(1,MAX_SIZE_WORD):
#Generate WORD_NUMBER words of i size
tab_words = [tab_words[i]+random.choice(string.ascii_lowercase) for i in range(0,words_number)]
#Create file to write words
file_tmp = open("tmp.txt","w")
for word in tab_words:
file_tmp.write("%s\n" % word)
file_tmp.close()
#Start the program
start = time.time()
#subprocess.call([PROGRAM,"<","tmp.txt",">","/dev/null"], shell=True)
subprocess.call(["./words < tmp.txt > /dev/null"], shell=True)
end = time.time()
tab_times.append((end - start) * 1000)
os.remove("tmp.txt")
tab_results.append(tab_times)
print("DONE : %d words" % words_number)
if PYGAL_OK:
#Draw the chart
line_chart = pygal.Line(show_dots=False, x_title="Word length", y_title="Time (ms)")
line_chart.title = 'Time to find a word according to its length'
line_chart.x_labels = map(str, range(1,MAX_SIZE_WORD))
for i in range(0,len(tab_results)):
line_chart.add(str(WORD_RANGE[i]), tab_results[i])
line_chart.render_to_file('graph_test.svg')
print("DONE : Chart graph_test.svg generated")
subprocess.call(["inkscape graph_test.svg"], shell=True)
else:
print("No charts generating because Pygal is missing")
#---------------END SCRIPT --------------------------------------------