-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from sarchlab/Task-Based-Backtracing
feat BackTraceTracer
- Loading branch information
Showing
6 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package tracing | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
// TaskPrinter can print tasks with a format. | ||
type TaskPrinter interface { | ||
Print(task Task) | ||
} | ||
|
||
type defaultTaskPrinter struct { | ||
} | ||
|
||
func (p *defaultTaskPrinter) Print(task Task) { | ||
fmt.Printf("%s-%s@%s\n", task.Kind, task.What, task.Where) | ||
} | ||
|
||
// BackTraceTracer can record tasks incomplete tasks | ||
type BackTraceTracer struct { | ||
printer TaskPrinter | ||
tracingTasks map[string]Task | ||
lock sync.Mutex | ||
} | ||
|
||
// NewBackTraceTracer creates a new BackTraceTracer | ||
func NewBackTraceTracer(printer TaskPrinter) *BackTraceTracer { | ||
t := &BackTraceTracer{ | ||
printer: printer, | ||
tracingTasks: make(map[string]Task), | ||
} | ||
|
||
if t.printer == nil { | ||
t.printer = &defaultTaskPrinter{} | ||
} | ||
|
||
return t | ||
} | ||
|
||
func (t *BackTraceTracer) StartTask(task Task) { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
||
t.tracingTasks[task.ID] = task | ||
} | ||
|
||
func (t *BackTraceTracer) StepTask(task Task) { | ||
// Do Nothing | ||
} | ||
|
||
func (t *BackTraceTracer) EndTask(task Task) { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
||
delete(t.tracingTasks, task.ID) | ||
} | ||
|
||
func (t *BackTraceTracer) DumpBackTrace(task Task) { | ||
t.printer.Print(task) | ||
|
||
if task.ParentID == "" { | ||
return | ||
} | ||
|
||
parentTask, ok := t.tracingTasks[task.ParentID] | ||
if !ok { | ||
return | ||
} | ||
|
||
t.DumpBackTrace(parentTask) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package tracing | ||
|
||
import ( | ||
gomock "github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("BackTraceTracer", func() { | ||
var ( | ||
mockCtrl *gomock.Controller | ||
mockTaskPrinter *MockTaskPrinter | ||
t *BackTraceTracer | ||
) | ||
|
||
BeforeEach(func() { | ||
mockCtrl = gomock.NewController(GinkgoT()) | ||
mockTaskPrinter = NewMockTaskPrinter(mockCtrl) | ||
|
||
t = NewBackTraceTracer(mockTaskPrinter) | ||
}) | ||
|
||
AfterEach(func() { | ||
mockCtrl.Finish() | ||
}) | ||
|
||
It("should trace a single task", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(1)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
}) | ||
|
||
It("should trace two tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(2)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
Expect(t.tracingTasks["2"].ParentID).To(Equal("1")) | ||
}) | ||
|
||
It("should trace three tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
t.StartTask(Task{ID: "3", ParentID: "2"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(3)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
Expect(t.tracingTasks["2"].ParentID).To(Equal("1")) | ||
Expect(t.tracingTasks["3"].ParentID).To(Equal("2")) | ||
}) | ||
|
||
It("should end a single task", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
|
||
t.EndTask(Task{ID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(0)) | ||
}) | ||
|
||
It("should end two tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
t.StartTask(Task{ID: "3", ParentID: "2"}) | ||
|
||
t.EndTask(Task{ID: "3", ParentID: "2"}) | ||
t.EndTask(Task{ID: "2", ParentID: "1"}) | ||
|
||
Expect(len(t.tracingTasks)).To(Equal(1)) | ||
Expect(t.tracingTasks["1"].ParentID).To(Equal("")) | ||
}) | ||
|
||
It("should print single tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
|
||
mockTaskPrinter.EXPECT().Print(Task{ID: "1"}) | ||
|
||
t.DumpBackTrace(Task{ID: "1"}) | ||
}) | ||
|
||
It("should print three tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
t.StartTask(Task{ID: "3", ParentID: "2"}) | ||
|
||
e3 := mockTaskPrinter.EXPECT(). | ||
Print(Task{ID: "3", ParentID: "2"}) | ||
e2 := mockTaskPrinter.EXPECT(). | ||
Print(Task{ID: "2", ParentID: "1"}). | ||
After(e3) | ||
mockTaskPrinter.EXPECT().Print(Task{ID: "1"}). | ||
After(e2) | ||
|
||
t.DumpBackTrace(Task{ID: "3", ParentID: "2"}) | ||
}) | ||
|
||
It("should print three tasks", func() { | ||
t.StartTask(Task{ID: "1"}) | ||
t.StartTask(Task{ID: "2", ParentID: "1"}) | ||
t.StartTask(Task{ID: "3", ParentID: "2"}) | ||
|
||
t.EndTask(Task{ID: "2", ParentID: "1"}) | ||
|
||
mockTaskPrinter.EXPECT(). | ||
Print(Task{ID: "3", ParentID: "2"}) | ||
|
||
t.DumpBackTrace(Task{ID: "3", ParentID: "2"}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters