Added a few stats #8

Open
d3lxa opened this Issue Apr 17, 2011 · 0 comments

Comments

Projects
None yet
1 participant

d3lxa commented Apr 17, 2011

Hello,

I've modified the source to add some stats about the found hashes, the total counts, average etc... They are displayed on the line with the last event message. When you exit it shows you all the found hashes and the uptime. I hope it will help people and improve your experience :) Tested and patched for b981138.

-> Uploaded here: http://pastebin.com/gzg9g6ie
and below (seems it miss so lines at the end).

diff --git a/BitcoinMiner.py b/BitcoinMiner.py
index 0f47225..66e6a51 100644
--- a/BitcoinMiner.py
+++ b/BitcoinMiner.py
@@ -24,6 +24,28 @@ def socketwrap(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
    return sockobj
 socket.socket = socketwrap
 
+hash_found = [ ]
+startup_time = time()
+timeago_desc = [
+               (1, "sec"),
+               (60, "mn"),
+               (3600, "hours"),
+               (86400, "days"),
+               (604800, "weeks"),
+               (2419200, "months"),
+               (29030400, "years")
+       ]
+timeago_desc.reverse()
+def timeago(time, n=2):
+       delta = int(time)
+       fdesc = [ ]
+       for (div, desc) in timeago_desc:
+               fdesc.append( (delta/div, desc) )
+               delta = delta % div
+       fdesc = filter(lambda (d,t): d!=0, fdesc)
+       if len(fdesc) == 0:
+               return "now"
+       return ', '.join(map(lambda (d,t): "%i %s" % (d, t), fdesc[:n])) + " ago"
 
 VERSION = '201103.beta3'
 
@@ -73,6 +95,7 @@ class BitcoinMiner():
        self.worksize = int(worksize)
        self.frames = max(int(frames), 3)
        self.verbose = verbose
+       self.lastMessage = (None, None) # (Time, Msg)
        self.longPollActive = self.stop = False
        self.update = True
        self.lock = RLock()
@@ -93,19 +116,30 @@ class BitcoinMiner():
            if self.verbose:
                print '%s,' % datetime.now().strftime(TIME_FORMAT), format % args
            else:
-               sys.stdout.write('\r                                                            \r%s' % (format % args))
+               sys.stdout.write('\r%s\r%s' % (" "*70, format % args))
+               (mtime, msg) = self.lastMessage
+               if mtime and msg:
+                   sys.stdout.write(' - %s [%s]' % (msg, timeago(time()-mtime)))
            sys.stdout.flush()
 
    def sayLine(self, format, args=()):
-       if not self.verbose:
-           format = '%s, %s\n' % (datetime.now().strftime(TIME_FORMAT), format)
-       self.say(format, args)
+       self.lastMessage = (time(), format % args)
 
    def exit(self):
+       print "\nHash found (%i): %s" % \
+               (len(hash_found), \
+               ', '.join(map(lambda (x,y): "[%s@%i]" % (y, x), hash_found)))
+       print "Uptime: %i" % (time() - startup_time)
+       print 'bye'
        self.stop = True
 
    def hashrate(self, rate):
-       self.say('%s khash/s', rate)
+       hash_count = len(hash_found)
+       hash_avg = 0
+       if hash_count > 0:
+           hash_avg = float(time() - startup_time) / float(hash_count)
+       self.say('%.3f Mhs, ct: %i, avg: %.1f' % \
+               (rate/1000.0, hash_count, hash_avg))
 
    def failure(self, message):
        print '\n%s' % message
@@ -116,6 +150,7 @@ class BitcoinMiner():
            self.sayLine('checking %s <= %s', (hash, target))
 
    def blockFound(self, hash, accepted):
+       hash_found.append((time(), hash))
        self.sayLine('%s, %s', (hash, if_else(accepted, 'accepted', 'invalid or stale')))
 
    def mine(self):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment