MySQL DBA
分类: NOSQL
2013-03-07 13:07:09
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MongoDB.Bson;
using MongoDB.Driver;
using System.IO;
插入测试函数:
public int mongodbconn(int couts)// test insert
{
try
{
string connectionstring = "mongodb://192.168.3.10";
MongoServer mserver = MongoServer.Create(connectionstring);
MongoDatabase db = mserver.GetDatabase("test");
MongoCollection<BsonDocument> mc = db.GetCollection<BsonDocument>("test");
DateTime begintime = DateTime.Now;
writefile("bgn: "+begintime.ToString());
int i;
for ( i = 1; i <= couts; i++)
{
BsonDocument bd = new BsonDocument
{
{"_id",i},
{"id",i},
{ "name", "c#" },
{ "title", "testing inert data " }
};
mc.Insert(bd);
DateTime midtime = DateTime.Now;
if((i%10000)==0)
{
writefile("row: "+i);
TimeSpan tso = midtime.Subtract(begintime);
string stimeo = tso.TotalMilliseconds.ToString();
writefile("mid: " + stimeo+" ms");
}
}
DateTime endtime = DateTime.Now;
writefile("end: "+endtime.ToString());
TimeSpan ts = endtime.Subtract(begintime);
string stime=ts.TotalMilliseconds.ToString();
writefile("use " + stime + " ms to insert "+i+" rows");
return 1;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return 0;
}
}
日志函数:
public static bool writefile(string str) //logifile
{
StreamWriter sw = new StreamWriter("f:\\mongodb\\mongodb.txt", true);
sw.WriteLine(str);
sw.Close();
return true;
}
查询测试函数:
public bool query(int type)
{
try
{
string sql;
long counts=0;
string connectionstring = "mongodb://192.168.3.10";
MongoServer mserver = MongoServer.Create(connectionstring);
MongoDatabase db = mserver.GetDatabase("test");
MongoCollection<BsonDocument> mc = db.GetCollection<BsonDocument>("test");
if (type == 1)
{
sql = textBox3.Text;
}
else
{
sql = textBox2.Text;
}
DateTime qtime = DateTime.Now;
var query = new QueryDocument("_id",Convert.ToInt32(sql));
writefile("begin: " + qtime.ToString());
// BsonDocument result=mc.FindOne(query);
foreach (BsonDocument emp in mc.Find(query).SetLimit(100))
{
counts = counts + 1;
string rename = emp["name"].AsString;
string revalue = emp["title"].AsString;
writefile(rename + " "+revalue);
}
DateTime etime = DateTime.Now;
TimeSpan ts = etime.Subtract(qtime);
string stime = ts.TotalMilliseconds.ToString();
writefile("use " + stime + " ms to query "+counts+"data rows");
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}