text:
/*
Package main provides benchmark with setting flags.

An example to run some benchmarks with profiling enabled:

go run benchmark/benchmain/main.go -benchtime=10s -workloads=all \
  -compression=gzip -maxConcurrentCalls=1 -trace=off \
  -reqSizeBytes=1,1048576 -respSizeBytes=1,1048576 -networkMode=Local \
  -cpuProfile=cpuProf -memProfile=memProf -memProfileRate=10000 -resultFile=result

As a suggestion, when creating a branch, you can run this benchmark and save the result
file "-resultFile=basePerf", and later when you at the middle of the work or finish the
work, you can get the benchmark result and compare it with the base anytime.

Assume there are two result files names as "basePerf" and "curPerf" created by adding
-resultFile=basePerf and -resultFile=curPerf.
	To format the curPerf, run:
  	go run benchmark/benchresult/main.go curPerf
	To observe how the performance changes based on a base result, run:
  	go run benchmark/benchresult/main.go basePerf curPerf
*/
package main

import (
	"context"
	"encoding/gob"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"os"
	"reflect"
	"runtime"
	"runtime/pprof"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/benchmark"
	bm "google.golang.org/grpc/benchmark"
	"google.golang.org/grpc/benchmark/flags"
	"google.golang.org/grpc/benchmark/latency"
	"google.golang.org/grpc/benchmark/stats"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/grpclog"
	"google.golang.org/grpc/internal/channelz"
	"google.golang.org/grpc/keepalive"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/test/bufconn"

	testgrpc "google.golang.org/grpc/interop/grpc_testing"
	testpb "google.golang.org/grpc/interop/grpc_testing"
)

var (
	workloads = flags.StringWithAllowedValues("workloads", workloadsAll,
		fmt.Sprintf("Workloads to execute - One of: %v", strings.Join(allWorkloads, ", ")), allWorkloads)
	traceMode = flags.StringWithAllowedValues("trace", toggleModeOff,
		fmt.Sprintf("Trace mode - One of: %v", strings.Join(allToggleModes, ", ")), allToggleModes)
	preloaderMode = flags.StringWithAllowedValues("preloader", toggleModeOff,
		fmt.Sprintf("Preloader mode - One of: %v", strings.Join(allToggleModes, ", ")), allToggleModes)
	channelzOn = flags.StringWithAllowedValues("channelz", toggleModeOff,
		fmt.Sprintf("Channelz mode - One of: %v", strings.Join(allToggleModes, ", ")), allToggleModes)
	compressorMode = flags.StringWithAllowedValues("compression", compModeOff,
		fmt.Sprintf("Compression mode - One of: %v", strings.Join(allCompModes, ", ")), allCompModes)
	networkMode = flags.StringWithAllowedValues("networkMode", networkModeNone,
		"Network mode includes LAN, WAN, Local and Longhaul", allNetworkModes)
	readLatency           = flags.DurationSlice("latency", defaultReadLatency, "Simulated one-way network latency - may be a comma-separated list")
	readKbps              = flags.IntSlice("kbps", defaultReadKbps, "Simulated network throughput (in kbps) - may be a comma-separated list")
	readMTU               = flags.IntSlice("mtu", defaultReadMTU, "Simulated network MTU (Maximum Transmission Unit) - may be a comma-separated list")
	maxConcurrentCalls    = flags.IntSlice("maxConcurrentCalls", defaultMaxConcurrentCalls, "Number of concurrent RPCs during benchmarks")
	readReqSizeBytes      = flags.IntSlice("reqSizeBytes", nil, "Request size in bytes - may be a comma-separated list")
	readRespSizeBytes     = flags.IntSlice("respSizeBytes", nil, "Response size in bytes - may be a comma-separated list")
	reqPayloadCurveFiles  = flags.StringSlice("reqPayloadCurveFiles", nil, "comma-separated list of CSV files describing the shape a random distribution of request payload sizes")
	respPayloadCurveFiles = flags.StringSlice("respPayloadCurveFiles", nil, "comma-separated list of CSV files describing the shape a random distribution of response payload sizes")
	benchTime             = flag.Duration("benchtime", time.Second, "Configures the amount of time to run each benchmark")
	memProfile            = flag.String("memProfile", "", "Enables memory profiling output to the filename provided.")
	memProfileRate        = flag.Int("memProfileRate", 512*1024, "Configures the memory profiling rate. \n"+
		"memProfile should be set before setting profile rate. To include every allocated block in the profile, "+
		"set MemProfileRate to 1. To turn off profiling entirely, set MemProfileRate to 0. 512 * 1024 by default.")
	cpuProfile          = flag.String("cpuProfile", "", "Enables CPU profiling output to the filename provided")
	benchmarkResultFile = flag.String("resultFile", "", "Save the benchmark result into a binary file")
	useBufconn          = flag.Bool("bufconn", false, "Use in-memory connection instead of system network I/O")
	enableKeepalive     = flag.Bool("enable_keepalive", false, "Enable client keepalive. \n"+
		"Keepalive.Time is set to 10s, Keepalive.Timeout is set to 1s, Keepalive.PermitWithoutStream is set to true.")

	logger = grpclog.Component("benchmark")
)

const (
	workloadsUnary         = "unary"
	workloadsStreaming     = "streaming"
	workloadsUnconstrained = "unconstrained"
	workloadsAll           = "all"
	// Compression modes.
	compModeOff  = "off"
	compModeGzip = "gzip"
	compModeNop  = "nop"
	compModeAll  = "all"
	// Toggle modes.
	toggleModeOff  = "off"
	toggleModeOn   = "on"
	toggleModeBoth = "both"
	// Network modes.
	networkModeNone  = "none"
	networkModeLocal = "Local"
	networkModeLAN   = "LAN"
	networkModeWAN   = "WAN"
	networkLongHaul  = "Longhaul"

	numStatsBuckets = 10
	warmupCallCount = 10
	warmuptime      = time.Second
)

var (
	allWorkloads              = []string{workloadsUnary, workloadsStreaming, workloadsUnconstrained, workloadsAll}
	allCompModes              = []string{compModeOff, compModeGzip, compModeNop, compModeAll}
	allToggleModes            = []string{toggleModeOff, toggleModeOn, toggleModeBoth}
	allNetworkModes           = []string{networkModeNone, networkModeLocal, networkModeLAN, networkModeWAN, networkLongHaul}
	defaultReadLatency        = []time.Duration{0, 40 * time.Millisecond} // if non-positive, no delay.
	defaultReadKbps           = []int{0, 10240}                           // if non-positive, infinite
	defaultReadMTU            = []int{0}                                  // if non-positive, infinite
	defaultMaxConcurrentCalls = []int{1, 8, 64, 512}
	defaultReqSizeBytes       = []int{1, 1024, 1024 * 1024}
	defaultRespSizeBytes      = []int{1, 1024, 1024 * 1024}
	networks                  = map[string]latency.Network{
		networkModeLocal: latency.Local,
		networkModeLAN:   latency.LAN,
		networkModeWAN:   latency.WAN,
		networkLongHaul:  latency.Longhaul,
	}
	keepaliveTime    = 10 * time.Second
	keepaliveTimeout = 1 * time.Second
	// This is 0.8*keepaliveTime to prevent connection issues because of server
	// keepalive enforcement.
	keepaliveMinTime = 8 * time.Second
)

// runModes indicates the workloads to run. This is initialized with a call to
// `runModesFromWorkloads`, passing the workloads flag set by the user.
type runModes struct {
	unary, streaming, unconstrained bool
}

// runModesFromWorkloads determines the runModes based on the value of
// workloads flag set by the user.
func runModesFromWorkloads(workload string) runModes {
	r := runModes{}
	switch workload {
	case workloadsUnary:
		r.unary = true
	case workloadsStreaming:
		r.streaming = true
	case workloadsUnconstrained:
		r.unconstrained = true
	case workloadsAll:
		r.unary = true
		r.streaming = true
		r.unconstrained = true
	default:
		log.Fatalf("Unknown workloads setting: %v (want one of: %v)",
			workloads, strings.Join(allWorkloads, ", "))
	}
	return r
}

type startFunc func(mode string, bf stats.Features)
type stopFunc func(count uint64)
type ucStopFunc func(req uint64, resp uint64)
type rpcCallFunc func(pos int)
type rpcSendFunc func(pos int)
type rpcRecvFunc func(pos int)
type rpcCleanupFunc func()

func unaryBenchmark(start startFunc, stop stopFunc, bf stats.Features, s *stats.Stats) {
	caller, cleanup := makeFuncUnary(bf)
	defer cleanup()
	runBenchmark(caller, start, stop, bf, s, workloadsUnary)
}

func streamBenchmark(start startFunc, stop stopFunc, bf stats.Features, s *stats.Stats) {
	caller, cleanup := makeFuncStream(bf)
	defer cleanup()
	runBenchmark(caller, start, stop, bf, s, workloadsStreaming)
}

func unconstrainedStreamBenchmark(start startFunc, stop ucStopFunc, bf stats.Features) {
	var sender rpcSendFunc
	var recver rpcRecvFunc
	var cleanup rpcCleanupFunc
	if bf.EnablePreloader {
		sender, recver, cleanup = makeFuncUnconstrainedStreamPreloaded(bf)
	} else {
		sender, recver, cleanup = makeFuncUnconstrainedStream(bf)
	}
	defer cleanup()

	var req, resp uint64
	go func() {
		// Resets the counters once warmed up
		<-time.NewTimer(warmuptime).C
		atomic.StoreUint64(&req, 0)
		atomic.StoreUint64(&resp, 0)
		start(workloadsUnconstrained, bf)
	}()

	bmEnd := time.Now().Add(bf.BenchTime + warmuptime)
	var wg sync.WaitGroup
	wg.Add(2 * bf.MaxConcurrentCalls)
	for i := 0; i < bf.MaxConcurrentCalls; i++ {
		go func(pos int) {
			defer wg.Done()
			for {
				t := time.Now()
				if t.After(bmEnd) {
					return
				}
				sender(pos)
				atomic.AddUint64(&req, 1)
			}
		}(i)
		go func(pos int) {
			defer wg.Done()
			for {
				t := time.Now()
				if t.After(bmEnd) {
					return
				}
				recver(pos)
				atomic.AddUint64(&resp, 1)
			}
		}(i)
	}
	wg.Wait()
	stop(req, resp)
}

// makeClient returns a gRPC client for the grpc.testing.BenchmarkService
// service. The client is configured using the different options in the passed
// 'bf'. Also returns a cleanup function to close the client and release
// resources.
func makeClient(bf stats.Features) (testgrpc.BenchmarkServiceClient, func()) {
	nw := &latency.Network{Kbps: bf.Kbps, Latency: bf.Latency, MTU: bf.MTU}
	opts := []grpc.DialOption{}
	sopts := []grpc.ServerOption{}
	if bf.ModeCompressor == compModeNop {
		sopts = append(sopts,
			grpc.RPCCompressor(nopCompressor{}),
			grpc.RPCDecompressor(nopDecompressor{}),
		)
		opts = append(opts,
			grpc.WithCompressor(nopCompressor{}),
			grpc.WithDecompressor(nopDecompressor{}),
		)
	}
	if bf.ModeCompressor == compModeGzip {
		sopts = append(sopts,
			grpc.RPCCompressor(grpc.NewGZIPCompressor()),
			grpc.RPCDecompressor(grpc.NewGZIPDecompressor()),
		)
		opts = append(opts,
			grpc.WithCompressor(grpc.NewGZIPCompressor()),
			grpc.WithDecompressor(grpc.NewGZIPDecompressor()),
		)
	}
	if bf.EnableKeepalive {
		sopts = append(sopts,
			grpc.KeepaliveParams(keepalive.ServerParameters{
				Time:    keepaliveTime,
				Timeout: keepaliveTimeout,
			}),
			grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
				MinTime:             keepaliveMinTime,
				PermitWithoutStream: true,
			}),
		)
		opts = append(opts,
			grpc.WithKeepaliveParams(keepalive.ClientParameters{
				Time:                keepaliveTime,
				Timeout:             keepaliveTimeout,
				PermitWithoutStream: true,
			}),
		)
	}
	sopts = append(sopts, grpc.MaxConcurrentStreams(uint32(bf.MaxConcurrentCalls+1)))
	opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))

	var lis net.Listener
	if bf.UseBufConn {
		bcLis := bufconn.Listen(256 * 1024)
		lis = bcLis
		opts = append(opts, grpc.WithContextDialer(func(ctx context.Context, address string) (net.Conn, error) {
			return nw.ContextDialer(func(context.Context, string, string) (net.Conn, error) {
				return bcLis.Dial()
			})(ctx, "", "")
		}))
	} else {
		var err error
		lis, err = net.Listen("tcp", "localhost:0")
		if err != nil {
			logger.Fatalf("Failed to listen: %v", err)
		}
		opts = append(opts, grpc.WithContextDialer(func(ctx context.Context, address string) (net.Conn, error) {
			return nw.ContextDialer((&net.Dialer{}).DialContext)(ctx, "tcp", lis.Addr().String())
		}))
	}
	lis = nw.Listener(lis)
	stopper := bm.StartServer(bm.ServerInfo{Type: "protobuf", Listener: lis}, sopts...)
	conn := bm.NewClientConn("" /* target not used */, opts...)
	return testgrpc.NewBenchmarkServiceClient(conn), func() {
		conn.Close()
		stopper()
	}
}

func makeFuncUnary(bf stats.Features) (rpcCallFunc, rpcCleanupFunc) {
	tc, cleanup := makeClient(bf)
	return func(int) {
		reqSizeBytes := bf.ReqSizeBytes
		respSizeBytes := bf.RespSizeBytes
		if bf.ReqPayloadCurve != nil {
			reqSizeBytes = bf.ReqPayloadCurve.ChooseRandom()
		}
		if bf.RespPayloadCurve != nil {
			respSizeBytes = bf.RespPayloadCurve.ChooseRandom()
		}
		unaryCaller(tc, reqSizeBytes, respSizeBytes)
	}, cleanup
}

func makeFuncStream(bf stats.Features) (rpcCallFunc, rpcCleanupFunc) {
	tc, cleanup := makeClient(bf)

	streams := make([]testgrpc.BenchmarkService_StreamingCallClient, bf.MaxConcurrentCalls)
	for i := 0; i < bf.MaxConcurrentCalls; i++ {
		stream, err := tc.StreamingCall(context.Background())
		if err != nil {
			logger.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
		}
		streams[i] = stream
	}

	return func(pos int) {
		reqSizeBytes := bf.ReqSizeBytes
		respSizeBytes := bf.RespSizeBytes
		if bf.ReqPayloadCurve != nil {
			reqSizeBytes = bf.ReqPayloadCurve.ChooseRandom()
		}
		if bf.RespPayloadCurve != nil {
			respSizeBytes = bf.RespPayloadCurve.ChooseRandom()
		}
		streamCaller(streams[pos], reqSizeBytes, respSizeBytes)
	}, cleanup
}

func makeFuncUnconstrainedStreamPreloaded(bf stats.Features) (rpcSendFunc, rpcRecvFunc, rpcCleanupFunc) {
	streams, req, cleanup := setupUnconstrainedStream(bf)

	preparedMsg := make([]*grpc.PreparedMsg, len(streams))
	for i, stream := range streams {
		preparedMsg[i] = &grpc.PreparedMsg{}
		err := preparedMsg[i].Encode(stream, req)
		if err != nil {
			logger.Fatalf("%v.Encode(%v, %v) = %v", preparedMsg[i], req, stream, err)
		}
	}

	return func(pos int) {
			streams[pos].SendMsg(preparedMsg[pos])
		}, func(pos int) {
			streams[pos].Recv()
		}, cleanup
}

func makeFuncUnconstrainedStream(bf stats.Features) (rpcSendFunc, rpcRecvFunc, rpcCleanupFunc) {
	streams, req, cleanup := setupUnconstrainedStream(bf)

	return func(pos int) {
			streams[pos].Send(req)
		}, func(pos int) {
			streams[pos].Recv()
		}, cleanup
}

func setupUnconstrainedStream(bf stats.Features) ([]testgrpc.BenchmarkService_StreamingCallClient, *testpb.SimpleRequest, rpcCleanupFunc) {
	tc, cleanup := makeClient(bf)

	streams := make([]testgrpc.BenchmarkService_StreamingCallClient, bf.MaxConcurrentCalls)
	md := metadata.Pairs(benchmark.UnconstrainedStreamingHeader, "1")
	ctx := metadata.NewOutgoingContext(context.Background(), md)
	for i := 0; i < bf.MaxConcurrentCalls; i++ {
		stream, err := tc.StreamingCall(ctx)
		if err != nil {
			logger.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
		}
		streams[i] = stream
	}

	pl := bm.NewPayload(testpb.PayloadType_COMPRESSABLE, bf.ReqSizeBytes)
	req := &testpb.SimpleRequest{
		ResponseType: pl.Type,
		ResponseSize: int32(bf.RespSizeBytes),
		Payload:      pl,
	}

	return streams, req, cleanup
}

// Makes a UnaryCall gRPC request using the given BenchmarkServiceClient and
// request and response sizes.
func unaryCaller(client testgrpc.BenchmarkServiceClient, reqSize, respSize int) {
	if err := bm.DoUnaryCall(client, reqSize, respSize); err != nil {
		logger.Fatalf("DoUnaryCall failed: %v", err)
	}
}

func streamCaller(stream testgrpc.BenchmarkService_StreamingCallClient, reqSize, respSize int) {
	if err := bm.DoStreamingRoundTrip(stream, reqSize, respSize); err != nil {
		logger.Fatalf("DoStreamingRoundTrip failed: %v", err)
	}
}

func runBenchmark(caller rpcCallFunc, start startFunc, stop stopFunc, bf stats.Features, s *stats.Stats, mode string) {
	// Warm up connection.
	for i := 0; i < warmupCallCount; i++ {
		caller(0)
	}

	// Run benchmark.
	start(mode, bf)
	var wg sync.WaitGroup
	wg.Add(bf.MaxConcurrentCalls)
	bmEnd := time.Now().Add(bf.BenchTime)
	var count uint64
	for i := 0; i < bf.MaxConcurrentCalls; i++ {
		go func(pos int) {
			defer wg.Done()
			for {
				t := time.Now()
				if t.After(bmEnd) {
					return
				}
				start := time.Now()
				caller(pos)
				elapse := time.Since(start)
				atomic.AddUint64(&count, 1)
				s.AddDuration(elapse)
			}
		}(i)
	}
	wg.Wait()
	stop(count)
}

// benchOpts represents all configurable options available while running this
// benchmark. This is built from the values passed as flags.
type benchOpts struct {
	rModes              runModes
	benchTime           time.Duration
	memProfileRate      int
	memProfile          string
	cpuProfile          string
	networkMode         string
	benchmarkResultFile string
	useBufconn          bool
	enableKeepalive     bool
	features            *featureOpts
}

// featureOpts represents options which can have multiple values. The user
// usually provides a comma-separated list of options for each of these
// features through command line flags. We generate all possible combinations
// for the provided values and run the benchmarks for each combination.
type featureOpts struct {
	enableTrace        []bool
	readLatencies      []time.Duration
	readKbps           []int
	readMTU            []int
	maxConcurrentCalls []int
	reqSizeBytes       []int
	respSizeBytes      []int
	reqPayloadCurves   []*stats.PayloadCurve
	respPayloadCurves  []*stats.PayloadCurve
	compModes          []string
	enableChannelz     []bool
	enablePreloader    []bool
}

// makeFeaturesNum returns a slice of ints of size 'maxFeatureIndex' where each
// element of the slice (indexed by 'featuresIndex' enum) contains the number
// of features to be exercised by the benchmark code.
// For example: Index 0 of the returned slice contains the number of values for
// enableTrace feature, while index 1 contains the number of value of
// readLatencies feature and so on.
func makeFeaturesNum(b *benchOpts) []int {
	featuresNum := make([]int, stats.MaxFeatureIndex)
	for i := 0; i < len(featuresNum); i++ {
		switch stats.FeatureIndex(i) {
		case stats.EnableTraceIndex:
			featuresNum[i] = len(b.features.enableTrace)
		case stats.ReadLatenciesIndex:
			featuresNum[i] = len(b.features.readLatencies)
		case stats.ReadKbpsIndex:
			featuresNum[i] = len(b.features.readKbps)
		case stats.ReadMTUIndex:
			featuresNum[i] = len(b.features.readMTU)
		case stats.MaxConcurrentCallsIndex:
			featuresNum[i] = len(b.features.maxConcurrentCalls)
		case stats.ReqSizeBytesIndex:
			featuresNum[i] = len(b.features.reqSizeBytes)
		case stats.RespSizeBytesIndex:
			featuresNum[i] = len(b.features.respSizeBytes)
		case stats.ReqPayloadCurveIndex:
			featuresNum[i] = len(b.features.reqPayloadCurves)
		case stats.RespPayloadCurveIndex:
			featuresNum[i] = len(b.features.respPayloadCurves)
		case stats.CompModesIndex:
			featuresNum[i] = len(b.features.compModes)
		case stats.EnableChannelzIndex:
			featuresNum[i] = len(b.features.enableChannelz)
		case stats.EnablePreloaderIndex:
			featuresNum[i] = len(b.features.enablePreloader)
		default:
			log.Fatalf("Unknown feature index %v in generateFeatures. maxFeatureIndex is %v", i, stats.MaxFeatureIndex)
		}
	}
	return featuresNum
}

// sharedFeatures returns a bool slice which acts as a bitmask. Each item in
// the slice represents a feature, indexed by 'featureIndex' enum.  The bit is
// set to 1 if the corresponding feature does not have multiple value, so is
// shared amongst all benchmarks.
func sharedFeatures(featuresNum []int) []bool {
	result := make([]bool, len(featuresNum))
	for i, num := range featuresNum {
		if num <= 1 {
			result[i] = true
		}
	}
	return result
}

// generateFeatures generates all combinations of the provided feature options.
// While all the feature options are stored in the benchOpts struct, the input
// parameter 'featuresNum' is a slice indexed by 'featureIndex' enum containing
// the number of values for each feature.
// For example, let's say the user sets -workloads=all and
// -maxConcurrentCalls=1,100, this would end up with the following
// combinations:
// [workloads: unary, maxConcurrentCalls=1]
// [workloads: unary, maxConcurrentCalls=1]
// [workloads: streaming, maxConcurrentCalls=100]
// [workloads: streaming, maxConcurrentCalls=100]
// [workloads: unconstrained, maxConcurrentCalls=1]
// [workloads: unconstrained, maxConcurrentCalls=100]
func (b *benchOpts) generateFeatures(featuresNum []int) []stats.Features {
	// curPos and initialPos are two slices where each value acts as an index
	// into the appropriate feature slice maintained in benchOpts.features. This
	// loop generates all possible combinations of features by changing one value
	// at a time, and once curPos becomes equal to initialPos, we have explored
	// all options.
	var result []stats.Features
	var curPos []int
	initialPos := make([]int, stats.MaxFeatureIndex)
	for !reflect.DeepEqual(initialPos, curPos) {
		if curPos == nil {
			curPos = make([]int, stats.MaxFeatureIndex)
		}
		f := stats.Features{
			// These features stay the same for each iteration.
			NetworkMode:     b.networkMode,
			UseBufConn:      b.useBufconn,
			EnableKeepalive: b.enableKeepalive,
			BenchTime:       b.benchTime,
			// These features can potentially change for each iteration.
			EnableTrace:        b.features.enableTrace[curPos[stats.EnableTraceIndex]],
			Latency:            b.features.readLatencies[curPos[stats.ReadLatenciesIndex]],
			Kbps:               b.features.readKbps[curPos[stats.ReadKbpsIndex]],
			MTU:                b.features.readMTU[curPos[stats.ReadMTUIndex]],
			MaxConcurrentCalls: b.features.maxConcurrentCalls[curPos[stats.MaxConcurrentCallsIndex]],
			ModeCompressor:     b.features.compModes[curPos[stats.CompModesIndex]],
			EnableChannelz:     b.features.enableChannelz[curPos[stats.EnableChannelzIndex]],
			EnablePreloader:    b.features.enablePreloader[curPos[stats.EnablePreloaderIndex]],
		}
		if len(b.features.reqPayloadCurves) == 0 {
			f.ReqSizeBytes = b.features.reqSizeBytes[curPos[stats.ReqSizeBytesIndex]]
		} else {
			f.ReqPayloadCurve = b.features.reqPayloadCurves[curPos[stats.ReqPayloadCurveIndex]]
		}
		if len(b.features.respPayloadCurves) == 0 {
			f.RespSizeBytes = b.features.respSizeBytes[curPos[stats.RespSizeBytesIndex]]
		} else {
			f.RespPayloadCurve = b.features.respPayloadCurves[curPos[stats.RespPayloadCurveIndex]]
		}
		result = append(result, f)
		addOne(curPos, featuresNum)
	}
	return result
}

// addOne mutates the input slice 'features' by changing one feature, thus
// arriving at the next combination of feature values. 'featuresMaxPosition'
// provides the numbers of allowed values for each feature, indexed by
// 'featureIndex' enum.
func addOne(features []int, featuresMaxPosition []int) {
	for i := len(features) - 1; i >= 0; i-- {
		if featuresMaxPosition[i] == 0 {
			continue
		}
		features[i] = (features[i] + 1)
		if features[i]/featuresMaxPosition[i] == 0 {
			break
		}
		features[i] = features[i] % featuresMaxPosition[i]
	}
}

// processFlags reads the command line flags and builds benchOpts. Specifying
// invalid values for certain flags will cause flag.Parse() to fail, and the
// program to terminate.
// This *SHOULD* be the only place where the flags are accessed. All other
// parts of the benchmark code should rely on the returned benchOpts.
func processFlags() *benchOpts {
	flag.Parse()
	if flag.NArg() != 0 {
		log.Fatal("Error: unparsed arguments: ", flag.Args())
	}

	opts := &benchOpts{
		rModes:              runModesFromWorkloads(*workloads),
		benchTime:           *benchTime,
		memProfileRate:      *memProfileRate,
		memProfile:          *memProfile,
		cpuProfile:          *cpuProfile,
		networkMode:         *networkMode,
		benchmarkResultFile: *benchmarkResultFile,
		useBufconn:          *useBufconn,
		enableKeepalive:     *enableKeepalive,
		features: &featureOpts{
			enableTrace:        setToggleMode(*traceMode),
			readLatencies:      append([]time.Duration(nil), *readLatency...),
			readKbps:           append([]int(nil), *readKbps...),
			readMTU:            append([]int(nil), *readMTU...),
			maxConcurrentCalls: append([]int(nil), *maxConcurrentCalls...),
			reqSizeBytes:       append([]int(nil), *readReqSizeBytes...),
			respSizeBytes:      append([]int(nil), *readRespSizeBytes...),
			compModes:          setCompressorMode(*compressorMode),
			enableChannelz:     setToggleMode(*channelzOn),
			enablePreloader:    setToggleMode(*preloaderMode),
		},
	}

	if len(*reqPayloadCurveFiles) == 0 {
		if len(opts.features.reqSizeBytes) == 0 {
			opts.features.reqSizeBytes = defaultReqSizeBytes
		}
	} else {
		if len(opts.features.reqSizeBytes) != 0 {
			log.Fatalf("you may not specify -reqPayloadCurveFiles and -reqSizeBytes at the same time")
		}
		for _, file := range *reqPayloadCurveFiles {
			pc, err := stats.NewPayloadCurve(file)
			if err != nil {
				log.Fatalf("cannot load payload curve file %s: %v", file, err)
			}
			opts.features.reqPayloadCurves = append(opts.features.reqPayloadCurves, pc)
		}
		opts.features.reqSizeBytes = nil
	}
	if len(*respPayloadCurveFiles) == 0 {
		if len(opts.features.respSizeBytes) == 0 {
			opts.features.respSizeBytes = defaultRespSizeBytes
		}
	} else {
		if len(opts.features.respSizeBytes) != 0 {
			log.Fatalf("you may not specify -respPayloadCurveFiles and -respSizeBytes at the same time")
		}
		for _, file := range *respPayloadCurveFiles {
			pc, err := stats.NewPayloadCurve(file)
			if err != nil {
				log.Fatalf("cannot load payload curve file %s: %v", file, err)
			}
			opts.features.respPayloadCurves = append(opts.features.respPayloadCurves, pc)
		}
		opts.features.respSizeBytes = nil
	}

	// Re-write latency, kpbs and mtu if network mode is set.
	if network, ok := networks[opts.networkMode]; ok {
		opts.features.readLatencies = []time.Duration{network.Latency}
		opts.features.readKbps = []int{network.Kbps}
		opts.features.readMTU = []int{network.MTU}
	}
	return opts
}

func setToggleMode(val string) []bool {
	switch val {
	case toggleModeOn:
		return []bool{true}
	case toggleModeOff:
		return []bool{false}
	case toggleModeBoth:
		return []bool{false, true}
	default:
		// This should never happen because a wrong value passed to this flag would
		// be caught during flag.Parse().
		return []bool{}
	}
}

func setCompressorMode(val string) []string {
	switch val {
	case compModeNop, compModeGzip, compModeOff:
		return []string{val}
	case compModeAll:
		return []string{compModeNop, compModeGzip, compModeOff}
	default:
		// This should never happen because a wrong value passed to this flag would
		// be caught during flag.Parse().
		return []string{}
	}
}

func main() {
	opts := processFlags()
	before(opts)

	s := stats.NewStats(numStatsBuckets)
	featuresNum := makeFeaturesNum(opts)
	sf := sharedFeatures(featuresNum)

	var (
		start  = func(mode string, bf stats.Features) { s.StartRun(mode, bf, sf) }
		stop   = func(count uint64) { s.EndRun(count) }
		ucStop = func(req uint64, resp uint64) { s.EndUnconstrainedRun(req, resp) }
	)

	for _, bf := range opts.generateFeatures(featuresNum) {
		grpc.EnableTracing = bf.EnableTrace
		if bf.EnableChannelz {
			channelz.TurnOn()
		}
		if opts.rModes.unary {
			unaryBenchmark(start, stop, bf, s)
		}
		if opts.rModes.streaming {
			streamBenchmark(start, stop, bf, s)
		}
		if opts.rModes.unconstrained {
			unconstrainedStreamBenchmark(start, ucStop, bf)
		}
	}
	after(opts, s.GetResults())
}

func before(opts *benchOpts) {
	if opts.memProfile != "" {
		runtime.MemProfileRate = opts.memProfileRate
	}
	if opts.cpuProfile != "" {
		f, err := os.Create(opts.cpuProfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
			return
		}
		if err := pprof.StartCPUProfile(f); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't start cpu profile: %s\n", err)
			f.Close()
			return
		}
	}
}

func after(opts *benchOpts, data []stats.BenchResults) {
	if opts.cpuProfile != "" {
		pprof.StopCPUProfile() // flushes profile to disk
	}
	if opts.memProfile != "" {
		f, err := os.Create(opts.memProfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
			os.Exit(2)
		}
		runtime.GC() // materialize all statistics
		if err = pprof.WriteHeapProfile(f); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't write heap profile %s: %s\n", opts.memProfile, err)
			os.Exit(2)
		}
		f.Close()
	}
	if opts.benchmarkResultFile != "" {
		f, err := os.Create(opts.benchmarkResultFile)
		if err != nil {
			log.Fatalf("testing: can't write benchmark result %s: %s\n", opts.benchmarkResultFile, err)
		}
		dataEncoder := gob.NewEncoder(f)
		dataEncoder.Encode(data)
		f.Close()
	}
}

// nopCompressor is a compressor that just copies data.
type nopCompressor struct{}

func (nopCompressor) Do(w io.Writer, p []byte) error {
	n, err := w.Write(p)
	if err != nil {
		return err
	}
	if n != len(p) {
		return fmt.Errorf("nopCompressor.Write: wrote %v bytes; want %v", n, len(p))
	}
	return nil
}

func (nopCompressor) Type() string { return compModeNop }

// nopDecompressor is a decompressor that just copies data.
type nopDecompressor struct{}

