Created
February 1, 2018 05:40
-
-
Save bruce-forks/51d24ddaddaca7465302e0b77f006857 to your computer and use it in GitHub Desktop.
My solution to the steem-python challenge #19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from steem.blockchain import Blockchain | |
import sys | |
import threading | |
import math | |
class BlockThread (threading.Thread): | |
def __init__(self, threadIndex, start_block, end_block, total_blocks, counter, blockchain, stats): | |
threading.Thread.__init__(self) | |
self.threadIndex = threadIndex | |
self.start_block = start_block | |
self.end_block = end_block | |
self.total_blocks = total_blocks | |
self.counter = counter | |
self.blockchain = blockchain | |
self.stats = stats | |
def run(self): | |
stream = self.blockchain.stream_from(start_block=self.start_block, end_block=self.end_block) | |
current_block = self.start_block | |
block_count = self.end_block - self.start_block | |
for post in stream: | |
if post['block'] != current_block: | |
count_lock.acquire() | |
self.counter[0] += 1 | |
count_lock.release() | |
print ("Block {}/{} {:.2f}%".format(self.counter[0], self.total_blocks, self.counter[0]/self.total_blocks*100)) | |
current_block = post['block'] | |
elif post['block'] == self.start_block+block_count: | |
break | |
operation = post['op'][0] | |
lock.acquire() | |
if operation not in self.stats: | |
self.stats[operation] = 1 | |
else: | |
self.stats[operation] += 1 | |
lock.release() | |
lock = threading.Lock() | |
count_lock = threading.Lock() | |
def run(): | |
blockchain = Blockchain() | |
head_block = blockchain.get_current_block_num() | |
start_block = head_block-(int(sys.argv[1])) | |
amount_of_threads = int(sys.argv[2]) | |
block_count = int(sys.argv[1]) | |
counter = [0] | |
print ("Starting from block {} for {} blocks\n".format(start_block, block_count)) | |
start_blocks = {} | |
end_blocks = {} | |
threads = [] | |
results = [] | |
for i in range(0, amount_of_threads): | |
start_block_i = 0 | |
end_block_i = 0 | |
if i < amount_of_threads - 1: | |
start_block_i = start_block + (math.floor(block_count / amount_of_threads) * i) | |
end_block_i = start_block + (math.floor(block_count / amount_of_threads) * (i + 1)) | |
else: | |
start_block_i = start_block + (math.floor(block_count / amount_of_threads) * i) | |
end_block_i = head_block | |
results.append({}) | |
threads.append(BlockThread(i, start_block_i, end_block_i, block_count, counter, blockchain, results[i])) | |
threads[i].start() | |
for t in threads: | |
t.join() | |
stats = {} | |
for r in results: | |
for key, value in r.items(): | |
if key not in stats: | |
stats[key] = value | |
else: | |
stats[key] += value | |
print ("operations {}\n".format(len(stats.keys()))) | |
for operation in stats: | |
print (operation, stats[operation]) | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment