Debian Codegraph @

apps/tc/tests-stdlib/bench/bench_inj.py

raw symbols SCIP
1 import subprocess
2 import time
3 import sys
4 import os
5 import re
6
7 """
8 About this file:
9
10 - it aims to test elpi vs coq performances of
11 type class search.
12
13 - it should be run from the ./apps/tc folder
14
15 - parameters of command line:
16 * N : the depth of the tree to build in tests/test.v
17 * -nocoq : optional to test only elpi
18 * -onlyOne : optional to run the test only for the tree of size N. By default, the tests are made for each i in [1..N] included
19 """
20
21 INJ_BASE_FUN = "f"
22 KEYS = "coqT, elpiT, tcSearch, refineT, compilT, runtimeT, buildQuery".split(", ")
23
24
25 def buildDict():
26 res = dict()
27 for key in KEYS:
28 res[key] = []
29 return res
30
31
32 def printDict(d):
33 for key in KEYS:
34 d[key] = sum(d[key])/len(d[key])
35 L = [d[k] for k in KEYS]
36 L.append(d["elpiT"] - d["refineT"] - d["buildQuery"])
37 L.append(d["coqT"] / d["elpiT"])
38 L.append(d["elpiT"] / d["coqT"] if d["coqT"] > 0 else 100)
39 print(", ".join(map(lambda x: str(round(x, 5)), L)))
40
41
42 def findFloats(s):
43 return [float(x) for x in re.findall("\d+\.\d*", s)]
44
45
46 def filterLines(lines):
47 #print(lines)
48 validStarts = ["Finished", "Refine", "Elpi:", "Instance search", "Time build query"]
49 for line in lines.split("\n"):
50 for start in validStarts:
51 if start in line:
52 yield line
53
54
55 def parseFile(s):
56 lines = [findFloats(x) for x in filterLines(s)]
57 #print(lines)
58 base = 0
59 coqT = lines[base][0]
60 buildQuery = lines[base + 1][0]
61 tcSearch = lines[base + 2][0]
62 refineT = lines[base + 3][0]
63 elpiStats = lines[base + 4]
64 compilT, runtimeT = elpiStats[0], elpiStats[-1]
65 elpiT = lines[base + 5][0]
66 res = buildDict()
67 for key in KEYS:
68 res[key].append(eval(key))
69 #print(res)
70 return res
71
72
73 def buildTree(len):
74 if len == 0:
75 return INJ_BASE_FUN + " "
76 S = buildTree(len-1)
77 STR = "(compose " + S + S + ")"
78 return STR
79
80 accumulate = """
81 Elpi Accumulate TC_solver lp:{{
82 :after "firstHook"
83 tc {{:gref Inj}} {{Inj lp:R1 lp:R1 (@compose lp:A lp:A lp:A lp:L lp:R)}} Sol :-
84 L = R, !,
85 tc {{:gref Inj}} {{Inj lp:R1 lp:R1 lp:L}} InjL,
86 Sol = {{@compose_inj lp:A lp:A lp:A lp:R1 lp:R1 lp:R1 lp:L lp:L lp:InjL lp:InjL }}.
87 }}.
88 """
89
90 def writeFile(fileName: str, composeLen: int, isCoq: bool):
91 PREAMBLE = f"""\
92 From elpi_apps_tc_tests_stdlib Require Import {"stdppInjClassic" if isCoq else "stdppInj"}.
93 {"" if isCoq else 'Elpi TC.Solver. Set TC Time Refine. Set TC Time Instance Search. Set TC Time Build Query. Set Debug "elpitime".'}
94 """
95 GOAL = buildTree(composeLen)
96 with open(fileName + ".v", "w") as fd:
97 fd.write(PREAMBLE)
98 fd.write(f"Goal Inj eq eq({GOAL}). Time apply _. Qed.\n")
99
100
101 def runCoqMake(fileName):
102 fileName = fileName + ".vo"
103 if (os.path.exists(file_name)):
104 subprocess.run(["rm", fileName])
105 return subprocess.check_output(["make", fileName]).decode()
106
107
108 def run(file_name, height):
109 def partialFun(isCoq: bool):
110 writeFile(file_name, height, isCoq)
111 return runCoqMake(file_name)
112 return partialFun
113
114
115 def loopTreeDepth(file_name: str, maxHeight: int, makeCoq=True, onlyOne=False):
116 print("Height, Coq, Elpi, TC search, Refine, ElpiCompil, ElpiRuntime, BuildQuery, ElpiNoRefine, Ratio(Coq/Elpi), Ratio(Elpi/Coq)")
117 for i in range(1 if not onlyOne else maxHeight, maxHeight+1):
118 FUN = run(file_name, i)
119 x = FUN(True) if makeCoq else "Finished 0.0"
120 y = FUN(False)
121 print(i, ", ", end="", sep="")
122 # print("The xx result is " , x)
123 dic = parseFile(x + y)
124 printDict(dic)
125
126
127 if __name__ == "__main__":
128 print(os.curdir)
129 file_name = "tests/bench/bench_inj"
130 height = int(sys.argv[1])
131 loopTreeDepth(file_name, height, makeCoq=not (
132 "-nocoq" in sys.argv), onlyOne=("-onlyOne" in sys.argv))
133 #writeFile(file_name, 1, False)