1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use chrono::Utc;
use qbsdiff::{Bsdiff, Bspatch, Compression, ParallelScheme};
use rand::random;
use std::fs;
use std::io;
use std::ffi::OsStr;
use std::process;
use std::path;
use std::path::Path;
use globwalk::glob;
use rand::distributions::uniform::{SampleUniform, Uniform};
use rand::prelude::*;

/// Options for qbsdiff.
#[derive(Copy, Clone, Debug)]
pub struct QbsdiffOptions {
    pub chunk_size: usize,
    pub small_match: usize,
    pub compression_level: Compression,
    pub buffer_size: usize,
}

impl Default for QbsdiffOptions {
    fn default() -> Self {
        QbsdiffOptions {
            chunk_size: 0,
            small_match: qbsdiff::bsdiff::SMALL_MATCH,
            compression_level: qbsdiff::bsdiff::LEVEL,
            buffer_size: qbsdiff::bsdiff::BUFFER_SIZE,
        }
    }
}

/// Options for qbspatch.
#[derive(Copy, Clone, Debug)]
pub struct QbspatchOptions {
    pub buffer_size: usize,
    pub delta_min: usize,
}

impl Default for QbspatchOptions {
    fn default() -> Self {
        QbspatchOptions {
            buffer_size: qbsdiff::bspatch::BUFFER_SIZE,
            delta_min: qbsdiff::bspatch::DELTA_MIN,
        }
    }
}

/// The testing context.
pub struct Testing {
    assets_dir: path::PathBuf,
}

impl Testing {
    /// Create new tesing context.
    pub fn new(assets_dir: path::PathBuf) -> Self {
        Testing { assets_dir }
    }

    /// Execute bsdiff command.
    pub fn bsdiff(&self, s: &[u8], t: &[u8]) -> io::Result<Vec<u8>> {
        let dir = self.assets_dir.join("bin");
        run_bsdiff_in(dir, s, t)
    }

    /// Execute bspatch command.
    pub fn bspatch(&self, s: &[u8], p: &[u8]) -> io::Result<Vec<u8>> {
        let dir = self.assets_dir.join("bin");
        run_bspatch_in(dir, s, p)
    }

    /// Perform qbsdiff.
    pub fn qbsdiff(&self, s: &[u8], t: &[u8]) -> io::Result<Vec<u8>> {
        let mut p = Vec::new();
        Bsdiff::new(s, t).compare(io::Cursor::new(&mut p))?;
        Ok(p)
    }

    /// Perform qbsdiff with options.
    pub fn qbsdiff_with(&self, s: &[u8], t: &[u8], opts: QbsdiffOptions) -> io::Result<Vec<u8>> {
        let mut p = Vec::new();
        Bsdiff::new(s, t)
            .parallel_scheme(ParallelScheme::ChunkSize(opts.chunk_size))
            .small_match(opts.small_match)
            .compression_level(opts.compression_level)
            .buffer_size(opts.buffer_size)
            .compare(io::Cursor::new(&mut p))?;
        Ok(p)
    }

    /// Perform qbspatch.
    pub fn qbspatch(&self, s: &[u8], p: &[u8]) -> io::Result<Vec<u8>> {
        let patcher = Bspatch::new(p)?;
        let mut t = Vec::with_capacity(patcher.hint_target_size() as usize);
        patcher.apply(s, io::Cursor::new(&mut t))?;
        Ok(t)
    }

    /// Perform qbspatch with options.
    pub fn qbspatch_with(&self, s: &[u8], p: &[u8], opts: QbspatchOptions) -> io::Result<Vec<u8>> {
        let patcher = Bspatch::new(p)?;
        let mut t = Vec::with_capacity(patcher.hint_target_size() as usize);
        patcher
            .buffer_size(opts.buffer_size)
            .delta_min(opts.delta_min)
            .apply(s, io::Cursor::new(&mut t))?;
        Ok(t)
    }

    /// Get regular samples.
    pub fn get_regular_samples(&self) -> io::Result<Vec<Sample>> {
        let dir = self.assets_dir.join("samples");
        get_samples_in(dir)
    }

    /// Get pathological samples.
    pub fn get_pathological_samples(&self) -> io::Result<Vec<Sample>> {
        let dir = self.assets_dir.join("pathological");
        get_samples_in(dir)
    }

    /// Prepare random samples if needed and get the sample list.
    pub fn get_random_samples(&self, descs: &[RandomSample]) -> io::Result<Vec<Sample>> {
        let dir = self.assets_dir.join("random");
        fs::create_dir_all(dir.as_path())?;
        get_random_caches_in(dir, descs)
    }

    /// Run bsdiff to generate patch if cache does not exist then load the cache.
    pub fn load_cached_patch(&self, sample: &Sample) -> io::Result<Vec<u8>> {
        if fs::metadata(sample.patch.as_path()).is_err() {
            let dir = self.assets_dir.join("bin");
            run_command_in(
                dir,
                "bsdiff",
                &[sample.source.as_os_str(), sample.target.as_os_str(), sample.patch.as_os_str()],
            )?;
        }
        fs::read(sample.patch.as_path())
    }
}

/// The benchmarking context.
pub struct Benchmarking {
    assets_dir: path::PathBuf,
}

impl Benchmarking {
    /// Create new benchmarking context.
    pub fn new(assets_dir: path::PathBuf) -> Self {
        Benchmarking { assets_dir }
    }

    /// Perform qbspatch via internal library calls.
    pub fn qbsdiff(&self, s: &[u8], t: &[u8]) -> io::Result<()> {
        Bsdiff::new(s, t)
            .parallel_scheme(ParallelScheme::Auto)
            .compare(io::sink())?;
        Ok(())
    }

    /// Perform qbsdiff with options.
    pub fn qbsdiff_with(&self, s: &[u8], t: &[u8], opts: QbsdiffOptions) -> io::Result<()> {
        Bsdiff::new(s, t)
            .parallel_scheme(ParallelScheme::ChunkSize(opts.chunk_size))
            .small_match(opts.small_match)
            .compression_level(opts.compression_level)
            .buffer_size(opts.buffer_size)
            .compare(io::sink())?;
        Ok(())
    }

    /// Perform qbspatch via internal library calls.
    pub fn qbspatch(&self, s: &[u8], p: &[u8]) -> io::Result<()> {
        Bspatch::new(p)?.apply(s, io::sink())?;
        Ok(())
    }

    /// Perform qbspatch with options.
    pub fn qbspatch_with(&self, s: &[u8], p: &[u8], opts: QbspatchOptions) -> io::Result<()> {
        Bspatch::new(p)?
            .buffer_size(opts.buffer_size)
            .delta_min(opts.delta_min)
            .apply(s, io::sink())?;
        Ok(())
    }

    /// Get regular samples.
    pub fn get_regular_samples(&self) -> io::Result<Vec<Sample>> {
        let dir = self.assets_dir.join("samples");
        get_samples_in(dir)
    }

    /// Get pathological samples.
    pub fn get_pathological_samples(&self) -> io::Result<Vec<Sample>> {
        let dir = self.assets_dir.join("pathological");
        get_samples_in(dir)
    }

    /// Prepare random samples if needed and get the sample list.
    pub fn get_random_samples(&self, descs: &[RandomSample]) -> io::Result<Vec<Sample>> {
        let dir = self.assets_dir.join("random").join("bench");
        fs::create_dir_all(dir.as_path())?;
        get_random_caches_in(dir, descs)
    }

    /// Run bsdiff to generate patch if cache does not exist then load the cache.
    pub fn load_cached_patch(&self, sample: &Sample) -> io::Result<Vec<u8>> {
        if fs::metadata(sample.patch.as_path()).is_err() {
            if self.should_use_bsdiff(sample) {
                let dir = self.assets_dir.join("bin");
                run_command_in(
                    dir,
                    "bsdiff",
                    &[sample.source.as_os_str(), sample.target.as_os_str(), sample.patch.as_os_str()],
                )?;
            } else {
                let mut patch = Vec::new();
                let source = fs::read(sample.source.as_path())?;
                let target = fs::read(sample.target.as_path())?;
                Bsdiff::new(&source[..], &target[..])
                    .compare(io::Cursor::new(&mut patch))?;
                patch.shrink_to_fit();
                fs::write(sample.patch.as_path(), &patch[..])?;
                return Ok(patch);
            }
        }
        fs::read(sample.patch.as_path())
    }

