for x in range(0, 3000): alert = [] for y in range(0, 6): alert.append(repr(randint(0, 8 - 1)) + "test") for c in hcombinations: label = [] key = alert[:] for x in range(2): key[c[x]] = "*" label.append(alert[c[x]]) hyperkey = tuple(key) hlabel = tuple(label) if hyperkey in hyper_dict: hyper_dict[hyperkey].nalerts += 1 result = hyper_dict[hyperkey].get_alert(hlabel) if result is not None: hyper_dict[hyperkey].insert_alert(hlabel, result + 1) else: hyper_dict[hyperkey].insert_alert(hlabel, 1) else: hyper_dict[hyperkey] = Hyperedge(key, c, hlabel) for hyperedge in hyper_dict.values(): hypersize_list.insert((hyperedge.nalerts, hyperedge.hyperkey), hyperedge) while not hypersize_list.is_empty(): (key, hyperedge) = hypersize_list.pop_max() hyperedge.foreach_alert(treeloop) print("Completed iteration %d of 10" % z)
alert = [] for y in range(0, 6): alert.append(repr(randint(0, 8 - 1)) + "test") for c in hcombinations: label = [] key = alert[:] for x in range(2): key[c[x]] = '*' label.append(alert[c[x]]) hyperkey = tuple(key) hlabel = tuple(label) if hyperkey in hyper_dict: hyper_dict[hyperkey].nalerts += 1 result = hyper_dict[hyperkey].get_alert(hlabel) if result is not None: hyper_dict[hyperkey].insert_alert(hlabel, result + 1) else: hyper_dict[hyperkey].insert_alert(hlabel, 1) else: hyper_dict[hyperkey] = Hyperedge(key, c, hlabel) for hyperedge in hyper_dict.values(): hypersize_list.insert((hyperedge.nalerts, hyperedge.hyperkey), hyperedge) while not hypersize_list.is_empty(): (key, hyperedge) = hypersize_list.pop_max() hyperedge.foreach_alert(treeloop) print("Completed iteration %d of 10" % z)
class MINPolicy(object): def __init__(self, cache_size_limit, trace, csv_suffix=".csv"): self.cache_size_limit = cache_size_limit self.cache = {} self.hits = 0.0 self.requests = 0.0 self.ts_order = ['row', 'hit'] self.ts_datapoint = {key: None for key in self.ts_order} self.ts_datapoint['row'] = 0 self.ts_file = open("csv/min" + csv_suffix, "w") self.ts_writer = csv.writer(self.ts_file) self.ts_writer.writerow(self.ts_order) self.clairvoyance = FastRBTree() self.precog = FastRBTree() last_time = time.time() for i, page_opcode in enumerate(trace): if time.time() > last_time + 0.1: last_time = time.time() print '1', i, '\r', sys.stdout.flush() page, _ = page_opcode try: self.precog[page].append(i) except KeyError: self.precog[page] = collections.deque() self.precog[page].append(i) known_max = i known_max += 2 for times in self.precog.values(): times.append(known_max) known_max += 1 print print 'Done loading.' def hit_rate(self): return self.hits / self.requests def request(self, page): self.requests += 1 if page in self.cache: was_hit = True self.hits += 1 else: was_hit = False self.cache[page] = self.precog[page].popleft() # This happens on startup. if self.cache[page] < self.requests: self.cache[page] = self.precog[page].popleft() self.clairvoyance[self.cache[page]] = page self.ts_datapoint['row'] += 1 if was_hit: self.ts_datapoint['hit'] = 1 else: self.ts_datapoint['hit'] = 0 self.ts_writer.writerow( [self.ts_datapoint[key] for key in self.ts_order]) self.ts_file.flush() if len(self.cache) > self.cache_size_limit: next_use, page = self.clairvoyance.pop_max() del self.cache[page]