Chinaunix首页 | 论坛 | 博客
  • 博客访问: 277615
  • 博文数量: 52
  • 博客积分: 120
  • 博客等级: 民兵
  • 技术积分: 1189
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-03 15:41
个人简介

MySQL DBA

文章分类

全部博文(52)

文章存档

2013年(51)

2011年(1)

分类: 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;

            }

        }

   

阅读(1745) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~