    fn should_use_bsdiff(&self, sample: &Sample) -> bool {
        sample.patch
            .parent()
            .and_then(|p| p.file_name())
            .map(|d| d == "samples")
            .unwrap_or(false)
    }
}

fn run_bsdiff_in<P: AsRef<Path>>(dir: P, s: &[u8], t: &[u8]) -> io::Result<Vec<u8>> {
    let spath = create_temp(s)?;
    let tpath = create_temp(t)?;
    let ppath = create_temp(b"")?;
    run_command_in(
        dir,
        "bsdiff",
        &[spath.as_os_str(), tpath.as_os_str(), ppath.as_os_str()],
    )?;
    fs::read(ppath)
}

fn run_bspatch_in<P: AsRef<Path>>(dir: P, s: &[u8], p: &[u8]) -> io::Result<Vec<u8>> {
    let spath = create_temp(s)?;
    let tpath = create_temp(b"")?;
    let ppath = create_temp(p)?;
    run_command_in(
        dir,
        "bspatch",
        &[spath.as_os_str(), tpath.as_os_str(), ppath.as_os_str()],
    )?;
    fs::read(tpath)
}

fn run_command_in<P, S>(dir: P, cmd: &str, args: &[S]) -> io::Result<()>
where
    P: AsRef<Path>,
    S: AsRef<OsStr>,
{
    let bin = get_binary_in(dir, cmd)?;
    let success = process::Command::new(bin)
        .args(args)
        .stdout(process::Stdio::null())
        .stderr(process::Stdio::null())
        .spawn()?
        .wait()?
        .success();
    if !success {
        return Err(io::Error::new(io::ErrorKind::Other, "command execution failed"));
    } else {
        Ok(())
    }
}

#[cfg(windows)]
fn get_binary_in<P: AsRef<Path>>(dir: P, name: &str) -> io::Result<path::PathBuf> {
    Ok(dir.as_ref().join(format!("{}.exe", name)))
}

#[cfg(unix)]
fn get_binary_in<P: AsRef<Path>>(dir: P, name: &str) -> io::Result<path::PathBuf> {
    use std::os::unix::fs::PermissionsExt;
    let bin = dir.as_ref().join(name);
    fs::set_permissions(bin.as_path(), fs::Permissions::from_mode(0o755))?;
    Ok(bin)
}

/// Test sample.
pub struct Sample {
    pub name: String,
    pub source: path::PathBuf,
    pub target: path::PathBuf,
    pub patch: path::PathBuf,
}

impl Sample {
    /// Load source data.
    pub fn load_source(&self) -> io::Result<Vec<u8>> {
        Ok(fs::read(self.source.as_path())?)
    }

    /// Load target data.
    pub fn load_target(&self) -> io::Result<Vec<u8>> {
        Ok(fs::read(self.target.as_path())?)
    }
}

fn get_samples_in<P: AsRef<Path>>(dir: P) -> io::Result<Vec<Sample>> {
    let mut samples = Vec::new();
    let pat = dir.as_ref().join("*.s");
    let walker;
    if let Some(p) = pat.to_str() {
        walker = glob(p).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
    } else {
        return Err(io::Error::new(
            io::ErrorKind::Other,
            "cannot convert to str",
        ));
    }
    for result in walker.into_iter() {
        let source = result
            .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
            .into_path();

        let name;
        let target;
        let patch;
        if let (Some(d), Some(n)) = (source.parent(), source.file_stem()) {
            let nbuf = n.to_owned();
            name = nbuf.to_string_lossy().into_owned();

            let mut tbuf = nbuf.clone();
            tbuf.push(".t");
            target = path::PathBuf::from(d).join(tbuf.as_os_str());

            let mut pbuf = nbuf.clone();
            pbuf.push(".p");
            patch = path::PathBuf::from(d).join(pbuf.as_os_str());
        } else {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "cannot make target or patch path",
            ));
        }
        
        if let Err(_) = fs::metadata(target.as_path()) {
            continue;
        }

        samples.push(Sample {
            name,
            source,
            target,
            patch,
        });
    }

    Ok(samples)
}

