Skip to content

Commit 99ba8cf

Browse files
committed
Improve callgen speed
When the sound directory is large, iterating over all items appears to be really slow. For this reason, we change the behavior from searching all subdirs to explicitly mention search directories in the commandline. Only these paths will be searched for sound files. No subdirectories anymore. This eliminates the need for iteration by only testing if the searched file exists.
1 parent e58fb6f commit 99ba8cf

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

yate/callgen.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import signal
55
import logging
6+
from pathlib import Path
67

78
from aiohttp import web
89

@@ -11,7 +12,6 @@
1112

1213
soundfile_extensions = [".slin", ".gsm"]
1314

14-
logging.basicConfig(level=logging.INFO)
1515

1616

1717
class SoundCallInfo:
@@ -29,7 +29,7 @@ def __init__(self, port, sounds_directory, bind_global=False):
2929

3030
self.active_calls = {}
3131
self.yate = YateAsync("127.0.0.1", port)
32-
self.sounds_directory = sounds_directory
32+
self.sounds_directories = sounds_directory
3333

3434
self.web_app = web.Application()
3535
self.web_app.add_routes([web.post("/call", self.web_call_handler)])
@@ -72,6 +72,7 @@ def shutdown(self):
7272
self.shutdown_future.set_result(True)
7373

7474
async def web_call_handler(self, request):
75+
logging.debug("TRACE: Request handler begin")
7576
params = await request.post()
7677

7778
soundfile = params.get("soundfile")
@@ -160,20 +161,26 @@ def drop_call_if_not_answered(self, id):
160161
self._drop_call(id)
161162

162163
def find_soundfile(self, name):
163-
for root, _, files in os.walk(self.sounds_directory):
164-
for f in files:
165-
fname, fext = os.path.splitext(f)
166-
if fname == name and fext in soundfile_extensions:
167-
return os.path.join(root, f)
168-
164+
for directory in self.sounds_directories:
165+
for ext in soundfile_extensions:
166+
test_path = Path(directory) / (name + ext)
167+
logging.debug("Testing %s for existence", test_path)
168+
if test_path.exists():
169+
return str(test_path)
169170

170171
def main():
171172
parser = argparse.ArgumentParser(description='Yate CLI to generate automated calls.')
172173
parser.add_argument("port", type=int, help="The port at which yate is listening")
173-
parser.add_argument("sounds_directory", type=str, help="The directory at which we find the sounds")
174+
parser.add_argument("sounds_directory", type=str, nargs="+", help="Directories at which we find the sounds")
174175
parser.add_argument("--bind_global", action="store_true")
176+
parser.add_argument("--trace", action="store_true", help="Enable debug tracing")
177+
175178

176179
args = parser.parse_args()
180+
if args.trace:
181+
logging.basicConfig(level=logging.DEBUG)
182+
else:
183+
logging.basicConfig(level=logging.INFO)
177184
app = YateCallGenerator(args.port, args.sounds_directory, args.bind_global)
178185
app.run()
179186

0 commit comments

Comments
 (0)