[banshee] [extras/metrics] Tweaks



commit 80685276852d5cd99532f6aed22c8b851d80bee2
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Wed Feb 24 11:31:10 2010 -0800

    [extras/metrics] Tweaks

 extras/metrics/{CreateMetricsDb.cs => Database.cs} |   49 +++++++++++++++++++-
 extras/metrics/MetaMetrics.cs                      |   21 +++++++--
 extras/metrics/metrics.csproj                      |    6 ++-
 .../Hyena/Hyena.Data.Sqlite/SqliteUtils.cs         |    6 +-
 4 files changed, 72 insertions(+), 10 deletions(-)
---
diff --git a/extras/metrics/CreateMetricsDb.cs b/extras/metrics/Database.cs
similarity index 67%
rename from extras/metrics/CreateMetricsDb.cs
rename to extras/metrics/Database.cs
index 4e907de..f325702 100644
--- a/extras/metrics/CreateMetricsDb.cs
+++ b/extras/metrics/Database.cs
@@ -1,5 +1,5 @@
 //
-// CreateMetricsDb.cs
+// Database.cs
 //
 // Author:
 //   Gabriel Burt <gabriel burt gmail com>
@@ -30,6 +30,8 @@ using Hyena;
 using Hyena.Metrics;
 using Hyena.Data.Sqlite;
 using Hyena.Json;
+using Mono.Data.Sqlite;
+using System.Collections.Generic;
 
 namespace metrics
 {
@@ -39,7 +41,12 @@ namespace metrics
 
         public static HyenaSqliteConnection Open ()
         {
-            return new HyenaSqliteConnection (db_path);
+            var db =  new HyenaSqliteConnection (db_path);
+            db.Execute ("PRAGMA cache_size = ?", 32768 * 2);
+            db.Execute ("PRAGMA synchronous = OFF");
+            db.Execute ("PRAGMA temp_store = MEMORY");
+            db.Execute ("PRAGMA count_changes = OFF");
+            return db;
         }
 
         public static bool Exists { get { return System.IO.File.Exists (db_path); } }
@@ -82,4 +89,42 @@ namespace metrics
             }
         }
     }
+
+    [SqliteFunction (Name = "HYENA_METRICS_MEDIAN_LONG", FuncType = FunctionType.Aggregate, Arguments = 1)]
+    internal class MedianFunctionLong : MedianFunction<long>
+    {
+    }
+
+    [SqliteFunction (Name = "HYENA_METRICS_MEDIAN_DOUBLE", FuncType = FunctionType.Aggregate, Arguments = 1)]
+    internal class MedianFunctionDouble : MedianFunction<double>
+    {
+    }
+
+    [SqliteFunction (Name = "HYENA_METRICS_MEDIAN_DATETIME", FuncType = FunctionType.Aggregate, Arguments = 1)]
+    internal class MedianFunctionDateTime : MedianFunction<DateTime>
+    {
+    }
+
+    internal class MedianFunction<T> : SqliteFunction
+    {
+        public override void Step (object[] args, int stepNumber, ref object contextData)
+        {
+            List<T> list = null;
+            if (contextData == null) {
+                contextData = list = new List<T> ();
+            } else {
+                list = contextData as List<T>;
+            }
+
+            var val = (T)SqliteUtils.FromDbFormat (typeof(T), args[0]);
+            list.Add (val);
+        }
+
+        public override object Final (object contextData)
+        {
+            var list = contextData as List<T>;
+            list.Sort ();
+            return list[list.Count / 2];
+        }
+    }
 }
