コード例 #1
0
ファイル: bench.py プロジェクト: qnu/paramark
class Bench:
    def __init__(self, opts):
        self.opts = opts
        self.cfg = self.opts.vals
        
        self.runtime = Values()
        self.runtime.version = version.PARAMARK_VERSION
        self.runtime.date = version.PARAMARK_DATE
        self.runtime.uid = os.getuid()
        self.runtime.pid = os.getpid()
        self.runtime.user = pwd.getpwuid(os.getuid())[0]
        self.runtime.hostname = socket.gethostname()
        self.runtime.platform = " ".join(os.uname())
        self.runtime.cmdline = " ".join(sys.argv)
        self.runtime.mountpoint = get_filesystem_info(self.cfg.wdir)
        self.runtime.wdir = self.cfg.wdir
        # May be set later in GXP mode
        self.runtime.hid = 0
        self.runtime.nhosts = 1

        self.cfg.hid = self.runtime.hid
        self.cfg.pid = self.runtime.pid
        self.loader = BenchLoad(self.cfg)
        self.threads = []
        self.db = None
        self.gxp = None
       
    def load(self):
        if self.cfg.gxpmode:
            self.gxp = Values()
            self.gxp.wp = os.fdopen(3, "wb")
            self.gxp.rp = os.fdopen(4, "rb")
            self.gxp.rank = gxp.get_rank()
            self.gxp.size = gxp.get_size()
            self.runtime.hid = self.gxp.rank
            self.runtime.nhosts = self.gxp.size
            self.cfg.hid = self.runtime.hid
        
        self.threadsync = ThreadSync(self.cfg.nthreads)
        for i in range(0, self.cfg.nthreads):
            self.threads.append(BenchThread(i, self.threadsync, 
                self.loader, self.gxp))

    def run(self):
        if self.runtime.hid == 0:
            message("Start benchmarking ...")
        
        self.start = timer()
        for t in self.threads: t.start()
        for t in self.threads: t.join()
        self.end = timer()

        if self.cfg.dryrun and self.runtime.hid == 0: 
            message("Dryrun, nothing was executed.\n")
        
        self.runtime.start = "%r" % self.start
        self.runtime.end = "%r" % self.end

    def save(self):
        if self.cfg.dryrun: return
        
        if self.cfg.gxpmode:
            # Gather results
            self.send_res()
            if self.gxp.rank == 0:
                reslist = []
                for i in range(0, self.gxp.size):
                    reslist.append(self.recv_res())
            else: return
        
        if self.cfg.logdir is None:  # generate random logdir in cwd
            self.cfg.logdir = os.path.abspath("./pmlog-%s-%s" %
                   (self.runtime.user, time.strftime("%j-%H-%M-%S")))
        
        # Initial log directory and database
        if self.cfg.gxpmode:
            self.cfg.confirm = False
        self.cfg.logdir = smart_makedirs(self.cfg.logdir,
            self.cfg.confirm)
        logdir = os.path.abspath(self.cfg.logdir)
        
        if self.cfg.nolog:
            message("Saving data in memory ...")
        else:
            message("Saving data to %s/fsbench.db ..." % logdir)
        
        # Save used configuration file
        if not self.cfg.nolog:
            verbose("Saving configurations to %s/fsbench.conf ..." 
                % logdir, VERBOSE)
            self.opts.save_conf("%s/fsbench.conf" % logdir)
        
        # Save results
        if self.cfg.nolog: self.db = Database(":memory:")
        else: self.db = Database("%s/fsbench.db" % logdir)
        self.db.insert_runtime(self.runtime)
        self.db.insert_conf(self.opts.cfgParser)

        if self.cfg.gxpmode:
            for res in reslist:
                for r in res: self.db.insert_rawdata(r)
        else:
            for t in self.threads:
                self.db.insert_rawdata(t.get_res())
        
        self.db.commit() 
        if self.cfg.noreport: self.db.close()
    
    def report(self):
        if self.cfg.dryrun or self.cfg.noreport: return
        if self.cfg.gxpmode and self.gxp.rank != 0: return
        
        message("Generating report ...")
        
        import report
        logdir = self.cfg.logdir
        if self.cfg.report:
            logdir = self.cfg.report
        if self.cfg.textreport:
            self.report = report.TextReport(logdir, self.db, self.cfg)
        elif self.cfg.csvreport:
            self.report = report.CSVReport(logdir, self.db, self.cfg)
        else:
            self.report = report.HTMLReport(logdir, self.db, self.cfg)
        self.report.write()
         
    def send_res(self):
        # Packing string without newlines
        res = cPickle.dumps([t.get_res() for t in self.threads], 0)
        self.gxp.wp.write('|'.join(res.split('\n')))
        self.gxp.wp.write("\n")
        self.gxp.wp.flush()

    def recv_res(self):
        res = self.gxp.rp.readline().strip('\n')
        return cPickle.loads('\n'.join(res.split('|')))
    
    def vs(self, msg):
        sys.stderr.write(msg)