github.com/loov/combiner@v0.1.0/extcombiner/latency_measure.go (about)

     1  // +build ignore
     2  
     3  package main
     4  
     5  import (
     6  	"bufio"
     7  	"compress/zlib"
     8  	"encoding/gob"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  	"sort"
    13  	"sync"
    14  	"time"
    15  
    16  	"github.com/loov/combiner/extcombiner"
    17  	"github.com/loov/combiner/testsuite"
    18  	"github.com/loov/hrtime"
    19  )
    20  
    21  func main() {
    22  	outputfile, err := os.Create("latency2.zgob")
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  	defer outputfile.Close()
    27  
    28  	bufferedfile := bufio.NewWriter(outputfile)
    29  	defer bufferedfile.Flush()
    30  
    31  	compressor := zlib.NewWriter(bufferedfile)
    32  	defer compressor.Close()
    33  
    34  	enc := gob.NewEncoder(compressor)
    35  
    36  	const N = 1000
    37  	const K = 1
    38  
    39  	params := testsuite.Params{
    40  		Procs:  []int{1, 4, 32, 256},
    41  		Bounds: []int{64},
    42  
    43  		WorkStart:  []int{100},
    44  		WorkDo:     []int{100},
    45  		WorkFinish: []int{1000},
    46  	}
    47  
    48  	params.Iterate(extcombiner.All, func(setup *testsuite.Setup) {
    49  		fmt.Print(setup.FullName(""), "\t")
    50  		hrs := make([]*hrtime.BenchmarkTSC, setup.Procs)
    51  		for i := range hrs {
    52  			hrs[i] = hrtime.NewBenchmarkTSC(N)
    53  		}
    54  
    55  		var wg sync.WaitGroup
    56  		wg.Add(setup.Procs)
    57  
    58  		_, combiner := setup.Make()
    59  		defer testsuite.StartClose(combiner)()
    60  
    61  		for i := 0; i < setup.Procs; i++ {
    62  			go func(b *hrtime.BenchmarkTSC) {
    63  				defer wg.Done()
    64  				v := int64(0)
    65  				for b.Next() {
    66  					for k := 0; k < K; k++ {
    67  						combiner.Do(v)
    68  						v++
    69  					}
    70  				}
    71  			}(hrs[i])
    72  		}
    73  
    74  		wg.Wait()
    75  
    76  		enc.Encode(setup)
    77  
    78  		average := time.Duration(0)
    79  		count := 0
    80  		var results [][]time.Duration
    81  		var all []time.Duration
    82  		for _, hr := range hrs {
    83  			laps := hr.Laps()
    84  			count += len(laps)
    85  			all = append(all, laps...)
    86  			for _, lap := range laps {
    87  				average += lap
    88  			}
    89  			results = append(results, laps)
    90  		}
    91  
    92  		sort.Slice(all, func(i, k int) bool { return all[i] < all[k] })
    93  		p := int(0.9999 * float64(len(all)))
    94  		fmt.Println(average/time.Duration(count), "\t", all[p])
    95  		enc.Encode(results)
    96  	})
    97  }