我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用time.perf_counter()。
def simulate_component(self, compID): """ Do simulation for the specified foreground component. """ logger.info("==================================================") logger.info(">>> Simulate component: %s <<<" % compID) logger.info("==================================================") t1_start = time.perf_counter() t2_start = time.process_time() comp_cls = COMPONENTS_ALL[compID] comp_obj = comp_cls(self.configs) comp_obj.preprocess() skyfiles = comp_obj.simulate() if self.products: self.products.add_component(compID, skyfiles) comp_obj.postprocess() t1_stop = time.perf_counter() t2_stop = time.process_time() logger.info("--------------------------------------------------") logger.info("Elapsed time: %.1f [min]" % ((t1_stop-t1_start)/60)) logger.info("CPU process time: %.1f [min]" % ((t2_stop-t2_start)/60)) logger.info("--------------------------------------------------")
def acquire(self, blocking=True, timeout=-1): """Must be used with 'yield' as 'yield lock.acquire()'. """ if not blocking and self._owner is not None: raise StopIteration(False) if not self._scheduler: self._scheduler = Pycos.scheduler() task = Pycos.cur_task(self._scheduler) if timeout < 0: timeout = None while self._owner is not None: if timeout is not None: if timeout <= 0: raise StopIteration(False) start = _time() self._waitlist.append(task) if (yield task._await_(timeout)) is None: try: self._waitlist.remove(task) except ValueError: pass if timeout is not None: timeout -= (_time() - start) self._owner = task raise StopIteration(True)
def check_cooldowns(self, userid, action, settings): path = settings["Config"][action] if abs(settings["Players"][userid][action] - int(time.perf_counter())) >= path: settings["Players"][userid][action] = int(time.perf_counter()) dataIO.save_json(self.file_path, self.system) return True elif settings["Players"][userid][action] == 0: settings["Players"][userid][action] = int(time.perf_counter()) dataIO.save_json(self.file_path, self.system) return True else: s = abs(settings["Players"][userid][action] - int(time.perf_counter())) seconds = abs(s - path) remaining = self.time_formatting(seconds) await self.bot.say("This action has a cooldown. You still have:\n{}".format(remaining)) return False
def check_cooldowns(self, settings, userid): if abs(settings["Users"][userid]["Trade Cooldown"] - int(time.perf_counter())) \ >= settings["Config"]["Trade Cooldown"]: settings["Users"][userid]["Trade Cooldown"] = int(time.perf_counter()) dataIO.save_json(self.file_path, self.system) return "OK" elif settings["Users"][userid]["Trade Cooldown"] == 0: settings["Users"][userid]["Trade Cooldown"] = int(time.perf_counter()) dataIO.save_json(self.file_path, self.system) return "OK" else: s = abs(settings["Users"][userid]["Trade Cooldown"] - int(time.perf_counter())) seconds = abs(s - settings["Config"]["Trade Cooldown"]) msg = ("You must wait before trading again. " "You still have: {}".format(self.time_format(seconds))) return msg
def heist_game(self, settings, server, t_heist, t_crew, t_vault): crew = len(settings["Crew"]) target = self.heist_target(settings, crew) settings["Config"]["Heist Start"] = True players = [server.get_member(x) for x in settings["Crew"]] results = self.game_outcomes(settings, players, target) start_output = self.message_handler(settings, crew, players) await self.bot.say("Get ready! The {} is starting with {}\nThe {} has decided to " "hit **{}**.".format(t_heist, start_output, t_crew, target)) await asyncio.sleep(3) await self.show_results(settings, results) if settings["Crew"]: players = [server.get_member(x) for x in settings["Crew"]] data = self.calculate_credits(settings, players, target) headers = ["Players", "Credits Obtained", "Bonuses", "Total"] t = tabulate(data, headers=headers) msg = ("The credits collected from the {} was split among the winners:\n```" "C\n{}```".format(t_vault, t)) else: msg = "No one made it out safe." settings["Config"]["Alert Time"] = int(time.perf_counter()) self.reset_heist(settings) self.save_system() await self.bot.say(msg)
def shutdown_save(self): for server in self.system["Servers"]: death_time = self.system["Servers"][server]["Config"]["Death Timer"] for player in self.system["Servers"][server]["Players"]: player_death = self.system["Servers"][server]["Players"][player]["Death Timer"] player_sentence = self.system["Servers"][server]["Players"][player]["Time Served"] sentence = self.system["Servers"][server]["Players"][player]["Sentence"] if player_death > 0: s = abs(player_death - int(time.perf_counter())) seconds = abs(s - death_time) self.system["Servers"][server]["Players"][player]["Death Timer"] = seconds if player_sentence > 0: s = abs(player_sentence - int(time.perf_counter())) seconds = abs(s - sentence) self.system["Servers"][server]["Players"][player]["Time Served"] = seconds
def __init__(self, title = None, pathfile = None, debug_mode = True, debug_level = 0): self.path_file = pathfile self.debug_mode = debug_mode self.starttime = time.perf_counter() self.nowtime = time.perf_counter() self.lastcall = time.perf_counter() self.debug_level = debug_level # create file? # if not isfile(self.path_file): # with open(self.path_file, 'w') as f: # f.write("-init log file-") if title is not None: today = datetime.datetime.now() s = title + " program started the " + today.strftime("%d of %b %Y at %H:%M") self.log("=============================================================\n" + s + "\n=============================================================")
def __call__(self, func, *args, **kwargs): '''Used to process callbacks in throughput-limiting thread through queue. Args: func (:obj:`callable`): the actual function (or any callable) that is processed through queue. *args: variable-length `func` arguments. **kwargs: arbitrary keyword-arguments to `func`. Returns: None ''' if not self.is_alive() or self.__exit_req: raise DelayQueueError('Could not process callback in stopped thread') self._queue.put((func, args, kwargs)) # The most straightforward way to implement this is to use 2 sequenital delay # queues, like on classic delay chain schematics in electronics. # So, message path is: # msg --> group delay if group msg, else no delay --> normal msg delay --> out # This way OS threading scheduler cares of timings accuracy. # (see time.time, time.clock, time.perf_counter, time.sleep @ docs.python.org)
def benchmark(package_name): def decorator(func): def inner(): try: package = import_module(package_name) except ImportError: return 'not available.' start = perf_counter() for i in range(TIMES): func(package) end = perf_counter() return end - start return inner return decorator
def start_game(self): builtins.cfg = \ chess_config.Config(self.config_name.lower(), self.crazy_mode) self.game._reset() agent1_module = __import__(self.agent1 + "_ChessPlayer") white_opponent_class = getattr(agent1_module, self.agent1 + "_ChessPlayer") self.white_opponent = white_opponent_class(self.game.board,'white') agent2_module = __import__(self.agent2 + "_ChessPlayer") black_opponent_class = getattr(agent2_module, self.agent2 + "_ChessPlayer") self.black_opponent = black_opponent_class(self.game.board,'black') self.game.white_player = self.agent1 self.game.black_player = self.agent2 self.player_time = {'white':0.0,'black':0.0} self.timer = time.perf_counter() move = self.take_player_turn() self.attempt_to_make_move(self.game.board[move[0]], *move) while self.game.started: move = self.switch_player_turn() self.attempt_to_make_move(self.game.board[move[0]], *move)
def switch_player_turn(self, the_time): if self.do_replay: self.player_time[self.game.player_turn] = the_time else: time_against = time.perf_counter() - self.timer self.player_time[self.game.player_turn] += time_against self.player_time_label[self.game.player_turn]['text'] = \ '{:.1f} sec'.format(self.player_time[self.game.player_turn]) if self.player_time[self.game.player_turn] > cfg.TIME_LIMIT: self.player_time_label[self.game.player_turn]['foreground'] = 'red' self.turn_label['foreground'] = self.game.player_turn self.game.player_turn = \ 'black' if self.game.player_turn == 'white' else 'white' self.turn_label['text'] = self.game.player_turn.capitalize() self.turn_label['background'] = self.game.player_turn if not self.do_replay: if self.player_time[self.game.player_turn] > cfg.TIME_LIMIT: self.force_random_move() else: self.take_player_turn()
def query(sql, **kwargs): ti = time.perf_counter() _query = text(sql).execution_options(autocommit=False) try: res = conn.execute(_query, **kwargs) ms = int((time.perf_counter() - ti) * 1000) QueryStats.log(sql, ms) if ms > 100: disp = re.sub('\s+', ' ', sql).strip()[:250] print("\033[93m[SQL][{}ms] {}\033[0m".format(ms, disp)) logger.debug(res) return res except Exception as e: print("[SQL] Error in query {} ({})".format(sql, kwargs)) conn.close() logger.exception(e) raise e # n*m
def __exec(self, method, *params): time_start = time.perf_counter() tries = 0 while True: try: result = self._client.exec(method, *params) assert result, "empty response {}".format(result) except (AssertionError, RPCError) as e: tries += 1 print("{} failure, retry in {}s -- {}".format(method, tries, e)) time.sleep(tries) continue break batch_size = len(params[0]) if method == 'get_accounts' else 1 total_time = int((time.perf_counter() - time_start) * 1000) ClientStats.log("%s()" % method, total_time, batch_size) return result # perform batch call (if jussi is enabled, use batches; otherwise, multi)
def __exec_batch(self, method, params): time_start = time.perf_counter() result = None if self._jussi: tries = 0 while True: try: result = list(self._client.exec_batch(method, params, batch_size=500)) break except (AssertionError, RPCError) as e: tries += 1 print("batch {} failure, retry in {}s -- {}".format(method, tries, e)) time.sleep(tries) continue else: result = list(self._client.exec_multi_with_futures( method, params, max_workers=10)) total_time = int((time.perf_counter() - time_start) * 1000) ClientStats.log("%s()" % method, total_time, len(params)) return result
def cache_accounts(cls, accounts): from hive.indexer.cache import batch_queries processed = 0 total = len(accounts) for i in range(0, total, 1000): batch = accounts[i:i+1000] lap_0 = time.perf_counter() sqls = cls._generate_cache_sqls(batch) lap_1 = time.perf_counter() batch_queries(sqls) lap_2 = time.perf_counter() if len(batch) < 1000: continue processed += len(batch) rem = total - processed rate = len(batch) / (lap_2 - lap_0) pct_db = int(100 * (lap_2 - lap_1) / (lap_2 - lap_0)) print(" -- account {} of {} ({}/s, {}% db) -- {}m remaining".format( processed, total, round(rate, 1), pct_db, round(rem / rate / 60, 2)))
def update_ranks(cls): sql = """ UPDATE hive_accounts SET rank = r.rnk FROM (SELECT id, ROW_NUMBER() OVER (ORDER BY vote_weight DESC) as rnk FROM hive_accounts) r WHERE hive_accounts.id = r.id AND rank != r.rnk; """ query(sql) return # the following method is 10-20x slower id_weight = query_all("SELECT id, vote_weight FROM hive_accounts") id_weight = sorted(id_weight, key=lambda el: el[1], reverse=True) print("Updating account ranks...") lap_0 = time.perf_counter() query("START TRANSACTION") for (i, (_id, _)) in enumerate(id_weight): query("UPDATE hive_accounts SET rank=%d WHERE id=%d" % (i+1, _id)) query("COMMIT") lap_1 = time.perf_counter() print("Updated %d ranks in %ds" % (len(id_weight), lap_1 - lap_0))
def ws_connect(self, url: str, timeout: float = None ) -> WebSocketClientConnection: """Make WebSocket connection to the url. Retries up to _max (default: 20) times. Client connections made by this method are closed after each test method. """ st = time.perf_counter() timeout = timeout or self.timeout while (time.perf_counter() - st) < timeout: try: ws = await to_asyncio_future(websocket_connect(url)) except ConnectionRefusedError: await self.wait() continue else: self._ws_connections.append(ws) return ws raise ConnectionRefusedError( 'WebSocket connection refused: {}'.format(url))
def do_work(job): tstart = time.perf_counter() status_code = None timeout = False connection_error = False expect_not_present = False try: result = requests.get(job.get('url'), timeout=job.get('timeout')) status_code = result.status_code if job.get('expect'): if job.get('expect') not in result.text: expect_not_present = True except ReadTimeout: timeout = True except socket_error: connection_error = True return {'status_code': status_code, 'ms': (time.perf_counter()-tstart)*1000, 'timeout': timeout, 'connection_error': connection_error, 'tstart': tstart, 'tstop': time.perf_counter(), 'expect_not_present': expect_not_present}
def try_with_timeout(target, args, timeout, pause): """ Attempt an operation as long as it fails up to the given timeout. :param target: The callable :param args: The arguments to pass :param timeout: the timeout :param pause: how long to pause between each failed invocation :return: either the result from successfully invoking the callable raises the exception thrown by the callable on timeout. """ end = time.perf_counter()+timeout result, exception = try_invoke(target, args) while exception is not None and time.perf_counter()<end: time.sleep(pause) result, exception = try_invoke(target, args) if exception is not None: raise exception return result
def upload_blocks(bucket, chunk_size, max_threads, lines): session = botocore.session.get_session() client = session.create_client('s3') start = time.perf_counter() futures = [] with ThreadPoolExecutor(max_workers=max_threads) as executor: # Start the load operations and mark each future with its URL for line in lines: raw_block, key = load_json_block(line) futures.append(executor.submit(client.put_object,Bucket=bucket, Key=key, Body=raw_block, ContentEncoding='UTF-8', ContentType='application/json')) end = time.perf_counter() done, pending = concurrent.futures.wait(futures) complete = time.perf_counter() rate = 1 / ((complete - start) / len(done)) return len(done), int(rate)
def __init__(self, signal_in=None, name="layer", print_fps=False, print_fps_every=timedelta(seconds=5), *args, **kwargs): super().__init__(*args, **kwargs) self.name = name self.counter = 0 self.signal = None self.is_first = True self.stop_event = None self.signal_in = None self.set_signal_in(signal_in) self.count = 0 self.start_time = None self.reset() self.print_fps_every = print_fps_every self.print_fps = print_fps self.fps = 0 self.last_tick_time = time.perf_counter()
def runPutTest(testDataPath, testDataRangeStart, testDataRangeEnd, f): log.debug('running put tests...') timeStart = time.perf_counter() times = [time.perf_counter()] for i in range(testDataRangeStart, testDataRangeEnd): print(i) thisPath = '%s/%i' % (testDataPath, i) o = loadTestData(thisPath) f.putObject(o, str(i)) times.append(time.perf_counter()) timeEnd = time.perf_counter() log.warning('RESULT (PUT): total test runtime: %s seconds, mean per object: %s' % ( timeEnd - timeStart, ((timeEnd - timeStart) / testDataRangeEnd))) log.critical('RESULT (PUT): median result: %s ' % statistics.median(calculateTimeDeltas(times))) log.critical('RESULT (PUT): standard deviation result: %s ' % statistics.stdev(calculateTimeDeltas(times))) log.critical('RESULT (PUT): mean result: %s ' % statistics.mean(calculateTimeDeltas(times))) # log.critical('RESULT (PUT): individual times: %s ' % (calculateTimeDeltas(times)))
def runGetTest(testDataPath, testDataRangeStart, testDataRangeEnd, f): log.debug('running get tests...') timeStart = time.perf_counter() times = [time.perf_counter()] for i in range(testDataRangeStart, testDataRangeEnd): thisPath = '%s/%i' % (testDataPath, i) o = f.getObject(str(i)) saveTestData(o, thisPath) times.append(time.perf_counter()) timeEnd = time.perf_counter() log.critical('RESULT (GET): total test runtime: %s seconds, mean per object: %s' % ( timeEnd - timeStart, ((timeEnd - timeStart) / testDataRangeEnd))) log.critical('RESULT (GET): median result: %s ' % statistics.median(calculateTimeDeltas(times))) log.critical('RESULT (GET): standard deviation result: %s ' % statistics.stdev(calculateTimeDeltas(times))) log.critical('RESULT (GET): mean result: %s ' % statistics.mean(calculateTimeDeltas(times))) # log.critical('RESULT (GET): individual times: %s ' % (calculateTimeDeltas(times)))
def runDeleteTest(testDataRangeStart, testDataRangeEnd, f): log.debug('running delete tests...') timeStart = time.perf_counter() times = [time.perf_counter()] for i in range(testDataRangeStart, testDataRangeEnd): f.deleteObject(str(i)) times.append(time.perf_counter()) timeEnd = time.perf_counter() log.critical('RESULT (DELETE): total test runtime: %s seconds, mean per object: %s' % ( timeEnd - timeStart, ((timeEnd - timeStart) / testDataRangeEnd))) log.critical('RESULT (DELETE): median result: %s ' % statistics.median(calculateTimeDeltas(times))) log.critical('RESULT (DELETE): standard deviation result: %s ' % statistics.stdev(calculateTimeDeltas(times))) log.critical('RESULT (DELETE): mean result: %s ' % statistics.mean(calculateTimeDeltas(times))) # log.critical('RESULT (DELETE): individual times: %s ' % (calculateTimeDeltas(times))) ############################################################################### ###############################################################################
def gain_xp(self, message): user = message.author id = user.id if self.check_joined(id): if id in self.gettingxp: seconds = abs(self.gettingxp[id] - int(time.perf_counter())) if seconds >= self.cooldown: self.add_xp(id) self.gettingxp[id] = int(time.perf_counter()) fileIO("data/levels/leader_board.json", "save", self.leader_board) if self.leader_board[user.id]["XP"] >= self.get_level_xp(self.leader_board[user.id]["rank"]): self.leader_board[user.id]["rank"] += 1 self.leader_board[user.id]["XP"] = 0 msg = '{} **has leveled up and is now level {}!!!\n HURRAY!!**' msg = msg.format(message.author.display_name, self.leader_board[user.id]["rank"]) await self.bot.send_message(message.channel, msg) fileIO("data/levels/leader_board.json", "save", self.leader_board) else: self.add_xp(id) self.gettingxp[id] = int(time.perf_counter()) fileIO("data/levels/leader_board.json", "save", self.leader_board)
def rate_limit(wait_length): last_time = 0 def decorate(f): @wraps(f) def rate_limited(*args, **kwargs): nonlocal last_time diff = perf_counter() - last_time if diff < wait_length: sleep(wait_length - diff) r = f(*args, **kwargs) last_time = perf_counter() return r return rate_limited return decorate
def eventuallyAny(coroFunc, *args, retryWait: float = 0.01, timeout: float = 5): start = time.perf_counter() def remaining(): return start + timeout - time.perf_counter() remain = remaining() data = None while remain >= 0: res = await coroFunc(*args) (complete, data) = res if complete: return data remain = remaining() if remain > 0: await asyncio.sleep(retryWait) remain = remaining() return data
def put_load(): port = genHa()[1] ha = HA('0.0.0.0', port) name = "hello" wallet = Wallet(name) wallet.addIdentifier( signer=DidSigner(seed=b'000000000000000000000000Steward1')) client = Client(name, ha=ha) with Looper(debug=getConfig().LOOPER_DEBUG) as looper: looper.add(client) print('Will send {} reqs in all'.format(numReqs)) requests = sendRandomRequests(wallet, client, numReqs) start = perf_counter() for i in range(0, numReqs, numReqs // splits): print('Will wait for {} now'.format(numReqs // splits)) s = perf_counter() reqs = requests[i:i + numReqs // splits + 1] waitForSufficientRepliesForRequests(looper, client, requests=reqs, customTimeoutPerReq=100, override_timeout_limit=True) print('>>> Got replies for {} requests << in {}'. format(numReqs // splits, perf_counter() - s)) end = perf_counter() print('>>>Total {} in {}<<<'.format(numReqs, end - start)) exit(0)
def simulate(self): """ Do simulation for all enabled components. """ timers = [] for compID in self.componentsID: t1 = time.perf_counter() self.simulate_component(compID) t2 = time.perf_counter() timers.append((compID, t1, t2)) logger.info("==================================================") logger.info(">>> Time usage <<<") logger.info("==================================================") for compID, t1, t2 in timers: logger.info("%s : %.1f [min]" % (compID, (t2-t1)/60)) logger.info("--------------------------------------------------")
def _task_test(self, **kwargs): """ Test task ... """ import time t1_start = time.perf_counter() t2_start = time.process_time() logger.info("Console TEST task: START ...") for i in range(kwargs["time"]): logger.info("Console TEST task: slept {0} seconds ...".format(i)) time.sleep(1) logger.info("Console TEST task: DONE!") t1_stop = time.perf_counter() t2_stop = time.process_time() logger.info("Elapsed time: {0:.3f} (s)".format(t1_stop - t1_start)) logger.info("CPU process time: {0:.3f} (s)".format(t2_stop - t2_start)) return (True, None)
def __call__(self, masker, dict_fact, cpu_time, io_time): test_time = time.perf_counter() if not hasattr(self, 'data'): self.data = masker.transform(self.test_imgs, confounds=self.test_confounds) scores = np.array([dict_fact.score(data) for data in self.data]) len_imgs = np.array([data.shape[0] for data in self.data]) score = np.sum(scores * len_imgs) / np.sum(len_imgs) self.test_time += time.perf_counter() - test_time this_time = time.perf_counter() - self.start_time - self.test_time self.score.append(score) self.time.append(this_time) self.cpu_time.append(cpu_time) self.io_time.append(io_time) self.iter.append(dict_fact.n_iter_) if self.info is not None: self.info['time'] = self.cpu_time self.info['score'] = self.score self.info['iter'] = self.iter
def sync(self): """Synchronize data from library and actual data in the musics folder Raises: ValueError: When musics_folder is not set """ if self.musics_folder is None or not pathlib.Path(self.musics_folder).is_dir(): raise ValueError('Invalid music folder: ' + str(self.musics_folder)) self.logger.info(f"Scanning {self.musics_folder}") start = time.perf_counter() self.__sync_songs() self.__sync_artists() self.__sync_albums() self.__sync_playlists() end = time.perf_counter() self.logger.info('Scan ended in {:.3f}'.format(end - start))
def __create_model(self): self.logger.info('Creating ListStore') start = time.perf_counter() model = AdapterSong.create_store() order = AbstractPlaylist.OrderBy[self.userconfig['grid']['sort']['field']] desc = self.userconfig['grid']['sort']['desc'] songs = self.current_playlist.collections(order, desc) for row in songs: model.insert_with_valuesv(-1, AdapterSong.create_col_number(), AdapterSong.create_row(row)) GObject.idle_add(lambda: self.__create_model_finished(model)) end = time.perf_counter() self.logger.info('ListStore created in {:.3f} seconds'.format(end - start))
def ping(self, ctx): t1 = time.perf_counter() await ctx.trigger_typing() t2 = time.perf_counter() result = round((t2-t1)*1000) if int(result) >=200: em = discord.Embed(title="Ping : " + str(result) + "ms", description="... c'est quoi ce ping !", colour=0xFF1111) await ctx.send(embed=em) elif int(result) > 100 and int(result) < 200: em = discord.Embed(title="Ping : " + str(result) + "ms", description="Ca va, ça peut aller, mais j'ai l'impression d'avoir 40 ans !", colour=0xFFA500) await ctx.send(embed=em) elif int(result) <= 100: em = discord.Embed(title="Ping : " + str(result) + "ms", description="Wow c'te vitesse de réaction, je m'épate moi-même !",colour=0x11FF11) await ctx.send(embed=em) ##INFO##
def ping(self, ctx): """Your average ping command.""" # Set the embed for the pre-ping clock = random.randint(0x1F550, 0x1F567) # pick a random clock embed = discord.Embed(colour=0xFFC107) embed.set_author(name=random.choice(PRE_PING_REMARKS), icon_url=emoji_url(chr(clock))) # Do the classic ping start = time.perf_counter() # fuck time.monotonic() message = await ctx.send(embed=embed) end = time.perf_counter() # fuck time.monotonic() ms = (end - start) * 1000 # Edit the embed to show the actual ping embed.colour = 0x4CAF50 embed.set_author(name='Poing!', icon_url=emoji_url('\U0001f3d3')) embed.add_field(name='Latency', value=f'{ctx.bot.latency * 1000 :.0f} ms') embed.add_field(name='Classic', value=f'{ms :.0f} ms', inline=False) await message.edit(embed=embed)
def load_questions(self, msg): msg = msg.split(" ") if len(msg) == 2: _, qlist = msg if qlist == "random": chosen_list = randchoice(glob.glob("data/trivia/*.txt")) self.question_list = self.load_list(chosen_list) self.status = "new question" self.timeout = time.perf_counter() if self.question_list: await self.new_question() else: if os.path.isfile("data/trivia/" + qlist + ".txt"): self.question_list = await self.load_list("data/trivia/" + qlist + ".txt") self.status = "new question" self.timeout = time.perf_counter() if self.question_list: await self.new_question() else: await trivia_manager.bot.say("There is no list with that name.") await self.stop_trivia() else: await trivia_manager.bot.say("trivia [list name]")
def check_answer(self, message): if message.author.id != trivia_manager.bot.user.id: self.timeout = time.perf_counter() if self.current_q is not None: for answer in self.current_q["ANSWERS"]: if answer in message.content.lower(): self.current_q["ANSWERS"] = [] self.status = "correct answer" self.add_point(message.author.name) msg = "You got it {}! **+1** to you!".format(message.author.name) try: await trivia_manager.bot.send_typing(self.channel) await trivia_manager.bot.send_message(message.channel, msg) except: await asyncio.sleep(0.5) await trivia_manager.bot.send_message(message.channel, msg) return True
def game(): # Controller page time_full = time.perf_counter() player_ip = request.remote_addr # To redirect players who isent in the ip list and has thearfor ni team redirect_var = True for i in players: if player_ip == i.ip: print("OK") redirect_var = False if redirect_var: return redirect(url_for('index')) team_var = get_team(player_ip) direction_var = None if request.method == 'POST': # Adds a request to move the robot in the multithread queue q.put(request.form['submit']) print(time.perf_counter() - time_full) return render_template('game.html',team=team_var, direction=direction_var)
def test_get_clock_info(self): clocks = ['clock', 'perf_counter', 'process_time', 'time'] if hasattr(time, 'monotonic'): clocks.append('monotonic') for name in clocks: info = time.get_clock_info(name) #self.assertIsInstance(info, dict) self.assertIsInstance(info.implementation, str) self.assertNotEqual(info.implementation, '') self.assertIsInstance(info.monotonic, bool) self.assertIsInstance(info.resolution, float) # 0.0 < resolution <= 1.0 self.assertGreater(info.resolution, 0.0) self.assertLessEqual(info.resolution, 1.0) self.assertIsInstance(info.adjustable, bool) self.assertRaises(ValueError, time.get_clock_info, 'xxx')
def write_pages(self, pages): sums = defaultdict(float) counts = defaultdict(int) # group by type by_type = defaultdict(list) for page in pages: by_type[(page.RENDER_PREFERRED_ORDER, page.TYPE)].append(page) # Render collecting timing statistics for (order, type), pgs in sorted(by_type.items(), key=lambda x:x[0][0]): start = time.perf_counter() for page in pgs: contents = page.render() for relpath, rendered in contents.items(): dst = self.output_abspath(relpath) rendered.write(dst) end = time.perf_counter() sums[type] = end - start counts[type] = len(pgs) return sums, counts
def launch(self): """Ask JupyterHub to launch the image.""" await self.emit({ 'phase': 'launching', 'message': 'Launching server...\n', }) launcher = self.settings['launcher'] username = launcher.username_from_repo(self.repo) try: launch_starttime = time.perf_counter() server_info = await launcher.launch(image=self.image_name, username=username) LAUNCH_TIME.labels(status='success').observe(time.perf_counter() - launch_starttime) except: LAUNCH_TIME.labels(status='failure').observe(time.perf_counter() - launch_starttime) raise event = { 'phase': 'ready', 'message': 'server running at %s\n' % server_info['url'], } event.update(server_info) await self.emit(event)
def generate_output(args): if not args.test: args.test = glob_with_format(args.format) # by default tests = construct_relationship_of_files(args.test, args.format) for name, it in sorted(tests.items()): log.emit('') log.info('%s', name) if 'out' in it: log.info('output file already exists.') log.info('skipped.') continue with open(it['in']) as inf: begin = time.perf_counter() answer, proc = utils.exec_command(args.command, shell=args.shell, stdin=inf) end = time.perf_counter() log.status('time: %f sec', end - begin) if proc.returncode != 0: log.failure(log.red('RE') + ': return code %d', proc.returncode) log.info('skipped.') continue log.emit(log.bold(answer.decode().rstrip())) path = path_from_format(args.format, match_with_format(args.format, it['in']).groupdict()['name'], 'out') with open(path, 'w') as fh: fh.buffer.write(answer) log.success('saved to: %s', path)
def _recorder(pid, stop, ival): """Subprocess call function to record cpu and memory.""" t = t0 = time() process = psutil.Process(pid) if stop is None: while True: m = process.memory_info() print(psutil.cpu_percent(), ',', m[0], ',', m[1]) sleep(ival) t = time() else: while t - t0 < stop: m = process.memory_info() print(psutil.cpu_percent(), ',', m[0], ',', m[1]) sleep(ival) t = time()
def fit(self, path=None): """Fit sub-learner""" if path is None: path = self.path t0 = time() transformers = self._load_preprocess(path) self._fit(transformers) if self.out_array is not None: self._predict(transformers, self.scorer is not None) o = IndexedEstimator(estimator=self.estimator, name=self.name_index, index=self.index, in_index=self.in_index, out_index=self.out_index, data=self.data) save(path, self.name_index, o) if self.verbose: msg = "{:<30} {}".format(self.name_index, "done") f = "stdout" if self.verbose < 10 - 3 else "stderr" print_time(t0, msg, file=f)
def _predict(self, transformers, score_preds): """Sub-routine to with sublearner""" n = self.in_array.shape[0] # For training, use ytemp to score predictions # During test time, ytemp is None xtemp, ytemp = slice_array(self.in_array, self.targets, self.out_index) t0 = time() if transformers: xtemp, ytemp = transformers.transform(xtemp, ytemp) predictions = getattr(self.estimator, self.attr)(xtemp) self.pred_time_ = time() - t0 # Assign predictions to matrix assign_predictions(self.out_array, predictions, self.out_index, self.output_columns, n) # Score predictions if applicable if score_preds: self.score_ = score_predictions( ytemp, predictions, self.scorer, self.name_index, self.name)
def fit(self, path=None): """Fit transformers""" path = path if path else self.path t0 = time() xtemp, ytemp = slice_array( self.in_array, self.targets, self.in_index) t0_f = time() self.estimator.fit(xtemp, ytemp) self.transform_time_ = time() - t0_f if self.out_array is not None: self._transform() o = IndexedEstimator(estimator=self.estimator, name=self.name_index, index=self.index, in_index=self.in_index, out_index=self.out_index, data=self.data) save(path, self.name_index, o) if self.verbose: f = "stdout" if self.verbose < 10 else "stderr" msg = "{:<30} {}".format(self.name_index, "done") print_time(t0, msg, file=f)
def fit(self, path=None): """Evaluate sub-learner""" path = path if path else self.path if self.scorer is None: raise ValueError("Cannot generate CV-scores without a scorer") t0 = time() transformers = self._load_preprocess(path) self._fit(transformers) self._predict(transformers) o = IndexedEstimator(estimator=self.estimator, name=self.name_index, index=self.index, in_index=self.in_index, out_index=self.out_index, data=self.data) save(path, self.name_index, o) if self.verbose: f = "stdout" if self.verbose else "stderr" msg = "{:<30} {}".format(self.name_index, "done") print_time(t0, msg, file=f)
def _score_preds(self, transformers, index): # Train scores xtemp, ytemp = slice_array(self.in_array, self.targets, index) if transformers: xtemp, ytemp = transformers.transform(xtemp, ytemp) t0 = time() if self.error_score is not None: try: scores = self.scorer(self.estimator, xtemp, ytemp) except Exception as exc: # pylint: disable=broad-except warnings.warn( "Scoring failed. Setting error score %r." "Details:\n%r" % (self.error_score, exc), FitFailedWarning) scores = self.error_score else: scores = self.scorer(self.estimator, xtemp, ytemp) pred_time = time() - t0 return scores, pred_time
def answer_with_doc(self, question: str, doc: str) -> Tuple[np.ndarray, List[WebParagraph]]: """ Answer a question using the given text as a document """ self.log.info("Answering question \"%s\" with a given document" % question) # Tokenize question = self.tokenizer.tokenize_paragraph_flat(question) context = [self.tokenizer.tokenize_with_inverse(x, False) for x in self._split_regex.split(doc)] # Split into super-paragraphs context = self._split_document(context, "User", None) # Select top paragraphs context = self.paragraph_selector.prune(question, context) if len(context) == 0: raise ValueError("Unable to process documents") # Select the top answer span t0 = time.perf_counter() span_scores = self._get_span_scores(question, context) self.log.info("Computing answer spans took %.5f seconds" % (time.perf_counter() - t0)) return span_scores
def loadQuestions(self, msg): msg = msg.split(" ") if len(msg) == 2: _, qlist = msg if qlist == "random": chosenList = choice(glob.glob("trivia/*.txt")) self.questionList = self.loadList(chosenList) self.status = "new question" self.timeout = time.perf_counter() if self.questionList: await self.newQuestion() else: if os.path.isfile("trivia/" + qlist + ".txt"): self.questionList = self.loadList("trivia/" + qlist + ".txt") self.status = "new question" self.timeout = time.perf_counter() if self.questionList: await self.newQuestion() else: await client.send_message(self.channel, "`There is no list with that name.`") await self.stopTrivia() else: await client.send_message(self.channel, "`" + settings["PREFIX"] + "trivia [list name]`")