Giter Site home page Giter Site logo

mydb's Introduction

mydb

mydb是一个golang写的键值存储引擎,基于b+树,mmap

怎样使用:

package main

import (
	"fmt"
	"strconv"

	"github.com/alberliu/mydb"
)

func toBytes(i int) []byte {
	return []byte(strconv.Itoa(i))
}

func main() {
	db, err := mydb.Open("data")
	if err != nil {
		panic(err)
	}

	fmt.Println("init: ", db.Range(mydb.Infinity, mydb.Infinity))

	for i := 1; i <= 5; i++ {
		_, _ = db.Set(toBytes(i), toBytes(i))
	}
	fmt.Println("set:  ", db.Range(mydb.Infinity, mydb.Infinity))

	_ = db.Delete(toBytes(1))
	fmt.Println("delete", db.Range(mydb.Infinity, mydb.Infinity))

	fmt.Println("range ", db.Range(toBytes(3), toBytes(4)))
}

简单性能测试

测试环境:

goos: darwin
goarch: amd64
pkg: github.com/alberliu/mydb
cpu: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz

插入100万数据

测试代码:

func Test_tree_get_init_data(t *testing.T) {
	tree := newDefaultTree()
	now := time.Now()
	for i := 1; i <= 1000000; i++ {
		if i%100000 == 0 {
			t.Log(i)
		}

		data := []byte(strconv.Itoa(i))
		tree.set(data, data)
	}

	t.Logf("cost:%v tps:%v", time.Now().Sub(now), 1000000/time.Now().Sub(now).Seconds())
	t.Log(tree.fm.statisticsPage())
}

测试结果:

=== RUN   Test_tree_get_init_data
    tree_test.go:296: 100000
    tree_test.go:296: 200000
    tree_test.go:296: 300000
    tree_test.go:296: 400000
    tree_test.go:296: 500000
    tree_test.go:296: 600000
    tree_test.go:296: 700000
    tree_test.go:296: 800000
    tree_test.go:296: 900000
    tree_test.go:296: 1000000
    tree_test.go:303: cost:28.372586518s tps:35245.288388313376
    tree_test.go:304: fileSize:44253184B, totalPageNum:10804, branchPageNum:126, leafPageNum:10677, recyclePageNum:0, depth:3, recordNum:1000000
--- PASS: Test_tree_get_init_data (28.72s)

100万数据下的随机查询

测试代码:

func Benchmark_tree_get(b *testing.B) {
	fm, err := newFileManager("data.txt", defaultPageSize)
	if err != nil {
		panic(err)
	}
	tree := newTree(fm)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		tree.get([]byte(strconv.Itoa(rand.Intn(1000000))))
	}
}

测试结果:

Benchmark_tree_get
Benchmark_tree_get-6   	   49418	     23364 ns/op

总结:在以上描述的场景下,写入性能35245次每秒,查询性能42800次每秒

mydb's People

Contributors

alberliu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.