Created
July 18, 2012 17:08
-
-
Save praeclarum/3137502 to your computer and use it in GitHub Desktop.
Test used to measure the performance of SQLite-net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MonoTouch.UIKit; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Diagnostics; | |
using System.Reflection; | |
using System.Collections.Generic; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Text; | |
using System.Runtime.Serialization; | |
using System.Xml; | |
using Newtonsoft.Json; | |
using System.Linq; | |
namespace SpeedTest | |
{ | |
[Serializable] | |
class TestObject { | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public DateTime ModifiedTime { get; set; } | |
public double Value { get; set; } | |
public string Text { get; set; } | |
public int Last { get; set; } | |
} | |
public class TestController : UIViewController | |
{ | |
public TestController () | |
{ | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
Task.Factory.StartNew (delegate { | |
Run (); | |
}); | |
} | |
Stopwatch _sw; | |
string _logs; | |
void LogBegin(string format, params object[] args) { | |
_sw = new Stopwatch(); | |
_logs = string.Format(format, args); | |
Console.WriteLine (_logs); | |
_sw.Start(); | |
} | |
void LogEnd() { | |
_sw.Stop(); | |
var dur = _sw.Elapsed; | |
Console.WriteLine ("{0}", dur); | |
Console.WriteLine (" "); | |
} | |
string RandomString(Random r, int maxlen) { | |
var len = (r.Next() % maxlen) + 1; | |
var chars = new char[len]; | |
for (var i = 0; i < len; i++) { | |
chars[i] = (char)((r.Next() % 26) + 'a'); | |
} | |
return new string(chars); | |
} | |
class TypeInfo { | |
enum TypeCode { | |
Int32, | |
DateTime, | |
String, | |
Double, | |
} | |
class PropInfo { | |
public PropertyInfo Prop; | |
public TypeCode Type; | |
} | |
TypeCode GetTypeCode(Type t) { | |
if (t == typeof(int)) return TypeCode.Int32; | |
else if (t == typeof(string)) return TypeCode.String; | |
else if (t == typeof(DateTime)) return TypeCode.DateTime; | |
else if (t == typeof(double)) return TypeCode.Double; | |
throw new NotSupportedException(t.Name); | |
} | |
List<PropInfo> Props = new List<PropInfo>(); | |
public TypeInfo(Type ty) { | |
foreach (var p in ty.GetProperties()) { | |
Props.Add(new PropInfo { | |
Prop = p, | |
Type = GetTypeCode(p.PropertyType) | |
}); | |
} | |
} | |
public void Write (TestObject o, BinaryWriter b) | |
{ | |
foreach (var p in Props) { | |
var v = p.Prop.GetValue(o, null); | |
if (p.Type == TypeCode.Int32) { b.Write((int)v); } | |
else if (p.Type == TypeCode.String) { b.Write((string)v); } | |
else if (p.Type == TypeCode.DateTime) { b.Write(((DateTime)v).Ticks); } | |
else if (p.Type == TypeCode.Double) { b.Write((double)v); } | |
} | |
} | |
public void Read (TestObject o, BinaryReader b) | |
{ | |
foreach (var p in Props) { | |
if (p.Type == TypeCode.Int32) { p.Prop.SetValue(o, b.ReadInt32(), null); } | |
else if (p.Type == TypeCode.String) { p.Prop.SetValue(o, b.ReadString(), null); } | |
else if (p.Type == TypeCode.DateTime) { p.Prop.SetValue(o, new DateTime(b.ReadInt64()), null); } | |
else if (p.Type == TypeCode.Double) { p.Prop.SetValue(o, b.ReadDouble(), null); } | |
} | |
} | |
} | |
void CreateFiles(int n, string path) { | |
Console.WriteLine ("SERIALIZING"); | |
var rand = new Random(42); | |
var objs = new List<TestObject>(); | |
for (var i = 0; i < n; i++) { | |
objs.Add(new TestObject() { | |
Id = rand.Next(), | |
Name = RandomString(rand, 32), | |
ModifiedTime = new DateTime((rand.Next() % 1000) + 1000, (rand.Next() % 11) + 1, (rand.Next() % 25) + 1), | |
Value = rand.NextDouble(), | |
Text = RandomString(rand, 400), | |
Last = rand.Next() | |
}); | |
} | |
LogBegin("BinaryFormatter"); | |
using (var f = File.OpenWrite(path + ".serial")) { | |
var b = new BinaryFormatter(); | |
b.Serialize(f, objs); | |
} | |
LogEnd(); | |
File.Delete (path + ".sqlite"); | |
using (var db = new SQLite.SQLiteConnection (path + ".sqlite", true)) { | |
db.CreateTable <TestObject> (); | |
LogBegin("SQLite-net"); | |
db.InsertAll (objs); | |
} | |
LogEnd(); | |
/*var s = new DataContractSerializer (typeof (TestObject), new Type[] { typeof(List<TestObject>) }); | |
LogBegin ("DataContractSerializer"); | |
using (var f = File.OpenWrite(path + ".dcs")) { | |
using (var w = new XmlTextWriter(f, Encoding.UTF8)) { | |
s.WriteObject (w, objs); | |
} | |
} | |
LogEnd ();*/ | |
/*var jsons = new JsonSerializer (); | |
LogBegin ("Newtonsoft.JsonSerializer"); | |
using (var f = File.OpenWrite(path + ".njson")) { | |
using (var tw = new StreamWriter(f, Encoding.UTF8)) { | |
using (var w = new JsonTextWriter(tw)) { | |
jsons.Serialize (w, objs); | |
} | |
} | |
} | |
LogEnd ();*/ | |
LogBegin("BinaryWriter Explicit"); | |
using (var f = File.OpenWrite(path + ".binary")) { | |
using (var b = new BinaryWriter(f, Encoding.UTF8)) { | |
b.Write(objs.Count); | |
foreach (var o in objs) { | |
b.Write(o.Id); | |
b.Write(o.Name); | |
b.Write(o.ModifiedTime.Ticks); | |
b.Write(o.Value); | |
b.Write(o.Text); | |
b.Write(o.Last); | |
} | |
} | |
} | |
LogEnd(); | |
var info = new TypeInfo(typeof(TestObject)); | |
LogBegin("BinaryWriter Reflection"); | |
using (var f = File.OpenWrite(path + ".rbinary")) { | |
using (var b = new BinaryWriter(f, Encoding.UTF8)) { | |
b.Write(objs.Count); | |
foreach (var o in objs) { | |
info.Write(o, b); | |
} | |
} | |
} | |
LogEnd(); | |
} | |
void ReadFiles(int n, string path) { | |
Console.WriteLine ("DESERIALIZING"); | |
LogBegin("BinaryFormatter"); | |
using (var f = File.OpenRead(path + ".serial")) { | |
var b = new BinaryFormatter(); | |
var objs = (List<TestObject>)b.Deserialize(f); | |
if (objs.Count != n) throw new Exception(); | |
} | |
LogEnd(); | |
/*var s = new DataContractSerializer (typeof (TestObject), new Type[] { typeof(List<TestObject>) }); | |
Console.WriteLine (File.ReadAllText (path + ".dcs")); | |
LogBegin ("DataContractSerializer"); | |
using (var f = File.OpenRead(path + ".dcs")) { | |
using (var w = new XmlTextReader(f)) { | |
var r = s.ReadObject (w); | |
var objs = (List<TestObject>)r; | |
if (objs.Count != n) throw new Exception(); | |
} | |
} | |
LogEnd ();*/ | |
using (var db = new SQLite.SQLiteConnection (path + ".sqlite", true)) { | |
db.CreateTable <TestObject> (); | |
LogBegin("SQLite-net"); | |
var objs = db.Table<TestObject> ().ToList (); | |
if (objs.Count != n) throw new Exception(objs.Count.ToString ()); | |
} | |
LogEnd(); | |
/*var jsons = new JsonSerializer (); | |
LogBegin ("Newtonsoft.JsonSerializer"); | |
using (var f = File.OpenRead(path + ".njson")) { | |
using (var tw = new StreamReader(f, Encoding.UTF8)) { | |
using (var w = new JsonTextReader(tw)) { | |
var objs = (List<TestObject>)jsons.Deserialize (w); | |
if (objs.Count != n) throw new Exception(); | |
} | |
} | |
} | |
LogEnd ();*/ | |
LogBegin("BinaryReader Explicit"); | |
using (var f = File.OpenRead(path + ".binary")) { | |
using (var b = new BinaryReader(f, Encoding.UTF8)) { | |
var count = b.ReadInt32(); | |
var objs = new List<TestObject>(count); | |
for (var i = 0; i < count; i++) { | |
var obj = new TestObject() { | |
Id = b.ReadInt32(), | |
Name = b.ReadString(), | |
ModifiedTime = new DateTime(b.ReadInt64()), | |
Value = b.ReadDouble(), | |
Text = b.ReadString(), | |
Last = b.ReadInt32() | |
}; | |
objs.Add(obj); | |
} | |
if (objs.Count != n) throw new Exception(); | |
} | |
} | |
LogEnd(); | |
var info = new TypeInfo(typeof(TestObject)); | |
LogBegin("BinaryReader Reflection"); | |
using (var f = File.OpenRead(path + ".rbinary")) { | |
using (var b = new BinaryReader(f, Encoding.UTF8)) { | |
var count = b.ReadInt32(); | |
var objs = new List<TestObject>(count); | |
for (var i = 0; i < count; i++) { | |
var obj = new TestObject(); | |
info.Read(obj, b); | |
objs.Add(obj); | |
} | |
if (objs.Count != n) throw new Exception(); | |
} | |
} | |
LogEnd(); | |
} | |
void Run() { | |
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "test"); | |
var n = 5000; | |
try { | |
CreateFiles(n, path); | |
ReadFiles(n, path); | |
} | |
catch (Exception ex) { | |
Console.WriteLine (ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment