Example #1
0
func TestCorrectness() {
	fmt.Printf("Correctness test\n")

	for ii := 0; ii < 20; ii++ {
		rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
		fmt.Printf("\nTest %v\n", ii)
		var arr []int

		if ii == 0 {
			arr = []int{0, 0, 1, 1, 2, 2, 3, 3}
		} else if ii == 1 {
			arr = make([]int, 500)

			for i := range arr {
				arr[i] = 8
			}

			arr[0] = 1
		} else if ii == 2 {
			arr = []int{0, 1, 2, 2, 2, 2, 7, 9, 9, 16, 16, 16, 1, 3,
				3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
		} else {
			arr = make([]int, 1024)
			idx := 0

			for idx < len(arr) {
				length := rnd.Intn(270)

				if length%3 == 0 {
					length = 1
				}

				val := rand.Intn(256) - 128
				end := idx + length

				if end >= len(arr) {
					end = len(arr) - 1
				}

				for j := idx; j < end; j++ {
					arr[j] = val
				}

				idx += length
				fmt.Printf("%v (%v) ", val, length)

			}
		}

		size := len(arr)
		input := make([]byte, size)
		output := make([]byte, 32+size*4/3)
		reverse := make([]byte, size)

		for i := range output {
			output[i] = 0xAA
		}

		for i := range arr {
			input[i] = byte(arr[i])
		}

		snappy, _ := function.NewSnappyCodec(0)
		fmt.Printf("\nOriginal: ")

		for i := range arr {
			fmt.Printf("%v ", input[i])
		}

		srcIdx, dstIdx, err := snappy.Forward(input, output)

		if err != nil {
			fmt.Printf("\n===Encoding error===\n%v\n", err)
			os.Exit(1)
		}

		if srcIdx != uint(len(input)) {
			fmt.Printf("\nNo compression (ratio > 1.0), skip reverse")
			continue
		}

		fmt.Printf("\nCoded: ")

		for i := uint(0); i < dstIdx; i++ {
			fmt.Printf("%v ", output[i])
		}

		// Required to reset internal attributes
		snappy, _ = function.NewSnappyCodec(dstIdx)

		_, _, err = snappy.Inverse(output, reverse)

		if err != nil {
			fmt.Printf("\n===Decoding error===\n%v\n", err)
			os.Exit(1)
		}

		fmt.Printf("\nDecoded: ")

		for i := range reverse {
			fmt.Printf("%v ", reverse[i])
		}

		fmt.Printf("\n")

		// Check
		for i := range reverse {
			if input[i] != reverse[i] {
				fmt.Printf("Different (index %v - %v)\n", input[i], reverse[i])
				os.Exit(1)
			}
		}

		fmt.Printf("Identical\n")
	}
}
Example #2
0
func TestSpeed() {
	iter := 50000
	size := 30000
	fmt.Printf("\n\nSpeed test\n")
	fmt.Printf("Iterations: %v\n", iter)

	for jj := 0; jj < 3; jj++ {
		input := make([]byte, size)
		output := make([]byte, 32+size*4/3)
		reverse := make([]byte, size)

		// Generate random data with runs
		n := 0
		delta1 := int64(0)
		delta2 := int64(0)

		for n < len(input) {
			val := byte(rand.Intn(3))
			input[n] = val
			n++
			run := rand.Intn(255)
			run -= 200
			run--

			for run > 0 && n < len(input) {
				input[n] = val
				n++
				run--
			}
		}

		var dstIdx uint
		var err error

		for ii := 0; ii < iter; ii++ {
			snappy, _ := function.NewSnappyCodec(0)
			before := time.Now()

			_, dstIdx, err = snappy.Forward(input, output)

			if err != nil {
				fmt.Printf("Encoding error%v\n", err)
				os.Exit(1)
			}

			after := time.Now()
			delta1 += after.Sub(before).Nanoseconds()
		}

		for ii := 0; ii < iter; ii++ {
			snappy, _ := function.NewSnappyCodec(dstIdx)
			before := time.Now()

			if _, _, err = snappy.Inverse(output, reverse); err != nil {
				fmt.Printf("Decoding error%v\n", err)
				os.Exit(1)
			}

			after := time.Now()
			delta2 += after.Sub(before).Nanoseconds()
		}

		idx := -1

		// Sanity check
		for i := range input {
			if input[i] != reverse[i] {
				idx = i
				break
			}
		}

		if idx >= 0 {
			fmt.Printf("Failure at index %v (%v <-> %v)\n", idx, input[idx], reverse[idx])
			os.Exit(1)
		}

		prod := int64(iter) * int64(size)
		fmt.Printf("\nSnappy encoding [ms]: %v", delta1/1000000)
		fmt.Printf("\nThroughput [MB/s]   : %d", prod*1000000/delta1*1000/(1024*1024))
		fmt.Printf("\nSnappy decoding [ms]: %v", delta2/1000000)
		fmt.Printf("\nThroughput [MB/s]   : %d", prod*1000000/delta2*1000/(1024*1024))
		println()
	}
}