Best Gauge code snippet using execution.Pid
tracer.go
Source:tracer.go
...14// Execution represents a complete command execution that started and finished15// while the tracer was active.16//17type Execution struct {18 Pid, Ppid uint3219 ExitCode int20 Time uint6421 Argv []string22 Start, Finish uint6423}24// payload is the data communicated between the kernel and userspace through a25// perf map.26//27type payload struct {28 Type int3229 Comm [16]byte30 ExitCode int3231 Pid, Ppid uint3232 Ts uint6433}34type EventType int3235const (36 EventStart EventType = iota + 137 EventFinish38)39type Tracer struct {40 procs map[uint32]Execution41 mod *bcc.Module42}43func New() (t Tracer) {44 return Tracer{45 mod: bcc.NewModule(src, []string{}),46 procs: make(map[uint32]Execution, 0),47 }48}49func (t *Tracer) Close() {50 t.mod.Close()51}52func (t *Tracer) Run(ctx context.Context) (err error) {53 err = attachProbes(t.mod)54 if err != nil {55 err = fmt.Errorf("attaching probes: %w", err)56 return57 }58 err = t.loop(ctx)59 if err != nil {60 err = fmt.Errorf("event loop: %w", err)61 return62 }63 return64}65func (t *Tracer) loop(ctx context.Context) (err error) {66 var (67 evs = bcc.NewTable(t.mod.TableId("events"), t.mod)68 evsC = make(chan []byte, 100)69 execsC = make(chan Execution, 1)70 )71 perfMap, err := bcc.InitPerfMap(evs, evsC)72 if err != nil {73 err = fmt.Errorf("perf map: %w", err)74 return75 }76 defer perfMap.Stop()77 go func() {78 for data := range evsC {79 dispatch(data, t.start, t.finish, execsC)80 }81 }()82 perfMap.Start()83 fmt.Printf("%-16s %-16s %-16s %-16s %-16s \n",84 "PID", "PPID", "CODE", "TIME(s)", "ARGV")85 for {86 select {87 case <-ctx.Done():88 err = ctx.Err()89 return90 case execution := <-execsC:91 fmt.Printf("%-16d %-16d %-16d %-16d %-16s \n",92 execution.Pid,93 execution.Ppid,94 execution.ExitCode,95 0,96 strings.Join(execution.Argv, " "))97 }98 }99 return100}101func procArgv(pid uint32) (argv []string, err error) {102 f, err := os.Open("/proc/" + strconv.Itoa(int(pid)) + "/cmdline")103 if err != nil {104 return105 }106 defer f.Close()107 s, err := ioutil.ReadAll(f)108 if err != nil {109 return110 }111 for _, b := range bytes.Split(s, []byte{'\x00'}) {112 argv = append(argv, string(b))113 }114 return115}116func (t *Tracer) start(p payload) (err error) {117 execution := Execution{118 Pid: p.Pid,119 Ppid: p.Ppid,120 Start: p.Ts,121 }122 execution.Argv, err = procArgv(p.Pid)123 if err != nil {124 return125 }126 t.procs[p.Pid] = execution127 return128}129func (t *Tracer) finish(p payload) (execution *Execution, err error) {130 e, found := t.procs[p.Pid]131 if !found {132 return133 }134 delete(t.procs, p.Pid)135 execution = &e136 return137}138func dispatch(data []byte,139 start func(payload) error,140 finish func(payload) (*Execution, error),141 results chan<- Execution,142) (err error) {143 var (144 p payload145 execution *Execution146 )147 err = binary.Read(bytes.NewBuffer(data), bcc.GetHostByteOrder(), &p)148 if err != nil {...
roundRobin_test.go
Source:roundRobin_test.go
...11 {ID: 2, ArrivalTime: 4, Duration: 6},12 {ID: 3, ArrivalTime: 4, Duration: 8},13 }14 processesExecution := []p.ProcessExecution{15 {Pid: 0, StartTime: 0, FinishTime: 2},16 {Pid: 1, StartTime: 2, FinishTime: 4},17 {Pid: 0, StartTime: 4, FinishTime: 6},18 {Pid: 2, StartTime: 6, FinishTime: 8},19 {Pid: 3, StartTime: 8, FinishTime: 10},20 {Pid: 1, StartTime: 10, FinishTime: 12},21 {Pid: 0, StartTime: 12, FinishTime: 14},22 {Pid: 2, StartTime: 14, FinishTime: 16},23 {Pid: 3, StartTime: 16, FinishTime: 18},24 {Pid: 1, StartTime: 18, FinishTime: 20},25 {Pid: 0, StartTime: 20, FinishTime: 22},26 {Pid: 2, StartTime: 22, FinishTime: 24},27 {Pid: 3, StartTime: 24, FinishTime: 26},28 {Pid: 1, StartTime: 26, FinishTime: 28},29 {Pid: 0, StartTime: 28, FinishTime: 30},30 {Pid: 3, StartTime: 30, FinishTime: 32},31 {Pid: 1, StartTime: 32, FinishTime: 34},32 {Pid: 0, StartTime: 34, FinishTime: 36},33 {Pid: 0, StartTime: 36, FinishTime: 38},34 {Pid: 0, StartTime: 38, FinishTime: 40},35 {Pid: 0, StartTime: 40, FinishTime: 42},36 {Pid: 0, StartTime: 42, FinishTime: 44},37 }38 got := RoundRobin(processes)39 want := processesExecution40 if !reflect.DeepEqual(got, want) {41 t.Errorf("got %v, wanted %v", got, want)42 }43}44func TestRoundRobinDurationNotMod2(t *testing.T) {45 processes := []p.Process{46 {ID: 1, ArrivalTime: 0, Duration: 6},47 {ID: 2, ArrivalTime: 0, Duration: 3},48 {ID: 3, ArrivalTime: 0, Duration: 1},49 {ID: 4, ArrivalTime: 0, Duration: 7},50 }51 processesExecution := []p.ProcessExecution{52 {Pid: 1, StartTime: 0, FinishTime: 2},53 {Pid: 2, StartTime: 2, FinishTime: 4},54 {Pid: 3, StartTime: 4, FinishTime: 5},55 {Pid: 4, StartTime: 5, FinishTime: 7},56 {Pid: 1, StartTime: 7, FinishTime: 9},57 {Pid: 2, StartTime: 9, FinishTime: 10},58 {Pid: 4, StartTime: 10, FinishTime: 12},59 {Pid: 1, StartTime: 12, FinishTime: 14},60 {Pid: 4, StartTime: 14, FinishTime: 16},61 {Pid: 4, StartTime: 16, FinishTime: 17},62 }63 got := RoundRobin(processes)64 want := processesExecution65 if !reflect.DeepEqual(got, want) {66 t.Errorf("\ngot %v \nwanted %v", got, want)67 }68}69func TestRoundRobinWithIdleTime(t *testing.T) {70 processes := []p.Process{71 {ID: 1, ArrivalTime: 0, Duration: 1},72 {ID: 2, ArrivalTime: 2, Duration: 2},73 {ID: 3, ArrivalTime: 2, Duration: 1},74 }75 processesExecution := []p.ProcessExecution{76 {Pid: 1, StartTime: 0, FinishTime: 1},77 {Pid: 2, StartTime: 2, FinishTime: 4},78 {Pid: 3, StartTime: 4, FinishTime: 5},79 }80 got := RoundRobin(processes)81 want := processesExecution82 if !reflect.DeepEqual(got, want) {83 t.Errorf("\ngot %v \nwanted %v", got, want)84 }85}86func TestRoundRobinFinal(t *testing.T) {87 processes := []p.Process{88 {ID: 1, ArrivalTime: 0, Duration: 7},89 {ID: 2, ArrivalTime: 2, Duration: 4},90 {ID: 3, ArrivalTime: 4, Duration: 1},91 {ID: 4, ArrivalTime: 5, Duration: 4},92 }93 processesExecution := []p.ProcessExecution{94 {Pid: 1, StartTime: 0, FinishTime: 2},95 {Pid: 2, StartTime: 2, FinishTime: 4},96 {Pid: 1, StartTime: 4, FinishTime: 6},97 {Pid: 3, StartTime: 6, FinishTime: 7},98 {Pid: 2, StartTime: 7, FinishTime: 9},99 {Pid: 4, StartTime: 9, FinishTime: 11},100 {Pid: 1, StartTime: 11, FinishTime: 13},101 {Pid: 4, StartTime: 13, FinishTime: 15},102 {Pid: 1, StartTime: 15, FinishTime: 16},103 }104 got := RoundRobin(processes)105 want := processesExecution106 if !reflect.DeepEqual(got, want) {107 t.Errorf("\ngot %v \nwanted %v", got, want)108 }109}...
averageTime_test.go
Source:averageTime_test.go
...8 {ID: 0, ArrivalTime: 0, Duration: 20}, {ID: 1, ArrivalTime: 0, Duration: 10},9 {ID: 2, ArrivalTime: 4, Duration: 6}, {ID: 3, ArrivalTime: 4, Duration: 8},10 }11 processesExecution := []ProcessExecution{12 {Pid: 0, StartTime: 0, FinishTime: 20},13 {Pid: 1, StartTime: 20, FinishTime: 30},14 {Pid: 2, StartTime: 30, FinishTime: 36},15 {Pid: 3, StartTime: 36, FinishTime: 44},16 }17 ttTime, responseTime, waitTime := AverageTime(processes, processesExecution, false)18 got := []float32{ttTime, responseTime, waitTime}19 want := []float32{30.5, 19.5, 19.5}20 if !reflect.DeepEqual(got, want) {21 t.Errorf("got %v, wanted %v", got, want)22 }23}24func TestCalcSjf(t *testing.T) {25 processes := []Process{26 {ID: 0, ArrivalTime: 0, Duration: 20}, {ID: 1, ArrivalTime: 0, Duration: 10},27 {ID: 2, ArrivalTime: 4, Duration: 6}, {ID: 3, ArrivalTime: 4, Duration: 8},28 }29 processesExecution := []ProcessExecution{30 {Pid: 1, StartTime: 0, FinishTime: 10},31 {Pid: 2, StartTime: 10, FinishTime: 16},32 {Pid: 3, StartTime: 16, FinishTime: 24},33 {Pid: 0, StartTime: 24, FinishTime: 44},34 }35 ttTime, responseTime, waitTime := AverageTime(processes, processesExecution, false)36 got := []float32{ttTime, responseTime, waitTime}37 want := []float32{21.5, 10.5, 10.5}38 if !reflect.DeepEqual(got, want) {39 t.Errorf("got %v, wanted %v", got, want)40 }41}42func TestCalcRoundRobin(t *testing.T) {43 processes := []Process{44 {ID: 0, ArrivalTime: 0, Duration: 20},45 {ID: 1, ArrivalTime: 0, Duration: 10},46 {ID: 2, ArrivalTime: 4, Duration: 6},47 {ID: 3, ArrivalTime: 4, Duration: 8},48 }49 processesExecution := []ProcessExecution{50 {Pid: 0, StartTime: 0, FinishTime: 2},51 {Pid: 1, StartTime: 2, FinishTime: 4},52 {Pid: 0, StartTime: 4, FinishTime: 6},53 {Pid: 2, StartTime: 6, FinishTime: 8},54 {Pid: 3, StartTime: 8, FinishTime: 10},55 {Pid: 1, StartTime: 10, FinishTime: 12},56 {Pid: 0, StartTime: 12, FinishTime: 14},57 {Pid: 2, StartTime: 14, FinishTime: 16},58 {Pid: 3, StartTime: 16, FinishTime: 18},59 {Pid: 1, StartTime: 18, FinishTime: 20},60 {Pid: 0, StartTime: 20, FinishTime: 22},61 {Pid: 2, StartTime: 22, FinishTime: 24},62 {Pid: 3, StartTime: 24, FinishTime: 26},63 {Pid: 1, StartTime: 26, FinishTime: 28},64 {Pid: 0, StartTime: 28, FinishTime: 30},65 {Pid: 3, StartTime: 30, FinishTime: 32},66 {Pid: 1, StartTime: 32, FinishTime: 34},67 {Pid: 0, StartTime: 34, FinishTime: 36},68 {Pid: 0, StartTime: 36, FinishTime: 38},69 {Pid: 0, StartTime: 38, FinishTime: 40},70 {Pid: 0, StartTime: 40, FinishTime: 42},71 {Pid: 0, StartTime: 42, FinishTime: 44},72 }73 ttTime, responseTime, waitTime := AverageTime(processes, processesExecution, true)74 got := []float32{ttTime, responseTime, waitTime}75 want := []float32{31.5, 2.0, 20.5}76 if !reflect.DeepEqual(got, want) {77 t.Errorf("got %v, wanted %v", got, want)78 }79}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!