Created
November 27, 2018 10:32
-
-
Save starius/d6901ee2500d0b0bf3c3484b4a8fcec6 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"math/rand" | |
"os" | |
"time" | |
"gitlab.com/NebulousLabs/Sia/modules" | |
) | |
var ( | |
nsectors = flag.Int("nsectors", 100, "Total number of sectors (only 10-20% are used)") | |
) | |
func write(f io.WriterAt, i int) { | |
buffer := make([]byte, modules.SectorSize) | |
r := rand.New(rand.NewSource(int64(i))) | |
if n, err := r.Read(buffer); err != nil || n != len(buffer) { | |
panic("!!!") | |
} | |
if n, err := f.WriteAt(buffer, int64(i)*int64(modules.SectorSize)); err != nil || n != len(buffer) { | |
panic("!!!") | |
} | |
} | |
func punch(f modules.File, i int) { | |
if err := f.PunchHole(int64(i)*int64(modules.SectorSize), int64(modules.SectorSize)); err != nil { | |
panic(err) | |
} | |
} | |
func read(f io.ReaderAt, i int) { | |
buffer := make([]byte, modules.SectorSize) | |
if n, err := f.ReadAt(buffer, int64(i)*int64(modules.SectorSize)); err != nil || n != len(buffer) { | |
panic("!!!") | |
} | |
} | |
func sync(f modules.File) { | |
if err := f.Sync(); err != nil { | |
panic(err) | |
} | |
if err := ioutil.WriteFile("/proc/sys/vm/drop_caches", []byte("1"), 0644); err != nil { | |
panic(err) | |
} | |
} | |
func main() { | |
flag.Parse() | |
r := rand.New(rand.NewSource(42)) | |
allSectors := r.Perm(*nsectors) | |
toWrite := allSectors[:(*nsectors / 10)] | |
holes := allSectors[(*nsectors / 10):(2 * *nsectors / 10)] | |
var toWrite2 []int | |
toWrite2 = append(toWrite2, toWrite...) | |
toWrite2 = append(toWrite2, holes...) | |
toWrite2 = append(toWrite2, holes...) | |
r.Shuffle(len(toWrite2), func(i, j int) { | |
toWrite2[i], toWrite2[j] = toWrite2[j], toWrite2[i] | |
}) | |
old, err := modules.ProdDependencies.OpenFile("old", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0700) | |
if err != nil { | |
panic(err) | |
} | |
withPunchHoles, err := modules.ProdDependencies.OpenFile("withPunchHoles", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0700) | |
if err != nil { | |
panic(err) | |
} | |
sync(withPunchHoles) | |
var t1 time.Time | |
var totalWrite, totalPunch time.Duration | |
var writeCount, punchCount int | |
seen := make(map[int]struct{}) | |
for _, i := range toWrite2 { | |
t1 = time.Now() | |
if _, has := seen[i]; !has { | |
seen[i] = struct{}{} | |
write(withPunchHoles, i) | |
totalWrite += time.Since(t1) | |
writeCount++ | |
} else { | |
punch(withPunchHoles, i) | |
totalPunch += time.Since(t1) | |
punchCount++ | |
} | |
} | |
fmt.Println("withPunchHoles write:", totalWrite/time.Duration(writeCount)) | |
fmt.Println("withPunchHoles punch:", totalPunch/time.Duration(punchCount)) | |
sync(withPunchHoles) | |
t1 = time.Now() | |
for _, i := range toWrite { | |
read(withPunchHoles, i) | |
} | |
fmt.Println("withPunchHoles read:", time.Since(t1)/time.Duration(len(toWrite))) | |
sync(withPunchHoles) | |
sync(old) | |
t1 = time.Now() | |
for _, i := range toWrite { | |
write(old, i) | |
} | |
fmt.Println("old write:", time.Since(t1)/time.Duration(len(toWrite))) | |
sync(old) | |
t1 = time.Now() | |
for _, i := range toWrite { | |
read(old, i) | |
} | |
fmt.Println("old read:", time.Since(t1)/time.Duration(len(toWrite))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment