"Fossies" - the Fresh Open Source Software Archive

Member "xdelta3-3.1.0/go/src/regtest.go" (7 Jan 2016, 7526 Bytes) of package /linux/misc/xdelta3-3.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package main
    2 
    3 import (
    4     "fmt"
    5     "io"
    6     "path"
    7     "os"
    8     "sort"
    9     "time"
   10 
   11     "xdelta"
   12 )
   13 
   14 const (
   15     xdataset = "/volume/home/jmacd/src/testdata"
   16     xcompare = "/volume/home/jmacd/src/xdelta-devel/xdelta3/build/x86_64-pc-linux-gnu-m64/xoff64/xdelta3"
   17     xdelta3  = "/volume/home/jmacd/src/xdelta-64bithash/xdelta3/build/x86_64-pc-linux-gnu-m64/usize64/xoff64/xdelta3"
   18     seed = 1422253499919909358
   19 )
   20 
   21 type Config struct {
   22     srcbuf_size int64
   23     window_size int64
   24     blocksize   int
   25 }
   26 
   27 func NewC() Config {
   28     // TODO make these (and above) flags
   29     return Config{1<<26, 1<<22, 1<<16}
   30 }
   31 
   32 func (c Config) smokeTest(t *xdelta.TestGroup, p xdelta.Program) {
   33     target := "Hello world!"
   34     source := "Hello world, nice to meet you!"
   35 
   36     enc, err := t.Exec("encode", p, true, []string{"-e"})
   37     if err != nil {
   38         t.Panic(err)
   39     }
   40     dec, err := t.Exec("decode", p, true, []string{"-d"})
   41     if err != nil {
   42         t.Panic(err)
   43     }
   44 
   45     encodeout := t.Drain(enc.Stdout, "encode.stdout")
   46     decodeout := t.Drain(dec.Stdout, "decode.stdout")
   47 
   48     t.Empty(enc.Stderr, "encode")
   49     t.Empty(dec.Stderr, "decode")
   50 
   51     t.TestWrite("encode.stdin", enc.Stdin, []byte(target))
   52     t.TestWrite("encode.srcin", enc.Srcin, []byte(source))
   53 
   54     t.TestWrite("decode.stdin", dec.Stdin, <-encodeout)
   55     t.TestWrite("decode.srcin", dec.Srcin, []byte(source))
   56 
   57     if do := string(<-decodeout); do != target {
   58         t.Panic(fmt.Errorf("It's not working! %s\n!=\n%s\n", do, target))
   59     }
   60     t.Wait(enc, dec)
   61 }
   62 
   63 type PairTest struct {
   64     // Input
   65     Config
   66     program xdelta.Program
   67     source, target string
   68 
   69     // Output
   70     TestOutput
   71 }
   72 
   73 type TestOutput struct {
   74     encoded int64
   75     encDuration time.Duration
   76     decDuration time.Duration
   77     encSysDuration time.Duration
   78     decSysDuration time.Duration
   79 }
   80 
   81 func (to *TestOutput) Add(a TestOutput) {
   82     to.encoded += a.encoded
   83     to.encDuration += a.encDuration
   84     to.decDuration += a.decDuration
   85     to.encSysDuration += a.encSysDuration
   86     to.decSysDuration += a.decSysDuration
   87 }
   88 
   89 func (to *TestOutput) String() string {
   90     return fmt.Sprintf("SIZE: %v\tT: %v\tTSYS: %v\tDT: %v\tDTSYS: %v",
   91         to.encoded, to.encDuration, to.encSysDuration, to.decDuration, to.encSysDuration)
   92 }
   93 
   94 // P is the test program, Q is the reference version.
   95 func (cfg Config) datasetTest(t *xdelta.TestGroup, p, q xdelta.Program) {
   96     dir, err := os.Open(xdataset)
   97     if err != nil {
   98         t.Panic(err)
   99     }
  100     dents, err := dir.Readdir(-1)
  101     if err != nil {
  102         t.Panic(err)
  103     }
  104     paths := make([]string, len(dents))
  105     var total int64
  106     for i, d := range dents {
  107         if !d.Mode().IsRegular() {
  108             continue
  109         }
  110         paths[i] = fmt.Sprint(xdataset, "/", d.Name())
  111         total += d.Size()
  112     }
  113     meansize := total / int64(len(dents))
  114     largest  := uint(20)
  115     for ; largest <= 31 && 1<<largest < meansize; largest++ {}
  116 
  117     sort.Strings(paths)
  118 
  119     testSum := map[uint]*TestOutput{}
  120     compSum := map[uint]*TestOutput{}
  121 
  122     for _, in1 := range paths {
  123         for _, in2 := range paths {
  124             if in1 == in2 { continue }
  125 
  126             // 1/4, 1/2, and 1 of the power-of-2 rounded-up mean size
  127             for b := largest - 2; b <= largest; b++ {
  128                 if _, has := testSum[b]; !has {
  129                     testSum[b] = &TestOutput{}
  130                     compSum[b] = &TestOutput{}
  131                 }
  132                 c1 := cfg
  133                 c1.srcbuf_size = 1<<b
  134                 ptest := &PairTest{c1, p, in1, in2, TestOutput{-1, 0, 0, 0, 0}}
  135                 ptest.datasetPairTest(t, 1<<b);
  136                 qtest := &PairTest{c1, q, in1, in2, TestOutput{-1, 0, 0, 0, 0}}
  137                 qtest.datasetPairTest(t, 1<<b)
  138 
  139                 testSum[b].Add(ptest.TestOutput)
  140                 compSum[b].Add(qtest.TestOutput)
  141 
  142                 fmt.Printf("%s, %s: %.2f%% %+d/%d\n\tE:%.2f%%/%s(%.2f%%/%s) D:%.2f%%/%s(%.2f%%/%s) [B=%d]\n",
  143                     path.Base(in1), path.Base(in2),
  144                     float64(ptest.encoded - qtest.encoded) * 100.0 / float64(qtest.encoded),
  145                     ptest.encoded - qtest.encoded,
  146                     qtest.encoded,
  147                     (ptest.encDuration - qtest.encDuration).Seconds() * 100.0 / qtest.encDuration.Seconds(),
  148                     qtest.encDuration,
  149                     (ptest.decDuration - qtest.decDuration).Seconds() * 100.0 / qtest.decDuration.Seconds(),
  150                     qtest.encDuration,
  151                     (ptest.encSysDuration - qtest.encSysDuration).Seconds() * 100.0 / qtest.encSysDuration.Seconds(),
  152                     qtest.encSysDuration,
  153                     (ptest.decSysDuration - qtest.decSysDuration).Seconds() * 100.0 / qtest.decSysDuration.Seconds(),
  154                     qtest.decSysDuration,
  155                     1<<b)
  156             }
  157         }
  158     }
  159     var keys []uint
  160     for k, _ := range testSum {
  161         keys = append(keys, k)
  162     }
  163     for _, k := range keys {        
  164         fmt.Printf("B=%v\nTEST: %v\nCOMP: %v\n", 1<<k, testSum[k], compSum[k])
  165     }
  166 }
  167 
  168 func (pt *PairTest) datasetPairTest(t *xdelta.TestGroup, meanSize int64) {
  169     cfg := pt.Config
  170     eargs := []string{"-e", fmt.Sprint("-B", cfg.srcbuf_size), // "-q",
  171         fmt.Sprint("-W", cfg.window_size), "-s", pt.source,
  172         "-I0", "-S", "none", pt.target}
  173     enc, err := t.Exec("encode", pt.program, false, eargs)
  174     if err != nil {
  175         t.Panic(err)
  176     }
  177 
  178     dargs := []string{"-dc", fmt.Sprint("-B", cfg.srcbuf_size), //"-q",
  179         fmt.Sprint("-W", cfg.window_size), "-s", pt.source,
  180         "-S", "none"}
  181 
  182     dec, err := t.Exec("decode", pt.program, false, dargs)
  183     if err != nil {
  184         t.Panic(err)
  185     }
  186     tgt_check, err := os.Open(pt.target)
  187     if err != nil {
  188         t.Panic(err)
  189     }
  190     tgt_info, err := tgt_check.Stat()
  191     if err != nil {
  192         t.Panic(err)
  193     }
  194     t.Empty(enc.Stderr, "encode")
  195     t.Empty(dec.Stderr, "decode")
  196     t.CopyStreams(enc.Stdout, dec.Stdin, &pt.encoded)
  197     t.CompareStreams(dec.Stdout, tgt_check, tgt_info.Size())
  198 
  199     t.Wait(enc, dec)
  200 
  201     pt.decDuration = dec.Cmd.ProcessState.UserTime()
  202     pt.encDuration = enc.Cmd.ProcessState.UserTime()
  203     pt.decSysDuration = dec.Cmd.ProcessState.SystemTime()
  204     pt.encSysDuration = enc.Cmd.ProcessState.SystemTime()
  205 }
  206 
  207 func (cfg Config) offsetTest(t *xdelta.TestGroup, p xdelta.Program, offset, length int64) {
  208     eargs := []string{"-e", "-0", fmt.Sprint("-B", cfg.srcbuf_size), "-q",
  209         fmt.Sprint("-W", cfg.window_size)}
  210     enc, err := t.Exec("encode", p, true, eargs)
  211     if err != nil {
  212         t.Panic(err)
  213     }
  214     
  215     dargs := []string{"-d", fmt.Sprint("-B", cfg.srcbuf_size), "-q",
  216         fmt.Sprint("-W", cfg.window_size)}
  217     dec, err := t.Exec("decode", p, true, dargs)
  218     if err != nil {
  219         t.Panic(err)
  220     }
  221 
  222     // The pipe used to read the decoder output and compare
  223     // against the target.
  224     read, write := io.Pipe()
  225 
  226     t.Empty(enc.Stderr, "encode")
  227     t.Empty(dec.Stderr, "decode")
  228 
  229     var encoded_size int64
  230     t.CopyStreams(enc.Stdout, dec.Stdin, &encoded_size)
  231     t.CompareStreams(dec.Stdout, read, length)
  232 
  233     // The decoder output ("read", above) is compared with the
  234     // test-provided output ("write", below).  The following
  235     // generates two identical inputs.
  236     t.WriteRstreams("encode", seed, offset, length, enc.Srcin, enc.Stdin)
  237     t.WriteRstreams("decode", seed, offset, length, dec.Srcin, write)
  238     t.Wait(enc, dec)
  239 
  240     expect := cfg.srcbuf_size - offset
  241     if float64(encoded_size) < (0.95 * float64(expect)) ||
  242         float64(encoded_size) > (1.05 * float64(expect)) {
  243         t.Fail("encoded size should be ~=", expect, ", actual ", encoded_size)
  244     }
  245 }
  246 
  247 func main() {
  248     r, err := xdelta.NewRunner()
  249     if err != nil {
  250         panic(err)
  251     }
  252     defer r.Cleanup()
  253 
  254     cfg := NewC()
  255 
  256     prog := xdelta.Program{xdelta3}
  257 
  258     r.RunTest("smoketest", func(t *xdelta.TestGroup) { cfg.smokeTest(t, prog) })
  259 
  260     for i := uint(29); i <= 33; i += 1 {
  261         // The arguments to offsetTest are offset, source
  262         // window size, and file size. The source window size
  263         // is (2 << i) and (in the 3.0x release branch) is
  264         // limited to 2^31, so the the greatest value of i is
  265         // 30.
  266         cfg.srcbuf_size = 2 << i
  267         r.RunTest(fmt.Sprint("offset", i), func(t *xdelta.TestGroup) {
  268             cfg.offsetTest(t, prog, 1 << i, 3 << i) })
  269     }
  270     
  271     comp := xdelta.Program{xcompare}
  272 
  273     r.RunTest("dataset", func(t *xdelta.TestGroup) { cfg.datasetTest(t, prog, comp) })
  274 }