-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
executable file
·59 lines (47 loc) · 1.37 KB
/
benchmark.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
#!/usr/bin/env python3
# Python RiveScript benchmark for A.L.I.C.E.
import statistics
import time
from rivescript import RiveScript
test_inputs = [
"Hello.",
"Hello Alice",
"Who is dr. wallace?",
"What is your name?",
"What is my name?",
"What is the capital of Texas?",
]
def main():
"""Main entry point."""
count = timereps(5, test_rs)
print("Average time of run: {:.8f}s".format(count))
def test_rs():
"""Test how fast RiveScript runs."""
t = time.time()
# Load Alice from disk.
bot = RiveScript()
bot.load_directory("./alice")
t = delta(t, "Loading A.L.I.C.E. from disk.")
# Sort the replies.
bot.sort_replies()
t = delta(t, "Sort the replies.")
# Get some responses.
for message in test_inputs:
reply = bot.reply("user", message)
t = delta(t, "Reply to {} => {}".format(message, reply))
print("")
def delta(t, desc):
"""Helper function to print a time delta with a message."""
delta = time.time() - t
print("[{:.8f}s] {}".format(delta, desc))
return time.time()
def timereps(reps, func):
"""Helper function to call a function multiple times for benchmarking."""
times = []
for i in range(0, reps):
start = time.time()
func()
times.append(time.time() - start)
return statistics.mean(times)
if __name__ == "__main__":
main()