fn get_random_caches_in<P: AsRef<Path>>(dir: P, descs: &[RandomSample]) -> io::Result<Vec<Sample>> {
    fs::create_dir_all(dir.as_ref())?;

    let mut samples = Vec::new();
    for desc in descs.iter() {
        let source = dir.as_ref().join(format!("{}.s", desc.name));
        let sdata;
        if !exists_file(source.as_path()) {
            match desc.source {
                RandomSource::Bytes(bytes) => {
                    sdata = Vec::from(bytes);
                }
                RandomSource::Random(size) => {
                    sdata = random_bytes(size);
                }
            }
            fs::write(source.as_path(), &sdata[..])?;
        } else {
            sdata = fs::read(source.as_path())?;
        }

        for (id, tdesc) in desc.targets.iter().enumerate() {
            let target = dir.as_ref().join(format!("{}.{}.t", desc.name, tdesc.name(id)));
            if !exists_file(target.as_path()) {
                match tdesc {
                    RandomTarget::Bytes(bytes) => 
                        fs::write(target.as_path(), bytes)?,
                    RandomTarget::Distort(rate) =>
                        fs::write(target.as_path(), &distort(&sdata[..], *rate)[..])?,
                }
            }
            let patch = dir.as_ref().join(format!("{}.{}.p", desc.name, tdesc.name(id)));
            samples.push(Sample {
                name: format!("{}/{}", desc.name, tdesc.name(id)),
                source: source.clone(),
                target,
                patch,
            });
        }
    }

    Ok(samples)
}

/// Description of the random sample.
pub struct RandomSample {
    pub name: &'static str,
    pub source: RandomSource,
    pub targets: Vec<RandomTarget>,
}