\ No newline at end of file
diff --git a/extras/metrics/MetaMetrics.cs b/extras/metrics/MetaMetrics.cs
index a5a9b14..e4040a7 100644
--- a/extras/metrics/MetaMetrics.cs
+++ b/extras/metrics/MetaMetrics.cs
@@ -97,10 +97,23 @@ namespace metrics
         private void SummarizeNumeric<T> (string metric_name)
         {
             Console.WriteLine ("{0}:", metric_name);
-            string fmt = typeof(T) == typeof(DateTime) ? "{0}" : "{0,-20:N1}";
-            Console.WriteLine (String.Format ("   Min: {0}", fmt), db.Query<T> ("SELECT MIN(CAST(Value as NUMERIC)) FROM Samples WHERE MetricName = ?", metric_name));
-            Console.WriteLine (String.Format ("   Max: {0}", fmt), db.Query<T> ("SELECT MAX(CAST(Value as NUMERIC)) FROM Samples WHERE MetricName = ?", metric_name));
-            Console.WriteLine (String.Format ("   Avg: {0}", fmt), db.Query<T> ("SELECT AVG(CAST(Value as NUMERIC)) FROM Samples WHERE MetricName = ?", metric_name));
+            var t = typeof(T);
+            string fmt = t == typeof(DateTime) ? "{0}" : "{0,10:N1}";
+            string median_func = "HYENA_METRICS_MEDIAN_" + (t == typeof(DateTime) ? "DATETIME" : t == typeof(long) ? "LONG" : "DOUBLE");
+
+            using (var reader = new HyenaDataReader (db.Query (String.Format (@"
+                    SELECT
+                        MIN(CAST(Value as NUMERIC)), MAX(CAST(Value as NUMERIC)),
+                        AVG(CAST(Value as NUMERIC)), {0}(CAST(Value as NUMERIC))
+                    FROM Samples WHERE MetricName = ?", median_func), metric_name))) {
+                if (reader.Read ()) {
+                    Console.WriteLine (String.Format ("   Min:    {0}", fmt), reader.Get<T> (0));
+                    Console.WriteLine (String.Format ("   Avg:    {0}", fmt), reader.Get<T> (2));
+                    Console.WriteLine (String.Format ("   Median: {0}", fmt), reader.Get<T> (3));
+                    Console.WriteLine (String.Format ("   Max:    {0}", fmt), reader.Get<T> (1));
+                }
+            }
+
             Console.WriteLine ();
         }
 
diff --git a/extras/metrics/metrics.csproj b/extras/metrics/metrics.csproj
index 1ba256a..e2a9c85 100644
--- a/extras/metrics/metrics.csproj
+++ b/extras/metrics/metrics.csproj
@@ -32,16 +32,20 @@
     <Reference Include="System" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="CreateMetricsDb.cs" />
     <Compile Include="MultiUserSample.cs" />
     <Compile Include="MetaMetrics.cs" />
     <Compile Include="Main.cs" />
+    <Compile Include="Database.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\..\src\Libraries\Hyena\Hyena.csproj">
       <Project>{95374549-9553-4C1E-9D89-667755F90E12}</Project>
       <Name>Hyena</Name>
     </ProjectReference>
+    <ProjectReference Include="..\..\src\Libraries\Mono.Data.Sqlite\Mono.Data.Sqlite.csproj">
+      <Project>{BB1D1D81-7A74-4183-B7B1-3E78B32D42F1}</Project>
+      <Name>Mono.Data.Sqlite</Name>
+    </ProjectReference>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 </Project>
\ No newline at end of file
diff --git a/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteUtils.cs b/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteUtils.cs
index cbe5269..c9083c2 100644
--- a/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteUtils.cs
+++ b/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteUtils.cs
@@ -34,9 +34,9 @@ using System.Collections.Generic;
 
 namespace Hyena.Data.Sqlite
 {
-    internal static class SqliteUtils
+    public static class SqliteUtils
     {
-        public static string GetType (Type type)
+        internal static string GetType (Type type)
         {
             if (type == typeof (string)) {
                 return "TEXT";
@@ -103,7 +103,7 @@ namespace Hyena.Data.Sqlite
             }
         }
 
-        public static string BuildColumnSchema (string type, string name, string default_value,
+        internal static string BuildColumnSchema (string type, string name, string default_value,
             DatabaseColumnConstraints constraints)
         {
             StringBuilder builder = new StringBuilder ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]