2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / perf / perf-suite-generator / GraphGenerator.cs
blob0007bf3dcbc32b44b2792f19a29fa020fa58d860
1 /*
2 * Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
4 * Contact:
5 * Moonlight List (moonlight-list@lists.ximian.com)
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
14 * conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
30 using System;
31 using System.Collections.Generic;
32 using PerfSuiteLib;
33 using Cairo;
35 namespace PerfSuiteGenerator {
37 public static class GraphGenerator {
39 public static void GenerateGraph (List <ResultDbEntry> resultList, string filename)
41 // FIXME Exception if more than 50 results...
43 ImageSurface surface = new ImageSurface (Format.Rgb24, 103, 52);
44 Context context = new Context (surface);
46 // Fill with grad
47 LinearGradient grad = new LinearGradient (0.0, 0.0, 0.0, 52.0);
48 grad.AddColorStopRgb (0.0, new Color (1.0, 1.0, 1.0));
49 grad.AddColorStopRgb (1.0, new Color (0.8, 0.8, 0.9));
50 context.Pattern = grad;
51 context.Paint ();
53 // Frame
54 context.SetSourceRGB (0, 0, 0);
55 context.LineWidth = 1.0;
56 context.Rectangle (0.5, 0.5, 102.0, 51.0);
57 context.Stroke ();
59 long denominator = (long) (FindBiggestResult (resultList) * 1.2);
61 context.LineWidth = 1.5;
63 // FIXME Reverse to copy
64 resultList.Reverse ();
66 double x = 100.5 - ((resultList.Count - 1) * 2.0);
67 bool hasPrevResult = false;
68 long prevResult = 0;
70 foreach (ResultDbEntry entry in resultList) {
72 if (entry.Failure) {
73 x += 2.0;
74 continue;
77 double sz = ((double) entry.Time / denominator) * 50.0;
79 if (hasPrevResult && UtilFu.GetValueDifference (prevResult, entry.Time) > 0.1)
80 context.SetSourceRGB (1.0, 0.0, 0.0);
81 else if (hasPrevResult && UtilFu.GetValueDifference (prevResult, entry.Time) < -0.1)
82 context.SetSourceRGB (0.0, 1.0, 0.0);
83 else
84 context.SetSourceRGB (0.4, 0.4, 0.4);
86 context.MoveTo (x, 51);
87 context.LineTo (x, 51 - sz);
88 context.Stroke ();
90 x += 2.0;
92 hasPrevResult = true;
93 prevResult = entry.Time;
96 surface.WriteToPng (filename);
98 resultList.Reverse ();
99 ((IDisposable) context).Dispose ();
100 ((IDisposable) surface).Dispose ();
103 private static long FindBiggestResult (List <ResultDbEntry> resultList)
105 long biggest = 0;
106 foreach (ResultDbEntry entry in resultList)
107 biggest = Math.Max (biggest, entry.Time);
109 return biggest;