def __init__(self, log, bindport, node_id): Greenlet.__init__(self) self.log = log self.dstaddr = '0.0.0.0' self.bindport = bindport self.node_id = node_id self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(('', bindport)) self.last_sent = 0 self.dht_cache = LRU.LRU(100000) self.dht_router = DHTRouter(self) self.taskman = DHTTaskManager() self.log.write(self.dstaddr + ':' + str(self.bindport) + " open DHT") # add ourselves to the router node_msgobj = codec_pb2.MsgDHTNode() node_msgobj.node_id = node_id node_msgobj.ip = socket.inet_pton(socket.AF_INET, "127.0.0.1") node_msgobj.port = bindport rc = self.dht_router.add_node(node_msgobj) if rc < 0: self.log.write("DHT: failed to add node %s %d" % ("127.0.0.1", bindport))
class LRUTest(unittest.TestCase): cache = LRU.LRU() def test_0_set_name(self): self.cache.put(0) self.assertEqual(self.cache.get_cache(), [0]) self.cache.put(1) self.cache.put(2) self.cache.put(3) self.cache.put(4) self.cache.put(5) self.assertEqual(self.cache.get_cache(), [1, 2, 3, 4, 5]) self.cache.put(3) self.assertEqual(self.cache.get_cache(), [1, 2, 4, 5, 3]) self.cache.put(2) self.assertEqual(self.cache.get_cache(), [1, 4, 5, 3, 2]) def test_1_set_name(self): self.cache.get(3) self.assertEqual(self.cache.get_cache(), [1, 4, 5, 2]) self.cache.get(3) self.assertEqual(self.cache.get_cache(), [1, 4, 5]) self.cache.put(10) self.cache.put(11) self.cache.put(12) self.assertEqual(self.cache.get_cache(), [4, 5, 10, 11, 12]) self.cache.put(5) self.assertEqual(self.cache.get_cache(), [4, 10, 11, 12, 5])
def check(self, fpVector, md5): #print fpVector.shape assert fpVector.shape[0] == 1 scoreMatrix = computeFPScoreMatrix(fpVector, self.fpMatrix) matchMatrix = scoreMatrix >= 0.9 i = 0 while i < self.size: if matchMatrix[0, i]: break i += 1 if i < self.size: self.hits += 1 self.cache.remove(self.KMap[i]) self.cache.addFirst(self.KMap[i]) else: self.miss += 1 if i < self.capacity: key = i else: key = self.cache.tail.key self.cache.removeLast() self.size -= 1 self.fpMatrix[:, key] = np.transpose(fpVector[0]) node = LRU.Node(key, md5) self.KMap[key] = node self.cache.addFirst(node) self.size += 1
def __init__(self, capacity): self.capacity = capacity self.size = 0 self.fpMatrix = np.zeros((240007, capacity)) self.KMap = {} self.cache = LRU.DoubleLinkedList() self.hits = 0 self.miss = 0 self.batchhits = 0 self.batchmiss = 0
def on_click_shou(self): print("INF-self.suanfa=1") try: if self.suanfa == 1: print(self.kuaishu + self.yemian) arr_str = str.split(self.yemian) arr_int = [int(i) for i in arr_str] print(arr_int) print("INF==" + str(arr_int) + str(self.kuaishu)) fifo = FIFO.main(arr_int, int(self.kuaishu)) res = fifo.get_res() print(res) self.res.setText(res) self.res.adjustSize() self.setcilv(fifo.get_lv(), fifo.get_ci()) elif self.suanfa == 2: print(self.kuaishu + self.yemian) arr_str = str.split(self.yemian) arr_int = [int(i) for i in arr_str] print(arr_int) print("INF==" + str(arr_int) + str(self.kuaishu)) fifo = LRU.main(arr_int, int(self.kuaishu)) res = fifo.get_res() print(res) self.res.setText(res) self.res.adjustSize() self.setcilv(fifo.get_lv(), fifo.get_ci()) elif self.suanfa == 3: print(self.kuaishu + self.yemian) arr_str = str.split(self.yemian) arr_int = [int(i) for i in arr_str] print(arr_int) print("INF==" + str(arr_int) + str(self.kuaishu)) fifo = OPT.OPT(int(self.kuaishu), arr_int) res = fifo.get_res() print(res) self.res.setText(res) self.res.adjustSize() self.setcilv(fifo.get_lv(), fifo.get_ci()) except: print("ERROR-kong") self.error.show() self.timer = QTimer() self.num = 0 self.timer.timeout.connect(self.hideerror) self.timer.start(1500)
def lru_testing(): new_lru = lru.LRU(10) new_lru.put(1, "Test") new_lru.put(2, "Opp") new_lru.put(3, "Test") new_lru.put(4, "opp") new_lru.put(5, "testtt") new_lru.put(6, "asdsad") new_lru.put(8, "ddd") new_lru.put(9, "3323") new_lru.put(10, "Last") new_lru.put(11, "sdds") new_lru.put(11, "sddsss") new_lru.put(8, "wiped") new_lru.put(9, "wiped") new_lru.put(6, "wiped") new_lru.put(11, "wiped") new_lru.put(6, "testssss") new_lru.put(12, "1222") empty = new_lru.get(12) new_lru.print_inorder()
def main(): while (True): input_data = list(map(int, input("\n값 입력 : ").split())) slotcount = int(input("slotcount : ")) starttime = datetime.now() LRU.LRU_implement(input_data, slotcount) print("LRU 수행 시간 : ", (datetime.now() - starttime).microseconds) starttime = datetime.now() FIFO.FIFO_implement(input_data, slotcount) print("FIFO 수행 시간 : ", (datetime.now() - starttime).microseconds) starttime = datetime.now() LFU.LFU_implement(input_data, slotcount) print("LFU 수행 시간 : ", (datetime.now() - starttime).microseconds) starttime = datetime.now() RND.Random_implement(input_data, slotcount) print("RND 수행 시간 : ", (datetime.now() - starttime).microseconds) starttime = datetime.now() NEW.NEW_implement(input_data, slotcount) print("NEW 수행 시간 : ", (datetime.now() - starttime).microseconds)
def on_click_suiji(self): print("INF-suiji" + str(self.suanfa)) # 范围 try: if self.suanfa == 1: arr_fanwei = str.split(self.suiji_num) start = arr_fanwei[0] end = arr_fanwei[1] size = self.suiji_size shu = self.neicunkuaishu arr = [] print(start) print(end) print(size) print(shu) for i in range(0, int(size)): arr.append(random.randint(int(start), int(end))) print(arr) fifo = FIFO.main(arr, int(shu)) res = fifo.get_res() print(res) self.res.setText(res) self.res.adjustSize() print("缺页次数===" + str(fifo.get_ci())) print("缺页率===" + str(fifo.get_lv())) self.setcilv(fifo.get_lv(), fifo.get_ci()) elif self.suanfa == 2: arr_fanwei = str.split(self.suiji_num) start = arr_fanwei[0] end = arr_fanwei[1] size = self.suiji_size shu = self.neicunkuaishu arr = [] print(start) print(end) print(size) print(shu) for i in range(0, int(size)): arr.append(random.randint(int(start), int(end))) print(arr) fifo = LRU.main(arr, int(shu)) res = fifo.get_res() print(res) self.res.setText(res) self.res.adjustSize() self.setcilv(fifo.get_lv(), fifo.get_ci()) elif self.suanfa == 3: arr_fanwei = str.split(self.suiji_num) start = arr_fanwei[0] end = arr_fanwei[1] size = self.suiji_size shu = self.neicunkuaishu arr = [] print(start) print(end) print(size) print(shu) for i in range(0, int(size)): arr.append(random.randint(int(start), int(end))) print(arr) fifo = OPT.OPT(int(shu), arr) res = fifo.get_res() print(res) self.res.setText(res) self.res.adjustSize() self.setcilv(fifo.get_lv(), fifo.get_ci()) except: traceback.print_exc() print("ERROR-kong") self.error.show() self.timer = QTimer() self.num = 0 self.timer.timeout.connect(self.hideerror) self.timer.start(1500)
for n in range(3, 11): time_LRU = 0 time_LFU = 0 time_RND = 0 time_FIFO = 0 for i in range(10): input_list = [] for j in range(input_size): input_list.append(randint(0, 11)) starttime = datetime.now() LRU.LRU_implement(input_list, n) tmpTime_LRU = (datetime.now() - starttime).microseconds starttime = datetime.now() LFU.LFU_implement(input_list, n) tmpTime_LFU = (datetime.now() - starttime).microseconds starttime = datetime.now() RND.Random_implement(input_list, n) tmpTime_FIFO = (datetime.now() - starttime).microseconds starttime = datetime.now() FIFO.FIFO_implement(input_list, n) tmpTime_RND = (datetime.now() - starttime).microseconds time_LRU += tmpTime_LRU
input_size = 30 for n in range(3, 11): tmp_LRU = 0 tmp_LFU = 0 tmp_RND = 0 tmp_FIFO = 0 for i in range(10): input_list = [] for j in range(input_size): input_list.append(randint(0, 11)) tmp_LRU += LRU.LRU_implement(input_list, n) tmp_LFU += LFU.LFU_implement(input_list, n) tmp_RND += RND.Random_implement(input_list, n) tmp_FIFO += FIFO.FIFO_implement(input_list, n) LRU_list.append(tmp_LRU / 10) LFU_list.append(tmp_LFU / 10) RND_list.append(tmp_RND / 10) FIFO_list.append(tmp_FIFO / 10) index = [i for i in range(3, 11)] index = np.array(index) width = 0 plt.plot(index, LRU_list, label='LRU') plt.plot(index, LFU_list, label='LFU')
def setUp(self): self.lru = LRU.LRU_Cache()
def main(): lru_object = LRU(5) assert (lru_object.get("1")) == None for file in os.listdir(): if file.startswith("file"): data = open(file, "r").read() lru_object.put(file, data) assert (lru_object.get("file1.txt")) == "This is First file." assert (lru_object.get("file2.txt")) == "This is Second file." assert (lru_object.get("file3.txt")) == "This is Third file." assert (lru_object.get("file4.txt")) == "This is Fourth file." assert (lru_object.get("file5.txt")) == "This is Fifth file." lru_object.put("1", 2) assert (lru_object.get("file1.txt")) == None lru_object.put("2", 3) assert (sorted(lru_object.get_cache())) == [ '2', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt' ] lru_object.put("3", 4) assert (lru_object.get("1")) == None assert (lru_object.get("2")) == None assert (sorted(lru_object.get_cache())) == [ '3', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt' ] print("All Testcases Passed!")
def run(self): self.cwl = 0.0 self.dailist = [] self.dailistlru = [] self.dailistopt = [] # print("start") try: # self.timer = QTimer(self) # self.timer.timeout.connect(self.drawLine) # self.timer.start(1) self.start_num = 10 shuliang = self.su self.dtaxisX.setMax(int(shuliang) + self.start_num) kuaishu = self.kuai arr_fanwei = str.split(self.fan) start = arr_fanwei[0] end = arr_fanwei[1] for i in range(3, int(shuliang) + self.start_num): arr = [] for arr_p in range(0, i): arr.append(random.randint(int(start), int(end))) # print(arr) self.fifo = FIFO.main(arr, int(kuaishu)) # print("错误率fifo" + str(i) + ":" + str(self.fifo.get_lv())) self.cwl = self.fifo.get_lv() # # self.dailist.append(QPointF(self.start_num, self.fifo.get_lv() * 100)) self.lru = LRU.main(arr, int(kuaishu)) # print("错误率lru" + str(i) + ":" + str(self.lru.get_lv())) # self.dailistlru.append(QPointF(self.start_num, self.lru.get_lv() * 100)) self.opt = OPT.OPT(int(kuaishu), arr) # print("错误率opt" + str(i) + ":" + str(self.opt.get_lv())) # self.dailistopt.append(QPointF(self.start_num, self.opt.get_lv() * 100)) # self.dailist.append(QPointF(i/100, i/100)) self.start_num = self.start_num + 1 # self.series.replace(self.dailist) # self.serieslru.replace(self.dailistlru) # self.seriesopt.replace(self.dailistopt) # self.sinOut.emit("[[" + str(self.start_num) + "," + str(self.fifo.get_lv() * 100) + "]," + # "[ " + str(self.start_num) + "," + str(self.lru.get_lv() * 100) + "]," + # "[ " + str(self.start_num) + "," + str(self.opt.get_lv() * 100) + " ]]") self.sinOut.emit("%d %0.2f %d %0.2f %d %0.2f" % (self.start_num, self.fifo.get_lv() * 100, self.start_num, self.lru.get_lv() * 100, self.start_num, self.opt.get_lv() * 100)) # if self.fifo.get_lv()>self.lru.get_lv() and self.fifo.get_lv() >self.opt.get_lv() : # self.bijiao[0] = self.bijiao[0]+1 # elif self.lru.get_lv()>self.fifo.get_lv() and self.lru.get_lv() >self.opt.get_lv(): # self.bijiao[1] = self.bijiao[1]+1 # elif self.opt.get_lv()>self.fifo.get_lv() and self.opt.get_lv()>self.lru.get_lv(): # self.bijiao[2] = self.bijiao[2]+1 # if i == 3: # self.bijiao[0] = self.fifo.get_lv() # self.bijiao[1] = self.lru.get_lv() # self.bijiao[2] = self.opt.get_lv() # else: # self.bijiao[0] = (self.fifo.get_lv() + self.bijiao[0]) / 2.0 # self.bijiao[1] = (self.lru.get_lv() + self.bijiao[1]) / 2.0 # self.bijiao[2] = (self.opt.get_lv() + self.bijiao[2]) / 2.0 # # # self.qq.setText("FIFO:%0.2f LRU:%0.2f \n OPT:%0.2f" % (self.bijiao[0], self.bijiao[1], self.bijiao[2])) # point_6 = QPointF(1.00, 0.5) # point_7 = QPointF(2.00, 0.6) # point_8 = QPointF(3.00, 0.8) # self._1_point_list.append(point_6) # self._1_point_list.append(point_7) # self._1_point_list.append(point_8) # self.series_1.append(self._1_point_list) except: traceback.print_exc()