/// Description of the source of random sample.
pub enum RandomSource {
    Bytes(&'static [u8]),
    Random(usize),
}

/// Description of a target of the random sample.
pub enum RandomTarget {
    Bytes(&'static [u8]),
    Distort(f64),
}

impl RandomTarget {
    fn name(&self, id: usize) -> String {
        match self {
            RandomTarget::Bytes(bytes) => format!("{}#bin{}", id, bytes.len()),
            RandomTarget::Distort(rate) => format!("{}#sim{}", id, (rate * 100.0) as u32),
        }
    }
}

/// Default random sample descriptions.
pub fn default_random_samples() -> Vec<RandomSample> {
    use RandomSource::{Bytes as SBytes, Random};
    use RandomTarget::{Bytes as TBytes, Distort};

    vec![
        RandomSample {
            name: "empty",
            source: SBytes(b""),
            targets: vec![TBytes(b""), TBytes(b"extra")],
        },
        RandomSample {
            name: "small",
            source: SBytes(
b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempo\
r incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis no\
strud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Dui\
s aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fu\
giat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in cu\
lpa qui officia deserunt mollit anim id est laborum."
            ),
            targets: vec![
                TBytes(b""),
                TBytes(
b"consectetur adip##cing elit, jed do eiusmod wir mussen wissen. wir werden wis\
sen/ laboris nisi ut al&^%ip ex ea coikodo consequat. "
                ),
                TBytes(b"the quick brown fox jumps over the lazy dog"),
                Distort(0.0),
                Distort(0.5),
                Distort(1.0),
            ],
        },
        RandomSample {
            name: "rand-4k",
            source: Random(4096),
            targets: vec![
                TBytes(b""),
                Distort(0.0),
                Distort(0.5),
                Distort(1.0),
            ],
        },
        RandomSample {
            name: "rand-256k",
            source: Random(256*1024),
            targets: vec![
                TBytes(b""),
                Distort(0.0),
                Distort(0.5),
                Distort(1.0),
            ],
        },
        RandomSample {
            name: "rand-1m",
            source: Random(1024*1024),
            targets: vec![
                TBytes(b""),
                Distort(0.0),
                Distort(0.5),
                Distort(1.0),
            ],
        },
        RandomSample {
            name: "rand-8m",
            source: Random(8*1024*1024),
            targets: vec![
                TBytes(b""),
                Distort(0.0),
                Distort(0.5),
                Distort(1.0),
            ],
        },
    ]
}

/// Default random benchmark samples.
pub fn default_random_bench_samples() -> Vec<RandomSample> {
    use RandomSource::Random;
    use RandomTarget::Distort;

    vec![
        RandomSample {
            name: "rand-512k",
            source: Random(512*1024),
            targets: vec![
                Distort(0.05),
                Distort(0.50),
                Distort(0.95),
            ],
        },
        RandomSample {
            name: "rand-4m",
            source: Random(4*1024*1024),
            targets: vec![
                Distort(0.05),
                Distort(0.50),
                Distort(0.95),
            ],
        },
    ]
}

fn random_bytes(n: usize) -> Vec<u8> {
    let mut rng = thread_rng();
    let mut bytes = Vec::with_capacity(n);
    for _ in 0..n {
        bytes.push(rng.gen())
    }
    bytes
}

fn distort(source: &[u8], similar: f64) -> Vec<u8> {
    let similar = fraction(similar);
    let rate = convex_mapping(similar);

    let tsize = random_between(
        (source.len() as f64 * 0.75) as usize,
        (source.len() as f64 * 1.25) as usize,
    );
    let dmax = random_between(
        Ord::min(16, (source.len() as f64 * 0.02) as usize),
        Ord::max(32, (source.len() as f64 * 0.33) as usize),
    );
    let emax = random_between(0, (source.len() as f64 * 0.15 * (1.0 - similar)) as usize);

    let mut target = Vec::with_capacity(tsize);
    let mut rng = thread_rng();
    while target.len() < tsize {
        // delta
        let remain = tsize - target.len();
        let dsize = {
            let dhi = Ord::min(Ord::min(dmax, remain), source.len());
            let dlo = Ord::min(16, dhi);
            random_between(dlo, dhi)
        };
        let offset = random_between(0, source.len() - dsize);
        for &x in source[offset..offset + dsize].iter() {
            if random_decide(rate) {
                target.push(x);
            } else {
                target.push(rng.gen());
            }
        }

        // extra
        let remain = tsize - target.len();
        if !random_decide(rate) {
            let esize = random_between(0, Ord::min(emax, remain));
            for _ in 0..esize {
                target.push(rng.gen());
            }
        }
    }

    target
}

fn random_decide(rate: f64) -> bool {
    random_between(0.0, 1.0) <= fraction(rate)
}

fn random_between<X: SampleUniform>(lo: X, hi: X) -> X {
    let mut rng = thread_rng();
    Uniform::new_inclusive(lo, hi).sample(&mut rng)
}

fn fraction(x: f64) -> f64 {
    if x.is_nan() || x.is_sign_negative() {
        0.0
    } else if x.is_infinite() || x > 1.0 {
        1.0
    } else {
        x
    }
}

fn convex_mapping(frac: f64) -> f64 {
    (1.0 - (1.0 - frac) * (1.0 - frac)).sqrt()
}

fn create_temp<B: AsRef<[u8]>>(bytes: B) -> io::Result<path::PathBuf> {
    let dir = std::env::temp_dir().join("qbsdiff-test");
    fs::create_dir_all(dir.as_path())?;

    let id = format!("{}-{:x}", Utc::now().format("%s.%f"), random::<u32>());
    let p = dir.join(id);

    fs::write(p.as_path(), bytes)?;
    Ok(p)
}

fn exists_file<P: AsRef<Path>>(name: P) -> bool {
    if let Ok(meta) = fs::metadata(name) {
        meta.is_file()
    } else {
        false
    }
}