Skip to content

Instantly share code, notes, and snippets.

@TimSC
Last active August 29, 2015 14:00
Show Gist options
  • Save TimSC/9e4362c93fa3a57338e1 to your computer and use it in GitHub Desktop.
Save TimSC/9e4362c93fa3a57338e1 to your computer and use it in GitHub Desktop.
This script does test reads and writes to a file.
#This script does test reads and writes to a file.
#Release under CC0 by Tim Sheerman-Chase, 2014
#This script fails on my computer but not on others. Weird.
import random, time, pickle, sys
import numpy as np
def MultiRead(handle, maxLen):
buff = []
while 1:
if maxLen == 0:
return b"".join(buff)
dat = handle.read(maxLen)
if len(dat) > 0:
buff.append(dat)
maxLen -= len(dat)
else:
return b"".join(buff)
if __name__=="__main__":
outfina = "soaktest-native.dat"
if len(sys.argv)>1:
outfina = sys.argv[1]
fi = open(outfina, "w+b")
globalRandSeed = 0
random.seed(globalRandSeed)
np.random.seed(globalRandSeed)
numBins = 1000
binSizes = [random.randint(0,500000) for i in range(numBins)]
print("Total test size", sum(binSizes))
binOffsets = [0, ]
for bs in binSizes[:-1]:
val = binOffsets[-1]+bs
binOffsets.append(val)
oldData = None
oldSeed = None
while 1:
print("Begin iteration", globalRandSeed, "native")
globalRandSeed += 1
random.seed(globalRandSeed)
np.random.seed(globalRandSeed)
#Generate new data
data = []
for bs in binSizes:
binDat = np.random.bytes(bs)
data.append(binDat)
#Generate write order
accessOrder = list(range(numBins))[:]
random.shuffle(accessOrder)
for count, binNum in enumerate(accessOrder):
if oldData is not None:
fi.seek(binOffsets[binNum])
#readback = fi.read(binSizes[binNum])
readback = MultiRead(fi, binSizes[binNum])
checkok = (readback == oldData[binNum])
if not checkok:
print("Readback failed. saving to file.", binNum)
print("Matches", sum([x==y for x, y in zip(readback, oldData[binNum])]))
if sys.version_info[0] > 2:
print([c for c in oldData[binNum][:30]], len(oldData[binNum]))
print([c for c in readback[:30]], len(readback))
else:
print([ord(c) for c in oldData[binNum][:30]], len(oldData[binNum]))
print([ord(c) for c in readback[:30]], len(readback))
import pickle
pickle.dump(oldData[binNum], open("expected.dat","wb"), protocol=-1)
pickle.dump(readback, open("got.dat","wb"), protocol=-1)
fi.flush()
exit(0)
fi.seek(binOffsets[binNum])
fi.write(data[binNum])
#Flush every tenth interation to see if it makes a difference
if globalRandSeed % 10 == 0:
print("Flushing")
fi.flush()
oldData = data
oldSeed = globalRandSeed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment