Advertisement
Guest User

Untitled

a guest
Feb 7th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. from steem.blockchain import Blockchain
  2. from steem import Steem
  3. from steem.post import Post
  4. import threading, time, sys, os, queue, json
  5. import matplotlib.pyplot as plt
  6.  
  7. # minimal difference between two consecutive measurements needed to consider the solution as optimal
  8. # defaultly it's 5%,
  9. STOP_CONDITION = 1.05
  10. BLOCK_COUNT = 1000
  11. MAX_THREADS = 25
  12.  
  13. class myThread (threading.Thread):
  14.     def __init__(self, thread_id, start_block, end_block, n, blockchain, workQueue, queueLock):
  15.         threading.Thread.__init__(self)
  16.         self.thread_id      = thread_id
  17.         self.start_block    = start_block
  18.         self.end_block      = end_block
  19.         self.n          = n
  20.         self.blockchain     = blockchain
  21.         self.stream     = self.blockchain.stream_from(start_block=start_block, end_block=end_block)
  22.         self.current_block  = self.start_block
  23.         self.workQueue      = workQueue
  24.         self.queueLock      = queueLock
  25.  
  26.         print (self.thread_id, self.start_block, self.end_block)
  27.  
  28.     def run(self):
  29.         data = {}
  30.         for post in self.stream:
  31.             if post['block'] != self.current_block:
  32.                 percentage = (self.current_block-self.start_block)/self.n*100
  33.                 print("Thread {} is at block {}/{} {:.2f}%".format(self.thread_id,post['block'],self.end_block, percentage))
  34.                 self.current_block = post['block']
  35.  
  36.             operation = post['op'][0]
  37.  
  38.             if operation not in data:
  39.                 data[operation] = 1
  40.             else:
  41.                 data[operation] += 1
  42.  
  43.         self.queueLock.acquire()
  44.         self.workQueue.put(data)
  45.         self.queueLock.release()
  46.  
  47. def run():
  48.     # each iteration calculates the same part of blockchain, to provide reliable results
  49.     blockchain      = Blockchain()
  50.     head_block      = blockchain.get_current_block_num()
  51.    
  52.     execution_times = []
  53.     last_elapsed_time = sys.float_info.max
  54.  
  55.     i = 1
  56.     while True:
  57.         start_time = time.time()
  58.        
  59.         start       = head_block-BLOCK_COUNT
  60.         amount_of_threads   = i
  61.         n           = int(BLOCK_COUNT/amount_of_threads)
  62.         threads = []
  63.  
  64.         queueLock = threading.Lock()
  65.         workQueue = queue.Queue(amount_of_threads)
  66.  
  67.         for x in range(0, amount_of_threads-1):
  68.             thread = myThread(x, start, start+ n-1, n, blockchain, workQueue, queueLock)
  69.             thread.start()
  70.             threads.append(thread)
  71.             start = start + n
  72.         # last thread can calculate one more block
  73.         thread = myThread(amount_of_threads-1, start, head_block, head_block - start, blockchain, workQueue, queueLock)
  74.         thread.start()
  75.         threads.append(thread)
  76.            
  77.         print()
  78.  
  79.         for t in threads:
  80.             t.join()
  81.  
  82.         merged_data = {}
  83.  
  84.         while not workQueue.empty():
  85.             data = workQueue.get()
  86.             for key in data:
  87.                 if key not in merged_data:
  88.                     merged_data[key] = data[key]
  89.                 else:
  90.                     merged_data[key] += data[key]
  91.                     # uncomment following lines to display statistics
  92.                     #for operation in merged_data:
  93.                     #    print (operation, merged_data[operation])
  94.         elapsed_time = time.time() - start_time
  95.         execution_times.append(elapsed_time)
  96.         if not last_elapsed_time == sys.float_info.max:
  97.             print("Adding this thread has decreased calculation time by {}%".format((last_elapsed_time/elapsed_time - 1.0)*100))
  98.         if last_elapsed_time/elapsed_time < STOP_CONDITION:
  99.             print("Optimal amount of threads for your CPU is {}".format(i-1))
  100.             break
  101.         if i == MAX_THREADS:
  102.             print("it seems that execution time can't stop decreasing, that's interesting!")
  103.             break
  104.         i += 1
  105.         last_elapsed_time = elapsed_time
  106.  
  107.     plt.plot(range(1,len(execution_times)+1),execution_times)
  108.     plt.ylabel('execution time [s]')
  109.     plt.xlabel('amount of threads')
  110.     plt.show()
  111.  
  112. if __name__ == '__main__':
  113.     run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement