centralserver_OO.py :  » Development » SimPy » SimPy-2.1.0beta » SimPyModels » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SimPy 
SimPy » SimPy 2.1.0beta » SimPyModels » centralserver_OO.py
""" centralserver.py

A time-shared computer consists of a single
central processing unit (CPU) and a number of
terminals. The operator of each terminal `thinks'
for a time (exponential, mean 100.0 sec) and then
submits a task to the computer with a service time
(exponential, mean 1.0 sec). The operator then
remains idle until the task completes service and
returns to him or her. The arriving tasks form a
single FCFS queue in front of the CPU.

Upon leaving the CPU a task is either finished
(probability 0.20) and returns to its operator
to begin another `think' time, or requires data
from a disk drive (probability 0.8). If a task
requires access to the disk, it joins a FCFS queue
before service (service time at the disk,
exponential, mean 1.39 sec). When finished with
the disk, a task returns to the CPU queue again
for another compute time (exp, mean 1.$ sec).

the objective is to measure the throughput of
the CPU (tasks per second)
"""
from SimPy.Simulation import *
## from SimPy.SimulationTrace import *
import random as ran

## Model components ------------------------

class Task(Process):
    """ A computer  task  requires at least
    one use of the CPU and possibly accesses to a
    disk drive."""
    completed = 0
    rate = 0.0
    def execute(self,maxCompletions):
        while Task.completed < MaxCompletions:
            self.debug(" starts thinking")
            thinktime = ran.expovariate(1.0/MeanThinkTime)
            yield hold,self,thinktime
            self.debug(" request cpu")
            yield request,self,self.sim.cpu
            self.debug(" got cpu")
            CPUtime=ran.expovariate(1.0/MeanCPUTime)
            yield hold,self,CPUtime
            yield release,self,self.sim.cpu
            self.debug(" finish cpu")
            while ran.random() < pDisk:
                self.debug(" request disk")
                yield request,self,self.sim.disk
                self.debug(" got disk")
                disktime=ran.expovariate(1.0/MeanDiskTime)
                yield hold,self,disktime
                self.debug(" finish disk")
                yield release,self,self.sim.disk
                self.debug(" request cpu")
                yield request,self,self.sim.cpu
                self.debug(" got cpu")
                CPUtime=ran.expovariate(1.0/MeanCPUTime)
                yield hold,self,CPUtime
                yield release,self,self.sim.cpu
            Task.completed += 1
        self.debug(" completed %d tasks"%(Task.completed,))
        Task.rate = Task.completed/float(self.sim.now())

    def debug(self,message):
        FMT="%9.3f %s %s"
        if DEBUG:
            print FMT%(self.sim.now(),self.name,message)
            
    
## Model ------------------------------
class CentralServerModel(Simulation):
    def run(self):
        self.initialize()
        self.cpu  = Resource(name='cpu',sim=self)
        self.disk = Resource(name='disk',sim=self)
        for i in range(Nterminals):
            t = Task(name="task"+`i`,sim=self)
            self.activate(t,t.execute(MaxCompletions))
        self.simulate(until = MaxrunTime)
        return (self.now(),Task.rate)

## Experiment data -------------------------
Nterminals = 3       ## Number of terminals = Tasks
pDisk    = 0.8       ## prob. of going to disk
MeanThinkTime = 10.0 ## seconds
MeanCPUTime = 1.0    ## seconds
MeanDiskTime = 1.39  ## seconds

ran.seed(111113333)
MaxrunTime = 20000.0
MaxCompletions = 100
DEBUG = False


## Experiment

result = CentralServerModel().run()

## Analysis/output -------------------------

print 'centralserver'
print '%7.4f: CPU rate = %7.4f tasks per second'%result
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.