From f162ae0de0f71391c56957389cc3c8babc8022e1 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 19 Apr 2015 16:37:00 +0200 Subject: Use with statement --- tests/APIExerciser.py | 54 ++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'tests/APIExerciser.py') diff --git a/tests/APIExerciser.py b/tests/APIExerciser.py index d17f81ae2..d7300b1a5 100644 --- a/tests/APIExerciser.py +++ b/tests/APIExerciser.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import with_statement + import string from threading import Thread from random import choice, sample, randint @@ -60,32 +62,32 @@ class APIExerciser(Thread): self.core.log.info("API Excerciser started %d" % self.id) - out = open("error.log", "ab") - # core errors are not logged of course - out.write("\n" + "Starting\n") - out.flush() - - while True: - try: - self.testAPI() - except Exception: - self.core.log.error("Excerciser %d throw an execption" % self.id) - print_exc() - out.write(format_exc() + 2 * "\n") - out.flush() - - if not self.count % 100: - self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count)) - if not self.count % 1000: - out.flush() - - if not sumCalled % 1000: #: not thread safe - self.core.log.info("Exercisers tested %d api calls" % sumCalled) - persec = sumCalled / (time() - self.time) - self.core.log.info("Approx. %.2f calls per second." % persec) - self.core.log.info("Approx. %.2f ms per call." % (1000 / persec)) - self.core.log.info("Collected garbage: %d" % gc.collect()) - # sleep(random() / 500) + with open("error.log", "ab") as out: + # core errors are not logged of course + out.write("\n" + "Starting\n") + out.flush() + + while True: + try: + self.testAPI() + except Exception: + self.core.log.error("Excerciser %d throw an execption" % self.id) + print_exc() + out.write(format_exc() + 2 * "\n") + out.flush() + + if not self.count % 100: + self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count)) + if not self.count % 1000: + out.flush() + + if not sumCalled % 1000: #: not thread safe + self.core.log.info("Exercisers tested %d api calls" % sumCalled) + persec = sumCalled / (time() - self.time) + self.core.log.info("Approx. %.2f calls per second." % persec) + self.core.log.info("Approx. %.2f ms per call." % (1000 / persec)) + self.core.log.info("Collected garbage: %d" % gc.collect()) + # sleep(random() / 500) def testAPI(self): -- cgit v1.2.3 From e1c22fad3fffb485da400e4b34c094f201a2c72e Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 19 Apr 2015 18:03:46 +0200 Subject: range -> xrange --- tests/APIExerciser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/APIExerciser.py') diff --git a/tests/APIExerciser.py b/tests/APIExerciser.py index d7300b1a5..f4b082479 100644 --- a/tests/APIExerciser.py +++ b/tests/APIExerciser.py @@ -17,7 +17,7 @@ from pyload.remote.thriftbackend.ThriftClient import ThriftClient, Destination def createURLs(): """ create some urls, some may fail """ urls = [] - for x in range(0, randint(20, 100)): + for x in xrange(0, randint(20, 100)): name = "DEBUG_API" if randint(0, 5) == 5: name = "" #: this link will fail @@ -34,7 +34,7 @@ sumCalled = 0 def startApiExerciser(core, n): - for _i in range(n): + for _i in xrange(n): APIExerciser(core).start() -- cgit v1.2.3