func (nopDecompressor) Do(r io.Reader) ([]byte, error) { return ioutil.ReadAll(r) }
func (nopDecompressor) Type() string                   { return compModeNop }


text:
{-# OPTIONS_GHC -funbox-strict-fields -Wall -Werror #-}

-- | A data structure for equivalence classes.  This provides an
-- interface whereby equivalence relations can be added to a
-- collection, then all the equivalence classes implied by the
-- relationships that have been added can be extracted.
--
-- This is the same as the "Data.Equiv" module, except that this one
-- is based on 'HashMap's.
module Data.Equivs.Hashable(
       Equivs,
       addSingle,
       addEquiv,
       addEquivs,
       toEquivs,
       ) where

import Data.Hashable
import Data.HashMap.Strict(HashMap)
import Data.HashSet(HashSet)
import Data.Monoid

import qualified Data.HashMap.Strict as HashMap
import qualified Data.HashSet as HashSet

-- | A set of equivalence relations.
newtype Equivs idty infoty =
  Equivs { equivMap :: HashMap idty (HashSet idty, infoty) }

-- | Add a single item with information.  If it already exists in an
-- equivalence class, then the information will be added to that
-- class.  Otherwise, a single-element equivalence class will be
-- created.
addSingle :: (Eq idty, Hashable idty, Monoid infoty) =>
             idty
          -- ^ The item to add.
          -> infoty
          -- ^ The extra information.
          -> Equivs idty infoty
          -- ^ The equivalence structure to which to add the equivalence.
          -> Equivs idty infoty
          -- ^ The equivalence structure with the new equivalence added.
addSingle a info Equivs { equivMap = equivs } =
  let
    -- Make a new set with the two elements' equivalence classes unioned
    newent @ (set, _) = case HashMap.lookup a equivs of
      Just (oldset, oldinfo) -> (oldset, info <> oldinfo)
      Nothing -> (HashSet.singleton a, info)

    foldfun e = HashMap.insert e newent

    -- Update the equivalence class mapping with the new equivalence class
    newmap = HashSet.foldr foldfun equivs set
  in
    Equivs { equivMap = newmap }

-- | Add an equivalence relationship to an 'Equivs'.
addEquiv :: (Eq idty, Hashable idty, Monoid infoty) =>
            idty
         -- ^ The first equivalent item.
         -> idty
         -- ^ The second equivalent item.
         -> infoty
         -- ^ The extra information.
         -> Equivs idty infoty
         -- ^ The equivalence structure to which to add the equivalence.
         -> Equivs idty infoty
         -- ^ The equivalence structure with the new equivalence added.
addEquiv a b info Equivs { equivMap = equivs } =
  let
    -- Make a new set with the two elements' equivalence classes unioned
    newent @ (set, _) = case (HashMap.lookup a equivs,
                              HashMap.lookup b equivs) of
      (Nothing, Nothing) -> (HashSet.fromList [a, b], info)
      (Nothing, Just (oldset, oldinfo)) ->
        (HashSet.insert a oldset, info <> oldinfo)
      (Just (oldset, oldinfo), Nothing) ->
        (HashSet.insert b oldset, info <> oldinfo)
      (Just (aset, ainfo), Just (bset, binfo)) ->
        (aset <> bset, info <> ainfo <> binfo)

    foldfun e = HashMap.insert e newent

    -- Update the equivalence class mapping with the new equivalence class
    newmap = HashSet.foldr foldfun equivs set
  in
    Equivs { equivMap = newmap }

-- | Add a set of equivalences to an 'Equivs'.
addEquivs :: (Eq idty, Hashable idty, Monoid infoty) =>
             [idty]
          -- ^ The equivalences to add.
          -> infoty
          -- ^ The extra information.
          -> Equivs idty infoty
          -- ^ The equivalence structure to which to add the equivalences.
          -> Equivs idty infoty
          --  ^ The equivalence structure with the equivalences added.
addEquivs l info Equivs { equivMap = equivs } =
  let
    getset a = HashMap.lookupDefault (mempty, mempty) a equivs

    -- Lookup and union all the equivalence sets
    (set, newinfo) = mconcat (map getset l)

    newent = (set, info <> newinfo)

    foldfun a = HashMap.insert a newent

    -- Update the equivalence class mapping with the new equivalence class
    newmap = HashSet.foldr foldfun equivs set
  in
    Equivs { equivMap = newmap }

-- | Turn an 'Equivs' into a list of equivalence classes.
toEquivs :: (Eq idty, Hashable idty) =>
            Equivs idty infoty
         -- ^ The 'Equivs' to decompose.
         -> [([idty], infoty)]
         -- ^ A list of all equivalence classes.
toEquivs Equivs { equivMap = equivs } =
  let
    pairs = HashMap.toList equivs

    foldfun (key, (set, info)) accum @ (skipset, equivsets)
      | HashSet.member key skipset = accum
      | otherwise = (skipset <> set, (HashSet.toList set, info) : equivsets)

    (_, out) = foldr foldfun (mempty, []) pairs
  in
    out

instance (Eq idty, Hashable idty, Monoid infoty) =>
         Monoid (Equivs idty infoty) where
  mempty = Equivs { equivMap = HashMap.empty }

  mappend Equivs { equivMap = map1 } Equivs { equivMap = map2 } =
    Equivs { equivMap = HashMap.unionWith mappend map1 map2 }

redpajama_set_name: RedPajamaGithub


text:
package server

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"

	"github.com/steenzout/go-playground/unix/common"
)

// UnixDomainSocketHTTPServer implements a HTTP server over a unix domain socket connection.
type UnixDomainSocketHTTPServer struct {
	ln net.Listener
}

// ServeHTTP handle HTTP requests.
func (h *UnixDomainSocketHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var err error
	var contents []byte

	contents, err = ioutil.ReadAll(r.Body)
	if err != nil {
		log.Println("server: ERROR ", err)
		return
	}
	log.Println("server: contents=", bytes.Index(contents, []byte{0}))

	log.Println("server: r.URL.Scheme=", r.URL.Scheme)
	log.Println("server: r.URL.Host=", r.URL.Host)
	log.Println("server: r.URL.RawQuery=", r.URL.RawQuery)
	log.Println("server: r.URL.Path=", r.URL.Path)

	w.Write([]byte(fmt.Sprintf("r.URL.Path=", r.URL.Path, "\r\n")))
	w.Write([]byte(fmt.Sprintf("r.URL.Query=", r.URL.Query(), "\r\n")))
}

// Close closes the unix domain socket connection.
func (h *UnixDomainSocketHTTPServer) Close() {
	h.ln.Close()
}

// Serve handle HTTP requests.
func (h *UnixDomainSocketHTTPServer) Serve() {
	var err error

	log.Println("server: starting")
	if err = http.Serve(h.ln, h); err != nil {
		h.ln.Close()
	}
}

// NewUnixDomainSocketServer creates a new HTTP server over unix domain socket.
func NewUnixDomainSocketServer(path string) *UnixDomainSocketHTTPServer {
	var ln net.Listener
	var err error
	var socket string

	if path == "" {
		socket = common.DomainSocket
	} else {
		socket = path
	}

	if ln, err = net.Listen("unix", socket); err != nil {
		panic(err)
	}
	return &UnixDomainSocketHTTPServer{ln}
}


text:
using System.ComponentModel.DataAnnotations;

namespace GRA.Domain.Model
{
    public class SchoolType : Abstract.BaseDomainEntity
    {
        public int SiteId { get; set; }
        [Required]
        public string Name { get; set; }
    }
}


text:
package com.example.fw;

import java.util.List;

import com.example.utils.SortedListOf;
import com.example.tests.ContactData;
import  com.example.tests.GroupData;

public class ApplicationModel {

	private SortedListOf<GroupData> groups;
	private List<GroupData> listGroups;
	private SortedListOf<ContactData> contacts;
	private List<ContactData> listContacts;

	//==========================================================================

	public SortedListOf<GroupData> getGroups() {
		return groups;
	}

	public void setGroups(List<GroupData> groups){
		this.groups = new SortedListOf<GroupData>(groups);
	}

	public ApplicationModel addGroup(GroupData group) {
		groups.add(group);
		return this;
	}

	public ApplicationModel remouveGroup(int index) {
		groups.remove(index);
		return this;
	}
	//==========================================================================

		public SortedListOf<ContactData> getContacts() {
			return contacts;
		}

		public void setContacts(List<ContactData> contacts){
			this.contacts = new SortedListOf<ContactData>(contacts);
		}

		public ApplicationModel addContact(ContactData contact) {
			contacts.add(contact);
			return this;
		}

		public ApplicationModel remouveContact(int index) {
			contacts.remove(index);
			return this;
		}
}


text:
<?php

namespace hiqdev\thememanager\widgets;

use yii\base\Widget;

/**
 * PoweredBy widget.
 * @author Andrii Vasyliev <sol@hiqdev.com>
 */
class PoweredBy extends Widget
{
    /**
     * @var string|array url to empowerer
     */
    public $url;

    /**
     * @var string name to display
     */
    public $name;

    /**
     * @var string version to display if given
     */
    public $version;

    /**
     * @var array html options
     */
    public $options = [];

    public function run()
    {
        return $this->render('PoweredBy', [
            'url'     => $this->url,
            'name'    => $this->name,
            'version' => $this->version,
            'options' => $this->options,
        ]);
    }
}


text:
module ListHelper
  # output comma-separated list of cities for a group of venues
  def comma_separated_list(array)
    array.join(", ")
  end
end


text:
using Spring.Threading.Execution;

namespace Spring.Threading.Future
{
    /// <summary>
    /// A delayed result-bearing action that can be cancelled.
    /// </summary>
    /// <remarks>
    /// Usually a scheduled future is the result of scheduling
    /// a task with a <see cref="IScheduledExecutorService"/>.
    /// </remarks>
    /// <author>Doug Lea</author>
    /// <author>Griffin Caprio(.NET)</author>
    public interface IScheduledFuture<T> : IDelayed, IFuture<T>
    {
    }
}


text:
"""This script translates JSON test vectors to BoringSSL's "FileTest" format.

Usage: translate_test_vectors.py TEST_VECTORS_JSON_FILE

The TEST_VECTORS_JSON_FILE is expected to come from the JSON copy of
draft-irtf-cfrg-hpke-12's test vectors, linked from its [TestVectors] citation.
The output is written to "hpke_test_vectors.txt".
"""

import collections
import json
import sys

HPKE_MODE_BASE = 0
HPKE_MODE_PSK = 1
HPKE_DHKEM_X25519_SHA256 = 0x0020
HPKE_HKDF_SHA256 = 0x0001
HPKE_AEAD_EXPORT_ONLY = 0xffff

def read_test_vectors_and_generate_code(json_file_in_path, test_file_out_path):
  """Translates JSON test vectors into BoringSSL's FileTest language.

    Args:
      json_file_in_path: Path to the JSON test vectors file.
      test_file_out_path: Path to output file.
  """

  # Load the JSON file into |test_vecs|.
  with open(json_file_in_path) as file_in:
    test_vecs = json.load(file_in)

  lines = []
  for test in test_vecs:
    # Filter out test cases that we don't use.
    if (test["mode"] != HPKE_MODE_BASE or
        test["kem_id"] != HPKE_DHKEM_X25519_SHA256 or
        test["aead_id"] == HPKE_AEAD_EXPORT_ONLY or
        test["kdf_id"] != HPKE_HKDF_SHA256):
      continue

    keys = ["mode", "kdf_id", "aead_id", "info", "skRm", "skEm", "pkRm", "pkEm"]

    if test["mode"] == HPKE_MODE_PSK:
      keys.append("psk")
      keys.append("psk_id")

    for key in keys:
      lines.append("{} = {}".format(key, str(test[key])))

    for i, enc in enumerate(test["encryptions"]):
      lines.append("# encryptions[{}]".format(i))
      for key in ("aad", "ct", "pt"):
        lines.append("{} = {}".format(key, str(enc[key])))

    for i, exp in enumerate(test["exports"]):
      lines.append("# exports[{}]".format(i))
      for key in ("exporter_context", "L", "exported_value"):
        lines.append("{} = {}".format(key, str(exp[key])))

    lines.append("")

  with open(test_file_out_path, "w") as file_out:
    file_out.write("\n".join(lines))

def main(argv):
  if len(argv) != 2:
    print(__doc__)
    sys.exit(1)

  read_test_vectors_and_generate_code(argv[1], "hpke_test_vectors.txt")

if __name__ == "__main__":
  main(sys.argv)


text:
package com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation;

import com.microsoft.azure.Page;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.resources.fluentcore.utils.PagedListConverter;
import com.microsoft.azure.management.resources.implementation.PageImpl;

import java.util.List;

/**
 * Base class for readable wrapper collections, i.e. those whose models can only be read, not created.
 * (Internal use only)
 * @param <T> the individual resource type returned
 * @param <ImplT> the individual resource implementation
 * @param <InnerT> the wrapper inner type
 */
public abstract class ReadableWrappersImpl<
        T,
        ImplT extends T,
        InnerT> {

    private final PagedListConverter<InnerT, T> converter;

    protected ReadableWrappersImpl() {
        this.converter = new PagedListConverter<InnerT, T>() {
            @Override
            public T typeConvert(InnerT inner) {
                return wrapModel(inner);
            }
        };
    }

    protected abstract ImplT wrapModel(InnerT inner);

    protected PagedList<T> wrapList(PagedList<InnerT> pagedList) {
        return converter.convert(pagedList);
    }

    protected PagedList<T> wrapList(List<InnerT> list) {
        return converter.convert(ReadableWrappersImpl.convertToPagedList(list));
    }

    /**
     * Converts the List to PagedList.
     * @param list list to be converted in to paged list
     * @param <InnerT> the wrapper inner type
     * @return the Paged list for the inner type.
     */
    public static <InnerT> PagedList<InnerT> convertToPagedList(List<InnerT> list) {
        PageImpl<InnerT> page = new PageImpl<>();
        page.setItems(list);
        page.setNextPageLink(null);
        return new PagedList<InnerT>(page) {
            @Override
            public Page<InnerT> nextPage(String nextPageLink) {
                return null;
            }
        };
    }

}


text:
<?php
/**
 * Slider Video
 *
 */

namespace angelrove\front_components\Slider\Video;

use angelrove\front_components\Slider\Slider;
use angelrove\utils\CssJsLoad;
use angelrove\utils\Vendor;

class Video extends Slider
{
    //------------------------------------------
    public function __construct(array $images, $images_subdir)
    {
        parent::__construct($images, $images_subdir);
        $this->id_slider = 'Slider_Video';

        //--------
        // Vendor::usef('video-js');
        CssJsLoad::set(__DIR__ . '/styles.css');
    }
    //-------------------------------------------------
    public function set_height($value)
    {
        if (!$value) {
            return;
        }

        CssJsLoad::set_css_block('
            .slider-video .height_video {
                max-height: '.$value.';
            }
        ');
    }
    //-------------------------------------------------
    public function set_opacity($value)
    {
        if (!$value) {
            return;
        }

        CssJsLoad::set_css_block('
           .slider-video .moving-background video {
              opacity: '.$value.';
           }
        ');
    }
    //------------------------------------------
    /*
     *  $images->: file_img, name, descripcion
     */
    public function show()
    {
        if (!$this->images) {
            return false;
        }

        // CssJsLoad::set_script("
        // ");

        include 'tmpl.inc';
    }
    //------------------------------------------
}


text:
<!--

********************************************************************************

WARNING:

    DO NOT EDIT "amazonlinux/README.md"

    IT IS AUTO-GENERATED

    (from the other files in "amazonlinux/" combined with a set of templates)

********************************************************************************

-->

# Supported tags and respective `Dockerfile` links

-	[`2017.12.0.20180330`, `2017.12`, `2` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/91cc3997375a8055ae62ffa2635acc464ec8ac51/Dockerfile)
-	[`2017.12.0.20180330-with-sources`, `2017.12-with-sources`, `2-with-sources` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/9129377feea1176b636427b295e8af57656db079/Dockerfile)
-	[`2018.03.0.20180424`, `2018.03`, `1`, `latest` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/2d98947f57ed4afe97daef60f4c05ec5e4adc69d/Dockerfile)
-	[`2018.03.0.20180424-with-sources`, `2018.03-with-sources`, `1-with-sources`, `with-sources` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/e323bc6480b6eb08de5ed48f5d049aa04dcef7d3/Dockerfile)
-	[`2017.09.1.20180409`, `2017.09` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/0b2dad813345cab464c6c0a716aa5be2ae072f79/Dockerfile)
-	[`2017.09.1.20180409-with-sources`, `2017.09-with-sources` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/6d5273cf53b85c27690ac394bee2c2935cca73b8/Dockerfile)
-	[`2017.03.1.20170812`, `2017.03` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/577139a6f2571e3adb59cfd34d61bc07e2fba238/Dockerfile)
-	[`2017.03.1.20170812-with-sources`, `2017.03-with-sources` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/47f91aec4a189232a7feb1ec544a3a6af0347113/Dockerfile)
-	[`2016.09.1.20161221`, `2016.09` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/e1b56e68ebd2b274c64e0a0a18ae0a9a8122822d/Dockerfile)
-	[`2016.09.1.20161221-with-sources`, `2016.09-with-sources` (*Dockerfile*)](https://github.com/aws/amazon-linux-docker-images/blob/2de60e8c98421694c293639659a88ed81ce29298/Dockerfile)

# Quick reference

-	**Where to get help**:
	[the Docker Community Forums](https://forums.docker.com/), [the Docker Community Slack](https://blog.docker.com/2016/11/introducing-docker-community-directory-docker-community-slack/), or [Stack Overflow](https://stackoverflow.com/search?tab=newest&q=docker)

-	**Where to file issues**:
	[the Amazon Linux forums](https://forums.aws.amazon.com/forum.jspa?forumID=228)

-	**Maintained by**:
	[the Amazon Linux Team](https://github.com/aws/amazon-linux-docker-images)

-	**Supported architectures**: ([more info](https://github.com/docker-library/official-images#architectures-other-than-amd64))
	[`amd64`](https://hub.docker.com/r/amd64/amazonlinux/)

-	**Published image artifact details**:
	[repo-info repo's `repos/amazonlinux/` directory](https://github.com/docker-library/repo-info/blob/master/repos/amazonlinux) ([history](https://github.com/docker-library/repo-info/commits/master/repos/amazonlinux))
	(image metadata, transfer size, etc)

-	**Image updates**:
	[official-images PRs with label `library/amazonlinux`](https://github.com/docker-library/official-images/pulls?q=label%3Alibrary%2Famazonlinux)
	[official-images repo's `library/amazonlinux` file](https://github.com/docker-library/official-images/blob/master/library/amazonlinux) ([history](https://github.com/docker-library/official-images/commits/master/library/amazonlinux))

-	**Source of this description**:
	[docs repo's `amazonlinux/` directory](https://github.com/docker-library/docs/tree/master/amazonlinux) ([history](https://github.com/docker-library/docs/commits/master/amazonlinux))

-	**Supported Docker versions**:
	[the latest release](https://github.com/docker/docker-ce/releases/latest) (down to 1.6 on a best-effort basis)

## What is Amazon Linux?

Amazon Linux is provided by Amazon Web Services (AWS). It is designed to provide a stable, secure, and high-performance execution environment for applications running on Amazon EC2. It also includes packages that enable easy integration with AWS, including launch configuration tools and many popular AWS libraries and tools. AWS provides ongoing security and maintenance updates to all instances running Amazon Linux.

AWS provides two versions of Amazon Linux: [Amazon Linux 2](https://aws.amazon.com/amazon-linux-2/) LTS Candidate and [Amazon Linux AMI](https://aws.amazon.com/amazon-linux-ami/).

![logo](https://raw.githubusercontent.com/docker-library/docs/9ca9202567ccc25ce110f98bbeb6c929844e05d0/amazonlinux/logo.png)

## Where can I run Amazon Linux container images?

You can run Amazon Linux container images in any Docker based environment. Examples include, your laptop, in AWS EC2 instances, and ECS clusters.

## How is Amazon Linux 2 LTS Candidate different from Amazon Linux AMI?

There are three major differences in Amazon Linux 2 LTS Candidate from its predecessors:

1.	it is available as a VM image for on-premises development and testing
2.	it includes systemd service and systems manager as opposed to System V init system and also includes new version of compiler and build tools
3.	it gives you the ability to install additional software packages through Extras mechanism without impacting the underlying LTS stability

## Is Amazon Linux 2 LTS Candidate (2017.12) build an official LTS build?

No, Amazon Linux 2 (2017.12) is a first candidate LTS build of Amazon Linux 2. In the coming weeks, we will continue to engage with our customers and the broader community to get feedback before announcing an official LTS build which will be supported for 5 years. You can provide your feedback through your designated AWS representative or directly through [Amazon Linux Discussion Forum](https://forums.aws.amazon.com/forum.jspa?forumID=228).

## What packages are available in the Amazon Linux containers?

Amazon Linux Docker container images contain a subset of the packages in the images for use on EC2 and as VMs in on-premises scenarios. The container images can be configured to use any of the full set of packages in images for EC2 and on-premises use. The Amazon Linux 2 LTS Candidate Container images comes with Extras included.

## What is an Amazon Linux 2 LTS Candidate Extra?

Extras is a new mechanism introduced in Amazon Linux 2 to enable the consumption of the newest versions of application software in a fully supported manner on a stable Amazon Linux 2 base. Extras help alleviate the compromise between stability of the OS and freshness of available software. For example, now you can install newer versions of Python being rest assured that the underlying operating system is stable. Examples of Extras include Python 3, nginx, Postgresql, MariaDB, Go, and Rust.

## How do Amazon Linux 2 LTS Candidate Extras work?

Extras introduces the notion of topics to select software bundles. Each topic contains all the dependencies required for the software to install and function on Amazon Linux 2. For example, Rust is an Extras topic in the curated list provide by Amazon. It provides the toolchain and runtimes for Rust, the systems programming language. This topic includes the cmake build system for Rust, cargo - the rust package manager, and the LLVM based compiler toolchain for Rust. The packages associated with each topic are consumed via the well-known yum installation process.

## How are Amazon Linux 2 LTS Candidate Extras topics different from the packages available in yum repositories?

`yum` is a utility for package management of RPM packages. The base image of Amazon Linux 2 (LTS) includes access to repositories that already contain stable versions of popular packages that can be installed with yum. These packages are part of the long term support for Amazon Linux 2.

However, if you need a new software package or a newer version of an existing software package that is not included in the base Amazon Linux 2 LTS Candidate image, Extras provide a way to install those packages in a supported manner. Extras is essentially a simplified mechanism to point yum to AWS curated sets of packages for a selected topic.

## How do I install a software package from Extras repository in Amazon Linux 2 LTS Candidate?

Available packages can be listed with the `amazon-linux-extras` command. Packages can be installed with the `sudo amazon-linux-extras install <package>` command. Example: sudo amazon-linux-extras install rust1

## Will updates be available for Amazon Linux containers?

Similar to the Amazon Linux images for AWS EC2 and on-premises use, Amazon Linux container images will get ongoing updates from Amazon in the form of security updates, bug fix updates, and other enhancements. Security bulletins for Amazon Linux are available at https://alas.aws.amazon.com/

## What support is available for Amazon Linux outside AWS?

-	Documentation: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/amazon-linux-ami-basics.html
-	Amazon Linux Forums: https://forums.aws.amazon.com/forum.jspa?forumID=228
-	Paid Support from AWS: https://aws.amazon.com/premiumsupport/

## With the availability of Amazon Linux 2 LTS Candidate, are there any changes to the existing version of Amazon Linux AMI (2017.09)?

With the availability of Amazon Linux 2, we are announcing that 2017.09 release of Amazon Linux AMI container image will be the last release for the current generation of Amazon Linux. Going forward, AWS will provide newer versions only for Amazon Linux 2.

## Will AWS support the current version of Amazon Linux going forward?

Yes; in order to avoid any disruption to your existing applications and to facilitate migration to Amazon Linux 2, AWS will provide regular security updates for Amazon Linux 2017.09 AMI and container image for 2 years after the final LTS build is announced. You can also use all your existing support channels such as AWS Premium Support and Amazon Linux Discussion Forum to continue to submit support requests.

## Is Amazon Linux 2 LTS Candidate backward compatible with the existing version of Amazon Linux?

Due to the inclusion of new components in Amazon Linux 2 such as systemd, your applications running on the current version of Amazon Linux may require additional changes to run on Amazon Linux 2.

# License

Amazon Linux is available under the [GNU General Public License, version 2.0](https://github.com/aws/amazon-linux-docker-images/blob/master/LICENSE). Individual software packages are available under their own licenses; run `rpm -qi [package name]` or check `/usr/share/doc/[package name]-*` and `/usr/share/licenses/[package name]-*` for details.

As with all Docker images, these likely also contain other software which may be under other licenses (such as Bash, etc from the base distribution, along with any direct or indirect dependencies of the primary software being contained).

Some additional license information which was able to be auto-detected might be found in [the `repo-info` repository's `amazonlinux/` directory](https://github.com/docker-library/repo-info/tree/master/repos/amazonlinux).

As for any pre-built image usage, it is the image user's responsibility to ensure that any use of this image complies with any relevant licenses for all software contained within.


text:
module.exports = {
  width: 1920,
  height: 1080,
  margin: 20,
  theme: require("../themes/default/index"),
  print: require("../themes/default/print"),
  html: require("../themes/default/html")
};


text:
export MACHTYPE=x86_64
export BINDIR=$(pwd)/bin
mkdir -p $BINDIR
(cd kent/src/lib && make)
(cd kent/src/jkOwnLib && make)
(cd kent/src/hg/lib && make)
(cd kent/src/utils/raToLines && make)
mkdir -p $PREFIX/bin
cp bin/raToLines $PREFIX/bin
chmod +x $PREFIX/bin/raToLines


text:
package org.github.yippee.netty.socketio;

//https://www.cnblogs.com/wxgblogs/p/6852470.html

import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SocketIOServers {
    private Map<String, SocketIOClient> clients = new ConcurrentHashMap<>();
    public void start(){
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setReuseAddress(true);
        socketConfig.setTcpNoDelay(true);
        socketConfig.setTcpKeepAlive(true);

//        socketConfig.setSoLinger(0);
        Configuration config = new Configuration();
        config.setPort(9999);
        config.setSocketConfig(socketConfig);
        config.setPingTimeout(6000000);
        config.setUpgradeTimeout(1000000);
        config.setPingInterval(25000);
        config.setBossThreads(2);
        config.setWorkerThreads(1000);

        config.setAllowCustomRequests(true);
//        config.setHostname("localhost");
        SocketIOServer server = new SocketIOServer(config);
//        server.addListeners(new EventListennter());
        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient client) {
                System.out.println(clients.size()+"建立连接 "+client.getSessionId());
                clients.put(client.getSessionId()+"",client);
            }
        });
        server.addDisconnectListener(new DisconnectListener() {
            @Override
            public void onDisconnect(SocketIOClient client) {
                System.out.println(clients.size()+"关闭连接 "+client.getSessionId());
                clients.remove(client.getSessionId()+"");
            }
        });
        server.addEventListener("/api/v1/box/login", String.class, new DataListener<String>() {
            @Override
            public void onData(SocketIOClient client, String data, AckRequest ackSender) throws Exception {
                System.out.println(clients.size()+"接收数据 "+data);
            }
        });
        server.start();
        System.out.println("启动正常");
    }

    public class EventListennter {

        //维护每个客户端的SocketIOClient
        private Map<String, SocketIOClient> clients = new ConcurrentHashMap<>();

        @OnConnect
        public void onConnect(SocketIOClient client) {
            System.out.println("建立连接 "+client.getSessionId());
            clients.put(client.getSessionId()+"",client);
        }

//        @OnEvent("token")
//        public void onToken(SocketIOClient client,String message) {
//            List<SocketIOClient> socketList = clients.get(message.getToken());
//            if (null == socketList || socketList.isEmpty()) {
//                List<SocketIOClient> list = new ArrayList<>();
//                list.add(client);
//                clients.put(message.getToken(), list);
//            }
//            System.err.println("get token Message is " + message.getToken());
//        }

        /**
         * 新事务
         * @param client 客户端
         * @param message 消息
         */
        @OnEvent("/api/v1/box/login")
        public void onLogin(SocketIOClient client, String message) {
            System.out.println("onLogin " + message);
            //send to all users
//            Collection<List<SocketIOClient>> clientsList = clients.values();
//            for (List<SocketIOClient> list : clientsList) {
//                for (SocketIOClient socketIOClient : list) {
//                    socketIOClient.sendEvent("newAlert", message);
//                }
//            }
        }

//        /**
//         * 通知所有在线客户端
//         */
//        public void sendAllUser() {
//            Set<Entry<String,List<SocketIOClient>>> entrySet = clients.entrySet();
//            for (Entry<String, List<SocketIOClient>> entry : entrySet) {
//                String key = entry.getKey();
//                List<SocketIOClient> value = entry.getValue();
//                for (SocketIOClient socketIOClient : value) {
//                    SocketIOMessage message = new SocketIOMessage();
//                    message.setMessage("send All user Msg" + key);
//                    socketIOClient.sendEvent("newAlert", message);
//                }
//            }
//        }

        @OnDisconnect
        public void onDisconnect(SocketIOClient client) {
            System.out.println("关闭连接 "+client.getSessionId());
        }
    }
    public static void main(String[] args) throws  Exception {
        SocketIOServers socketIOServers=new SocketIOServers();
        socketIOServers.start();
        Thread.sleep(Integer.MAX_VALUE);
        System.out.println("end " );
    }
}


text:
'use strict';

var redis = require('./redis');

function User() {
    this.prefix = 'user:';
    this.keyString = this.prefix + 'id:';
    this.hashExpire = 60 * 60 * 24 * 3;
}

User.prototype.setHash = function (userId, userObj, callback) {
    redis.client.hmset(this.keyString + userId, userObj, callback);
};

module.exports = new User;


text:
//
//  Copyright (c) 2016 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "FirebasePhoneAuthUI/Sources/FUIPhoneAuth_Internal.h"

#import <FirebaseAuthUI/FirebaseAuthUI.h>

#import "FirebasePhoneAuthUI/Sources/FUICountryCodes.h"
#import "FirebasePhoneAuthUI/Sources/FUIPhoneAuthStrings.h"
#import "FirebasePhoneAuthUI/Sources/FUIPhoneEntryViewController.h"

NS_ASSUME_NONNULL_BEGIN

@implementation FUIPhoneAuth {
  /** The @c FUIAuth instance of the application. */
  FUIAuth *_authUI;

  /** The callback which should be invoked when the sign in flow completes (or is cancelled.) */
  FUIAuthProviderSignInCompletionBlock _pendingSignInCallback;

  /** Available country codes For the authUI to use. */
  FUICountryCodes *_countryCodes;
}

+ (NSBundle *)bundle {
  return [FUIAuthUtils bundleNamed:FUIPhoneAuthBundleName
                 inFrameworkBundle:[NSBundle bundleForClass:[self class]]];
}

- (instancetype)initWithAuthUI:(FUIAuth *)authUI {
  if (self = [super init]) {
    _authUI = authUI;
    _countryCodes = [[FUICountryCodes alloc] init];
  }

  return self;
}

- (instancetype)initWithAuthUI:(FUIAuth *)authUI
          whitelistedCountries:(NSSet<NSString *> *)countries {
  NSParameterAssert(countries);
  NSParameterAssert(countries.count > 0);
  if (self = [self initWithAuthUI:authUI]) {
    [_countryCodes whitelistCountries:countries];
    NSAssert(_countryCodes.count, @"No available country code found.");
    if (!_countryCodes.count) {
      return nil;
    }
  }
  return self;
}

- (instancetype)initWithAuthUI:(FUIAuth *)authUI
          blacklistedCountries:(NSSet<NSString *> *)countries {
  if (!countries.count) {
    return nil;
  }
  if (self = [self initWithAuthUI:authUI]) {
    [_countryCodes blacklistCountries:countries];
    NSAssert(_countryCodes.count, @"No available country code found.");
    if (!_countryCodes.count) {
      return nil;
    }
  }
  return self;
}

#pragma mark - FUIAuthProvider

- (nullable NSString *)providerID {
  return FIRPhoneAuthProviderID;
}

/** @fn accessToken:
    @brief Phone Auth token is matched by FirebaseUI User Access Token
 */
- (nullable NSString *)accessToken {
  return nil;
}

/** @fn idToken:
    @brief Phone Auth Token Secret is matched by FirebaseUI User Id Token
 */
- (nullable NSString *)idToken {
  return nil;
}

- (NSString *)shortName {
  return @"Phone";
}

- (NSString *)signInLabel {
  return FUIPhoneAuthLocalizedString(kPAStr_SignInWithPhone);
}

- (UIImage *)icon {
  return [FUIAuthUtils imageNamed:@"ic_phone" fromBundle:[FUIPhoneAuth bundle]];
}

- (UIColor *)buttonBackgroundColor {
  return [UIColor colorWithRed:68.0f/255.0f green:197.0f/255.0f blue:166.0f/255.0f alpha:1.0f];
}

- (UIColor *)buttonTextColor {
  return [UIColor whiteColor];
}

- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController {
  [self signInWithPresentingViewController:presentingViewController phoneNumber:nil];
}

- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController
                               phoneNumber:(nullable NSString *)phoneNumber {
  [_authUI signInWithProviderUI:self presentingViewController:presentingViewController
                   defaultValue:phoneNumber];
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
- (void)signInWithEmail:(nullable NSString *)email
    presentingViewController:(nullable UIViewController *)presentingViewController
                  completion:(nullable FUIAuthProviderSignInCompletionBlock)completion {
  [self signInWithDefaultValue:email
      presentingViewController:presentingViewController
                    completion:completion];
}
#pragma clang diagnostic pop

- (void)signInWithDefaultValue:(nullable NSString *)defaultValue
      presentingViewController:(nullable UIViewController *)presentingViewController
                    completion:(nullable FUIAuthProviderSignInCompletionBlock)completion {
  _pendingSignInCallback = completion;

  FUIPhoneAuth *delegate = [_authUI providerWithID:FIRPhoneAuthProviderID];
  if (!delegate) {
    NSError *error = [FUIAuthErrorUtils errorWithCode:FUIAuthErrorCodeCantFindProvider
                                             userInfo:@{
                       FUIAuthErrorUserInfoProviderIDKey : FIRPhoneAuthProviderID
                     }];
    [self callbackWithCredential:nil error:error result:^(FIRUser *_Nullable user,
                                                          NSError *_Nullable error) {
      if (error) {
        [FUIAuthBaseViewController showAlertWithMessage:error.localizedDescription
                               presentingViewController:presentingViewController];
      }
    }];
    return;
  }

  UIViewController *controller = [[FUIPhoneEntryViewController alloc] initWithAuthUI:_authUI
                                                                         phoneNumber:defaultValue
                                                                        countryCodes:_countryCodes];
  UINavigationController *navigationController =
      [[UINavigationController alloc] initWithRootViewController:controller];
  [presentingViewController presentViewController:navigationController animated:YES completion:nil];
}

- (void)signOut {
  return;
}

- (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(nullable NSString *)sourceApplication {
  return NO;
}

- (void)callbackWithCredential:(nullable FIRAuthCredential *)credential
                         error:(nullable NSError *)error
                        result:(nullable FIRAuthResultCallback)result {
  FUIAuthProviderSignInCompletionBlock callback = _pendingSignInCallback;

  FIRAuthResultCallback resultAuthCallback = ^(FIRUser *_Nullable user, NSError *_Nullable error) {
    if (!error) {
      self->_pendingSignInCallback = nil;
    }
    if (result) {
      result(user, error);
    }
  };
  if (callback) {
    callback(credential, error, resultAuthCallback, nil);
  }
}

+ (UIAlertController *)alertControllerForError:(NSError *)error
                                 actionHandler:(nullable FUIAuthAlertActionHandler)actionHandler {
  NSString *message;
  if (error.code == FIRAuthErrorCodeInvalidPhoneNumber) {
    message = FUIPhoneAuthLocalizedString(kPAStr_IncorrectPhoneMessage);
  } else if (error.code == FIRAuthErrorCodeInvalidVerificationCode) {
    message = FUIPhoneAuthLocalizedString(kPAStr_IncorrectCodeMessage);
  } else if (error.code == FIRAuthErrorCodeTooManyRequests) {
    message = FUIPhoneAuthLocalizedString(kPAStr_TooManyCodesSent);
  } else if (error.code == FIRAuthErrorCodeQuotaExceeded) {
    message = FUIPhoneAuthLocalizedString(kPAStr_MessageQuotaExceeded);
  } else if (error.code == FIRAuthErrorCodeSessionExpired) {
    message = FUIPhoneAuthLocalizedString(kPAStr_MessageExpired);
  } else if ((error.code >= FIRAuthErrorCodeMissingPhoneNumber
             && error.code <= FIRAuthErrorCodeAppNotVerified)
             || error.code >= FIRAuthErrorCodeInternalError) {
    message = FUIPhoneAuthLocalizedString(kPAStr_InternalErrorMessage);
  } else {
    message = error.localizedDescription;
  }
  UIAlertController *alertController =
      [UIAlertController alertControllerWithTitle:nil
                                          message:message
                                   preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *okAction =
      [UIAlertAction actionWithTitle:FUIPhoneAuthLocalizedString(kPAStr_Done)
                              style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction *_Nonnull action) {
        if (actionHandler) {
          actionHandler();
        }
      }];
  [alertController addAction:okAction];
  return alertController;
}

@end

NS_ASSUME_NONNULL_END


text:
package com.google.code.twig.standard;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.ReadPolicy.Consistency;
import com.google.code.twig.LoadCommand.CacheMode;
import com.google.code.twig.Path;
import com.google.code.twig.Property;
import com.google.code.twig.PropertyTranslator;
import com.google.code.twig.Restriction;
import com.google.code.twig.Settings;
import com.google.code.twig.util.PropertySets;
import com.google.code.twig.util.RestrictionToPredicateAdaptor;
import com.google.common.collect.Sets;

@SuppressWarnings("unchecked")
class StandardDecodeCommand<C extends StandardDecodeCommand<C>> extends StandardCommand
{
//	private static final Logger log = Logger.getLogger(StandardDecodeCommand.class.getName());

	protected int depth;
	protected Restriction<Entity> entityRestriction;
	protected Restriction<Property> propertyRestriction;
	protected Settings.Builder settings;
	private boolean refresh;

	StandardDecodeCommand(TranslatorObjectDatastore datastore, int initialActivationDepth)
	{
		super(datastore);
		this.depth = initialActivationDepth;
	}

	public C refresh()
	{
		this.refresh = true;
		return (C) this;
	}

	public C restrictEntities(Restriction<Entity> restriction)
	{
		this.entityRestriction = restriction;
		return (C) this;
	}

	public C restrictProperties(Restriction<Property> restriction)
	{
		this.propertyRestriction = restriction;
		return (C) this;
	}

	public C cache(CacheMode cacheMode)
	{
		this.settings.cacheMode(cacheMode);
		return (C) this;
	}

	public final Object entityToInstance(Entity entity, Restriction<Property> restriction)
	{
		// we have the entity data but must return the associated instance
		Object instance = datastore.keyCache.getInstance(entity.getKey());

		// activate existing instance if we fetched the data
		if (instance != null)
		{
			// if we are freshing or the instance is not activated then load it
			if (depth >= 0 && (refresh || !datastore.isActivated(instance)))
			{
				// do not create new instance - reuse this one
				datastore.refresh = instance;

				// just to trigger the activation code
				instance = null;
			}
			else
			{
				// return the existing instance and do not load it with new data
				return instance;
			}
		}

		// push new decode context state
		Key existingDecodeKey = datastore.decodeKey;
		datastore.decodeKey = entity.getKey();
		StandardCommand existingCommand = datastore.command;

		// the activation depth may change while decoding a field value
		int existingActivationDepth = depth;

		Class<?> type = datastore.getConfiguration().kindToType(entity.getKind());

		Set<Property> properties = PropertySets.create(entity.getProperties(), false);

		// filter out unwanted properties at this low level
		if (restriction != null)
		{
			properties = Sets.filter(properties, new RestrictionToPredicateAdaptor<Property>(restriction));
		}

		// order the properties for efficient separation by field
		Set<Property> sorted = new TreeSet<Property>();
		sorted.addAll(properties);
		properties = sorted;

		PropertyTranslator decoder = datastore.decoder(entity);
		instance = decoder.decode(properties, Path.EMPTY_PATH, type);

		// pop the decode context after decode
		datastore.decodeKey = existingDecodeKey;
		depth = existingActivationDepth;
		datastore.command = existingCommand;

		// null signifies that the properties could not be decoded
		if (instance == null)
		{
			throw new IllegalStateException("Could not translate entity " + entity);
		}

		// a null value is indicated by this special return value
		if (instance == PropertyTranslator.NULL_VALUE)
		{
			instance = null;
		}

		// set the cached version number
		Long version = readEntityVersion(entity, type);
		if (version != null)
		{
			assert version > 0;
			datastore.keyCache.setVersion(instance, version);
		}
		else if (depth >= 0 && !datastore.isActivated(instance))
		{
			// set the version to 1 for activated non-versioned instances
			datastore.keyCache.setVersion(instance, 1);
		}

		return instance;
	}

	public final <T> Iterator<T> entitiesToInstances(final Iterator<Entity> entities, final Restriction<Property> filter)
	{
		return new Iterator<T>()
		{
			@Override
			public boolean hasNext()
			{
				return entities.hasNext();
			}

			@Override
			public T next()
			{
				datastore.command = StandardDecodeCommand.this;
				final T instance = (T) entityToInstance(entities.next(), filter);
				return instance;
			}

			@Override
			public void remove()
			{
				entities.remove();
			}
		};
	}

	// get from key cache or datastore
	public <T> T keyToInstance(Key key, Restriction<Property> filter)
	{
		T instance = (T) datastore.keyCache.getInstance(key);
		if (instance == null)
		{
			Entity entity = keyToEntity(key);
			if (entity == null)
			{
				instance = null;
			}
			else
			{
				instance = (T) entityToInstance(entity, filter);
			}
		}

		return instance;
	}

	public final <T> Map<Key, T> keysToInstances(Collection<Key> keys, Restriction<Property> filter)
	{
		// only load the instances we do not already have in the cache
		Map<Key, T> result = new HashMap<Key, T>(keys.size());
		List<Key> missing = null;
		for (Key key : keys)
		{
			T instance = (T) datastore.keyCache.getInstance(key);

			// if we are doing a refresh or activate then do load the entity
			if (instance == null || refresh)
			{
				if (missing == null)
				{
					missing = new ArrayList<Key>(keys.size());
				}
				missing.add(key);
			}
			else
			{
				result.put(key, instance);
			}
		}

		if (missing != null && !missing.isEmpty())
		{
			Map<Key, Entity> entities = keysToEntities(missing);

			// must decode in same order as keys - needed for refreshing
			for (Key key : missing)
			{
				Entity entity = entities.get(key);

				if (entity == null) continue;

//				// other instances that reference it will still work
//				if (datastore.refresh != null)
//				{
//					datastore.keyCache.evictKey(key);
//				}

				T instance = (T) entityToInstance(entity, filter);

				result.put(key, instance);
			}
		}

		return result;
	}

	final Entity keyToEntity(Key key)
	{
		if (depth >= 0)
		{
			try
			{
				return datastore.serviceGet(key, getSettings());
			}
			catch (EntityNotFoundException e)
			{
				return null;
			}
		}
		else
		{
			// don't load entity if it will not be activated - but need one for key
			return new Entity(key);
		}
	}

	final Map<Key, Entity> keysToEntities(Collection<Key> keys)
	{
		// only load entity if we will activate instance
		if (depth >= 0)
		{
			return datastore.serviceGet(keys, getSettings());
		}
		else
		{
			// we must return empty entities with the correct kind to instantiate
			HashMap<Key, Entity> result = new HashMap<Key, Entity>();
			for (Key key : keys)
			{
				result.put(key, new Entity(key));
			}
			return result;
		}
	}

	public C activate(int depth)
	{
		this.depth = depth;
		return (C) this;
	}

	public C activateAll()
	{
		this.depth = Integer.MAX_VALUE;
		return (C) this;
	}

	public C unactivated()
	{
		// do not activate the current instance - 0 means no referenced activated
		activate(-1);
		return (C) this;
	}

	public C consistency(Consistency consistency)
	{
		getSettingsBuilder().consistency(consistency);
		return (C) this;
	}

	public C deadline(long deadline, TimeUnit unit)
	{
		getSettingsBuilder().deadline(deadline, unit);
		return (C) this;
	}

	public Settings.Builder getSettingsBuilder()
	{
		if (settings == null)
		{
			settings = Settings.copy(datastore.getDefaultSettings());
		}
		return this.settings;
	}

	public Settings getSettings()
	{
		if (settings == null)
		{
			return datastore.getDefaultSettings();
		}
		else
		{
			return settings.build();
		}
	}

	void setDepth(int depth)
	{
		this.depth = depth;
	}

	int getDepth()
	{
		return depth;
	}
}


text:
package com.annimon.ownlang.lib;

/**
 *
 * @author aNNiMON
 */
public interface Function {

    Value execute(Value... args);
}


text:
<?php

namespace Mage\Catalog\Test\TestCase\Product;

use Mage\Catalog\Test\Fixture\CatalogCategory;
use Magento\Mtf\Fixture\FixtureFactory;
use Magento\Mtf\ObjectManager;
use Magento\Mtf\TestCase\Injectable;

/**
* Preconditions:
 * 1. Create category.
 * 2. Create products according to dataSet for with category created before.
 * 3. Apply configuration specified in dataSet.
 *
 * Steps:
 * 1. Perform assertions.
 *
 * @group Products_(CS)
 * @ZephyrId MPERF-6965
 */
class ApplyMapTest extends Injectable
{
    /**
     * Create category for test.
     *
     * @param FixtureFactory $fixtureFactory
     * @return array
     */
    public function __prepare(FixtureFactory $fixtureFactory)
    {
        $category = $fixtureFactory->createByCode('catalogCategory', ['dataSet' => 'default_subcategory']);
        $category->persist();

        return ['category' => $category];
    }

    /**
     * Run Apply MAP test.
     *
     * @param CatalogCategory $category
     * @param string $products
     * @param string $configData
     * @return array
     */
    public function test(CatalogCategory $category, $products, $configData)
    {
        // Preconditions:
        // Creating products:
        $products = $this->objectManager->create('Mage\Catalog\Test\TestStep\CreateProductsStep',
            [
                'products' => $products,
                'data' => $this->prepareProductData($category, $products)
            ]
        )->run()['products'];

        // Setup configuration:
        $this->objectManager->create('Mage\Core\Test\TestStep\SetupConfigurationStep',['configData' => $configData])
            ->run();

        return ['products' => $products, 'category' => $category];
    }

    /**
     * Prepare products data for persist.
     *
     * @param CatalogCategory $category
     * @param $products
     * @return array
     */
    protected function prepareProductData(CatalogCategory $category, $products)
    {
        $data = [];
        $productsCount = count(explode(',', $products));
        while ($productsCount > 0) {
            $data[] = ['category_ids' => ['category' => $category]];
            $productsCount--;
        }

        return $data;
    }

    /**
     * Rollback configuration after test variation.
     *
     * @return void
     */
    public function tearDown()
    {
        $this->objectManager->create(
            'Mage\Core\Test\TestStep\SetupConfigurationStep',
            ['configData' => $this->currentVariation['arguments']['configData'], 'rollback' => true]
        )->run();
    }
}


text:
import hy
import sys
import test

def entry_point(argv):
    return test.main(argv)

def target(driver, args):
    return entry_point, None

if __name__ == "__main__":
    entry_point(sys.argv)


text:
package osgi.extender.cdi.scopes;

import java.lang.annotation.Annotation;
import java.util.Collection;

/**
 * The interface to a CDI context within the CDI extender that should be used by bundles to manipulate
 * the setting of the current thread scope context as is defined by the CDI specification (one thread can
 * only have one scope). Normally, this would be used by servlet listeners to set the session scopes
 * and request scopes into the container before anything useful is done within the container using these
 * scopes.

 * In the extender context, the current context is identified by an object. Therefore as long as the object
 * passed is the same, contexts are handled as such.
 *
 * @author Arie van Wijngaarden
 */
public interface ExtenderContext {
    /**
     * Set the thread current context to the specified identifier. Note that this method assumes that
     * the context for this identifier already exists or at least will be added before any action is
     * done for the related scope.
     *
     * @param identifier The identifier to set as active context for this thread or null to remove it
     */
    public void setCurrent(Object identifier);
    /**
     * Add a context for the specific identifier.
     *
     * @param identifier The identifier
     */
    public void add(Object identifier);
    /**
     * Remove the context for the specific identifier, since it will not be used after this
     *
     * @param identifier The identifier
     */
    public void remove(Object identifier);
    /**
     * Get the scope of this context.
     *
     * @return The scope of the context
     */
    public Class<? extends Annotation> getScope();
    /**
     * Get the identifiers for this context.
     *
     * @return The identifiers for this context
     */
    public Collection<Object> getIdentifiers();
}


text:
package at.irian.ankor.event.dispatch;

import akka.actor.ActorRef;
import at.irian.ankor.event.Event;
import at.irian.ankor.session.ModelSession;

/**
* @author Manfred Geiler
*/
class AkkaConsistentHashingEventDispatcher implements EventDispatcher {

    private final ModelSession modelSession;
    private final ActorRef eventDispatcherActor;

    public AkkaConsistentHashingEventDispatcher(ModelSession modelSession, ActorRef eventDispatcherActor) {
        this.modelSession = modelSession;
        this.eventDispatcherActor = eventDispatcherActor;
    }

    @Override
    public void dispatch(Event event) {
        eventDispatcherActor.tell(new AkkaModelEventMsg(modelSession, event), ActorRef.noSender());
    }

    @Override
    public void close() {
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.handmark.pulltorefresh.extras.listfragment.test" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="22" />

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:functionalTest="false"
        android:handleProfiling="false"
        android:label="Tests for com.handmark.pulltorefresh.extras.listfragment.test"
        android:targetPackage="com.handmark.pulltorefresh.extras.listfragment.test" />

    <application>
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>


text:
2D

        2D+Phase

3D

3D+

        Normalize

Max. Amplitude: {{ctrl.maxState | number:6}}

        <canvas id="glcanvas" style="width:100%;"></canvas>

            State: |{{ctrl.stateNumber | number:0}}>

            Re: {{ctrl.stateRe | number:6}}

            Im: {{ctrl.stateIm | number:6}}

    <tabs>
      <pane title="Output">

              {{line}}

      </pane>
      <pane title="Call Stack">

              {{frame}}

      </pane>
      <pane title="Locals">

                Variable
                Value

                {{local[0]}}
                {{local[1]}}

      </pane>
    </tabs>

      <button type="button" class="btn btn-success" ng-click="ctrl.compile()"
              ng-disabled="ctrl.scriptIsLoading||ctrl.isRunning()">Compile</button>
      <button type="button" class="btn btn-danger" ng-click="ctrl.run()"
              ng-disabled="ctrl.scriptIsLoading||!ctrl.isCompiled()">
        {{ctrl.isRunning()?'   Stop   ':ctrl.isStopped()?'Continue':
        '    Run    '}}
      </button>
      <button type="button" class="btn btn-info" ng-click="ctrl.stepInto()"
              ng-disabled="ctrl.scriptIsLoading||!ctrl.isCompiled()||ctrl.isRunning()">
        Step Into</button>
      <button type="button" class="btn btn-info" ng-click="ctrl.stepOver()"
              ng-disabled="ctrl.scriptIsLoading||!ctrl.isStopped()">Step Over</button>
      <button type="button" class="btn btn-warning" ng-click="ctrl.stepBack()"
              ng-disabled="ctrl.scriptIsLoading||!ctrl.isStopped()">Step Back</button>

      <button type="button" class="btn btn-danger" ng-click="ctrl.newScript()"
              ng-show="ctrl.scriptCanUpdate">New</button>
      <button type="button" class="btn btn-success" ng-click="ctrl.saveScript()"
              ng-disabled="ctrl.scriptIsSaving||ctrl.scriptIsLoading">
        {{ctrl.scriptIsLoading?'Loading...':ctrl.scriptIsSaving?'Saving...':
        ctrl.scriptCanUpdate?'Update':'Save & Fork'}}
      </button>

            Title:
            <input class="form-control" type="text" ng-model="ctrl.scriptName" />

        Author: {{ctrl.scriptAuthor}}
        Created: {{ctrl.scriptCreated | date:'short'}}
        Modified: {{ctrl.scriptModified | date:'short'}}

 0">Comments

 0">
      Your comment:
      <input class="form-control" type="text" ng-model="ctrl.content" />

        <button type="button" class="btn btn-success" ng-click="ctrl.saveComment()">Submit</button>

        {{comment.created | date:'medium'}}

        {{comment.author}}   {{comment.content}}
        <button class="btn btn-danger btn-xs" ng-show="comment.id.length > 0"
                ng-click="ctrl.deleteComment(comment.id)">
          X</button>