summaryrefslogtreecommitdiff
path: root/examples/bps.py
blob: 888fcdb78b07fea1e7a6d55c03007d8784681173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

# 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
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# 
# Author: David I. Lehn <dlehn@users.sourceforge.net>
#

import pygtk
pygtk.require('2.0')

import sys
import time
import gobject
import gtk

import pygst
pygst.require('0.10')

import gst

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
        print '\t%d buffers / %fs\t= %f bps\t= %f spb' % (self.buffers, dt, bps, spb)

    def fakesrc(self, buffers):
        src = gst.element_factory_make('fakesrc','src')
        src.set_property('silent', 1)
        src.set_property('num_buffers', buffers)
        return src

    def fakesink(self):
        sink = gst.element_factory_make('fakesink','sink')
        sink.set_property('silent', 1)
        return sink

    def build_pipeline(self, buffers):
        pipeline = gst.Pipeline('pipeline')

        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()

    def test(self):
        self.bus = self.pipeline.get_bus()

        self.start = time.time()
        
        self.pipeline.set_state(gst.STATE_PLAYING)

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

        self.pipeline.set_state(gst.STATE_NULL)
        self.done()

    def run(self, buffers):
        self.buffers = buffers
        
        print '# Testing buffer processing rate for "fakesrc ! fakesink"'
        print '# bps = buffers per second'
        print '# spb = seconds per buffer'
        
        self.pipeline = self.build_pipeline(buffers)
        assert self.pipeline

        self.test()
    
def main(args):
    "GStreamer Buffers-Per-Second tester"

    if len(args) < 2:
        print 'usage: %s buffers' % args[0]
        return 1
    
    bps = BPS()
    
    buffers = int(args[1])
    if buffers < 1:
        print 'buffers must be higher than 0'
        return

    bps.run(buffers)

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