2222package org .rocksdb .benchmark ;
2323
2424import java .lang .Runnable ;
25+ import java .lang .Math ;
2526import java .io .File ;
2627import java .nio .ByteBuffer ;
2728import java .util .Collection ;
@@ -240,7 +241,8 @@ public WriteTask(
240241 if (entriesPerBatch_ == 1 ) {
241242 for (long i = 0 ; i < numEntries_ ; ++i ) {
242243 getKey (key , i , keyRange_ );
243- db_ .put (writeOpt_ , key , DbBenchmark .this .gen_ .generate (valueSize_ ));
244+ DbBenchmark .this .gen_ .generate (value );
245+ db_ .put (writeOpt_ , key , value );
244246 stats_ .finishedSingleOp (keySize_ + valueSize_ );
245247 writeRateControl (i );
246248 if (isFinished ()) {
@@ -252,7 +254,8 @@ public WriteTask(
252254 WriteBatch batch = new WriteBatch ();
253255 for (long j = 0 ; j < entriesPerBatch_ ; j ++) {
254256 getKey (key , i + j , keyRange_ );
255- batch .put (key , DbBenchmark .this .gen_ .generate (valueSize_ ));
257+ DbBenchmark .this .gen_ .generate (value );
258+ db_ .put (writeOpt_ , key , value );
256259 stats_ .finishedSingleOp (keySize_ + valueSize_ );
257260 }
258261 db_ .write (writeOpt_ , batch );
@@ -473,7 +476,6 @@ public DbBenchmark(Map<Flag, Object> flags) throws Exception {
473476 "No compression is used.%n" ,
474477 compressionType_ , e .toString ());
475478 compressionType_ = "none" ;
476- compressionRatio_ = 1.0 ;
477479 }
478480 gen_ = new RandomGenerator (randSeed_ , compressionRatio_ );
479481 }
@@ -1522,24 +1524,54 @@ private static class RandomGenerator {
15221524 private final byte [] data_ ;
15231525 private int dataLength_ ;
15241526 private int position_ ;
1527+ private double compressionRatio_ ;
15251528 Random rand_ ;
15261529
15271530 private RandomGenerator (long seed , double compressionRatio ) {
15281531 // We use a limited amount of data over and over again and ensure
15291532 // that it is larger than the compression window (32KB), and also
1533+ byte [] value = new byte [100 ];
15301534 // large enough to serve all typical value sizes we want to write.
15311535 rand_ = new Random (seed );
1532- dataLength_ = 1048576 + 100 ;
1536+ dataLength_ = value . length * 10000 ;
15331537 data_ = new byte [dataLength_ ];
1534- // TODO(yhchiang): mimic test::CompressibleString?
1535- for (int i = 0 ; i < dataLength_ ; ++i ) {
1536- data_ [i ] = (byte ) (' ' + rand_ .nextInt (95 ));
1538+ compressionRatio_ = compressionRatio ;
1539+ int pos = 0 ;
1540+ while (pos < dataLength_ ) {
1541+ compressibleBytes (value );
1542+ System .arraycopy (value , 0 , data_ , pos ,
1543+ Math .min (value .length , dataLength_ - pos ));
1544+ pos += value .length ;
15371545 }
15381546 }
15391547
1540- private byte [] generate (int length ) {
1541- position_ = rand_ .nextInt (data_ .length - length );
1542- return Arrays .copyOfRange (data_ , position_ , position_ + length );
1548+ private void compressibleBytes (byte [] value ) {
1549+ int baseLength = value .length ;
1550+ if (compressionRatio_ < 1.0d ) {
1551+ baseLength = (int ) (compressionRatio_ * value .length + 0.5 );
1552+ }
1553+ if (baseLength <= 0 ) {
1554+ baseLength = 1 ;
1555+ }
1556+ int pos ;
1557+ for (pos = 0 ; pos < baseLength ; ++pos ) {
1558+ value [pos ] = (byte ) (' ' + rand_ .nextInt (95 )); // ' ' .. '~'
1559+ }
1560+ while (pos < value .length ) {
1561+ System .arraycopy (value , 0 , value , pos ,
1562+ Math .min (baseLength , value .length - pos ));
1563+ pos += baseLength ;
1564+ }
1565+ }
1566+
1567+ private void generate (byte [] value ) {
1568+ if (position_ + value .length > data_ .length ) {
1569+ position_ = 0 ;
1570+ assert (value .length <= data_ .length );
1571+ }
1572+ position_ += value .length ;
1573+ System .arraycopy (data_ , position_ - value .length ,
1574+ value , 0 , value .length );
15431575 }
15441576 }
15451577
0 commit comments