bps.py 3.01 KB
Newer Older
1
#!/usr/bin/env python
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
2 3 4
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# gst-python
# Copyright (C) 2003 David I. Lehn
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
Simon Farnsworth's avatar
Simon Farnsworth committed
20
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
# Boston, MA 02110-1301, USA.
22 23 24 25
# 
# Author: David I. Lehn <dlehn@users.sourceforge.net>
#

Johan Dahlin's avatar
Johan Dahlin committed
26 27 28
import pygtk
pygtk.require('2.0')

29 30
import sys
import time
31
import gobject
32
import gtk
33 34

import pygst
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
35
pygst.require('0.10')
36

Johan Dahlin's avatar
Johan Dahlin committed
37
import gst
38 39 40 41 42 43 44 45 46 47 48

class BPS(object):
    def __init__(self):
        self.buffers = 0
        self.start = 0

    def done(self):
        end = time.time()
        dt = end - self.start
        bps = self.buffers/dt
        spb = dt/self.buffers
David I. Lehn's avatar
David I. Lehn committed
49
        print '\t%d buffers / %fs\t= %f bps\t= %f spb' % (self.buffers, dt, bps, spb)
50 51

    def fakesrc(self, buffers):
52
        src = gst.element_factory_make('fakesrc','src')
53 54 55 56 57
        src.set_property('silent', 1)
        src.set_property('num_buffers', buffers)
        return src

    def fakesink(self):
58
        sink = gst.element_factory_make('fakesink','sink')
59 60 61 62
        sink.set_property('silent', 1)
        return sink

    def build_pipeline(self, buffers):
63
        pipeline = gst.Pipeline('pipeline')
64 65 66 67 68 69 70 71 72 73 74 75

        src = self.fakesrc(buffers)
        pipeline.add(src)
        sink = self.fakesink()
        pipeline.add(sink)
        src.link(sink)

        return pipeline

    def idle(self, pipeline):
        return pipeline.iterate()

76 77 78 79
    def test(self):
        self.bus = self.pipeline.get_bus()

        self.start = time.time()
80 81
        
        self.pipeline.set_state(gst.STATE_PLAYING)
82 83 84 85 86

        while 1:
            msg = self.bus.poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, gst.SECOND)
            if msg:
                break
87

88
        self.pipeline.set_state(gst.STATE_NULL)
89
        self.done()
90

91
    def run(self, buffers):
92 93
        self.buffers = buffers
        
94 95 96
        print '# Testing buffer processing rate for "fakesrc ! fakesink"'
        print '# bps = buffers per second'
        print '# spb = seconds per buffer'
97 98
        
        self.pipeline = self.build_pipeline(buffers)
99
        assert self.pipeline
100 101

        self.test()
102 103 104 105 106
    
def main(args):
    "GStreamer Buffers-Per-Second tester"

    if len(args) < 2:
107
        print 'usage: %s buffers' % args[0]
108 109 110 111 112
        return 1
    
    bps = BPS()
    
    buffers = int(args[1])
113
    if buffers < 1:
114 115
        print 'buffers must be higher than 0'
        return
116

117
    bps.run(buffers)
118 119

if __name__ == '__main__':
120
    sys.exit(main(sys.argv))