|
| 1 | +package flatfs |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "math/rand" |
| 7 | + "time" |
| 8 | + "fmt" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + ds "github.com/ipfs/go-datastore" |
| 13 | + dsq "github.com/ipfs/go-datastore/query" |
| 14 | +) |
| 15 | + |
| 16 | +var N = 10000 |
| 17 | + |
| 18 | +func TimeQuery() { |
| 19 | + rand.Seed(time.Now().UTC().UnixNano()) |
| 20 | + path, err := ioutil.TempDir("/tmp", "flatdb_") |
| 21 | + if err != nil { |
| 22 | + panic(err) |
| 23 | + } |
| 24 | + |
| 25 | + d, err := New(path, 5, false) |
| 26 | + if err != nil { |
| 27 | + panic(err) |
| 28 | + } |
| 29 | + defer func() { |
| 30 | + os.RemoveAll(path) |
| 31 | + d.Close() |
| 32 | + }() |
| 33 | + for n := 0; n < N; n++ { |
| 34 | + err := d.Put(ds.NewKey(RandomString(52)), []byte(RandomString(1000))) |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + //TestQuery(d) |
| 41 | + //TestQuery(d) |
| 42 | + //TestQuery(d) |
| 43 | + // TestDirect(d) |
| 44 | + // TestIterator(d) |
| 45 | + // TestQuery(d) |
| 46 | + //TestDirect(d) |
| 47 | + //TestDirect(d) |
| 48 | + //TestDirect(d) |
| 49 | + // TestQuery(d) |
| 50 | + // TestDirect(d) |
| 51 | + // TestQuery(d) |
| 52 | + // TestDirect(d) |
| 53 | + // TestQuery(d) |
| 54 | + // TestDirect(d) |
| 55 | + // TestDirect(d) |
| 56 | + // TestDirect(d) |
| 57 | + // TestIterator(d) |
| 58 | + // TestIterator(d) |
| 59 | + // TestIterator(d) |
| 60 | + // TestIterator(d) |
| 61 | + // TestQuery(d) |
| 62 | + // TestQuery(d) |
| 63 | + // TestQuery(d) |
| 64 | + for i := 0; i <= 100; i++ { |
| 65 | + dsq.KeysOnlyBufSize = 1 << uint(rand.Intn(15)) |
| 66 | + if dsq.KeysOnlyBufSize <= 8192 { |
| 67 | + TestQuery(d) |
| 68 | + } else { |
| 69 | + TestDirect(d) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestQuery(d *Datastore) { |
| 75 | + start := time.Now() |
| 76 | + rs, err := d.Query(dsq.Query{KeysOnly: true}) |
| 77 | + if err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + i := 0 |
| 81 | + for r := range rs.Next() { |
| 82 | + i += int(r.Key[0]) |
| 83 | + } |
| 84 | + elapsed := time.Since(start) |
| 85 | + //fmt.Printf("i = %d\n", i) |
| 86 | + fmt.Printf("query %d %f\n", dsq.KeysOnlyBufSize, elapsed.Seconds()*1000) |
| 87 | +} |
| 88 | + |
| 89 | +func TestDirect(fs *Datastore) { |
| 90 | + start := time.Now() |
| 91 | + i := 0 |
| 92 | + err := filepath.Walk(fs.path, func(path string, info os.FileInfo, err error) error { |
| 93 | + if err != nil { |
| 94 | + log.Errorf("Walk func in Query got error: %v", err) |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + if !info.Mode().IsRegular() || strings.HasPrefix(info.Name(), ".") { |
| 99 | + return nil |
| 100 | + } |
| 101 | + |
| 102 | + key, ok := fs.decode(info.Name()) |
| 103 | + if !ok { |
| 104 | + log.Warning("failed to decode entry in flatfs") |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + i += int(key.String()[0]) |
| 109 | + |
| 110 | + return nil |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + log.Warning("walk failed: ", err) |
| 114 | + } |
| 115 | + elapsed := time.Since(start) |
| 116 | + //fmt.Printf("i = %d\n", i) |
| 117 | + fmt.Printf("direct %d %f\n", dsq.KeysOnlyBufSize, elapsed.Seconds()*1000) |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +func RandomString(strlen int) string { |
| 124 | + const chars = "abcdefghijklmnopqrstuvwxyz0123456789" |
| 125 | + result := make([]byte, strlen) |
| 126 | + for i := 0; i < strlen; i++ { |
| 127 | + result[i] = chars[rand.Intn(len(chars))] |
| 128 | + } |
| 129 | + return string(result) |
| 130 | +} |
| 131 | + |
0 commit comments