3 # Script to plot data in real(ish) time
4 # Naive approach is to just read from stdin and plot each point as it arrives
5 # This isn't so great if you have a fast data rate, the plot window flashes and gets out of sync
6 # Improvement is to use a thread to read from stdin, and a thread (main thread) that periodically plots the data (set the time interval to be sufficiently slow)
13 import Gnuplot, Gnuplot.funcutils
15 plt = Gnuplot.Gnuplot(persist=1)
17 semaphore = threading.Semaphore()
20 def ProcessData(points):
21 #TODO: Alter to change data as it is read
24 # Store finite differences
25 points += [points[i]-data[-1][i] for i in xrange(len(points))]
27 points += [0 for _ in xrange(len(points))]
31 #TODO: Alter to change plotting of data
35 plt.plot(Gnuplot.Data(data, with_="lp",using="2:($8/$9)", title="[Real] clock_gettime()"))
36 plt.replot(Gnuplot.Data(data, with_="lp",using="2:($8/$10)", title="[CPU] SDL_GetPerformanceCounter()"))
37 plt.replot(Gnuplot.Data(data, with_="lp",using="2:($8/$11)", title="[GPU] OpenGL Query (GL_TIME_ELAPSED)"))
39 # Produces data points by reading from stdin
44 line = sys.stdin.readline()
45 line = line.strip(" \t\r\n")
50 data.append(ProcessData(map(float, line.split())))
55 # "Consumes" data points by plotting them. Not really a consumer because it doesn't actually clear any but whatever
63 if data_size != prev_size and data_size >= 2:
71 # Setup plot and start threads
74 plt.title("Performance Graph")
75 plt.xlabel("Real Time (s)")
76 plt.ylabel("Average FPS (since last data point)")
77 producer = threading.Thread(target=Producer)
80 Consumer() # Can run plot in main thread, easier that way
81 except KeyboardInterrupt:
82 sys.stderr.write("Press enter to exit.\n")
87 if __name__ == "__main__":