Created
November 25, 2018 21:15
-
-
Save starius/e10685f228437ff531efd42fefa08a9b 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" | |
"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 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) | |
} | |
t1 := time.Now() | |
for _, i := range toWrite { | |
write(old, i) | |
} | |
fmt.Println("old write:", time.Since(t1)/time.Duration(len(toWrite))) | |
t1 = time.Now() | |
for _, i := range toWrite { | |
read(old, i) | |
} | |
fmt.Println("old read:", time.Since(t1)/time.Duration(len(toWrite))) | |
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)) | |
t1 = time.Now() | |
for _, i := range toWrite { | |
read(withPunchHoles, i) | |
} | |
fmt.Println("withPunchHoles 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