vizobject/vizcounter not working for Inheritance
WangJIanNNN opened this issue · 3 comments
Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
For details: https://github.com/gaogaotiantian/viztracer/blob/master/NOTICE.txt
from viztracer import VizCounter, VizTracer, VizObject
import time
class Hello(VizCounter):
def __init__(self, tracer, name):
super().__init__(tracer, name, trigger_on_change=False)
self.test = 1
def change(self):
self.test += 1
class TestCounterClass():
def test_basic(self):
tracer = VizTracer(verbose=0)
tracer.start()
counter = VizCounter(tracer, "name")
counter.a = 1
counter.b = 2
tracer.stop()
def test_exception(self):
tracer = VizTracer(verbose=0)
tracer.start()
counter = VizCounter(tracer, "name")
with self.assertRaises(Exception) as _:
counter.a = ""
with self.assertRaises(Exception) as _:
counter.b = {}
with self.assertRaises(Exception) as _:
counter.c = []
tracer.stop()
tracer.clear()
def test_inherit(self, tracer):
a = Hello(tracer, "name")
retry = 10
while retry !=0 :
retry -= 1
a.change()
time.sleep(0.1)
a.log()
def test_notracer(self):
counter = VizCounter(None, "name")
counter.a = 1
counter.b = 2
a = Hello(None, "name")
a.b = 1
a.c = 2
a.d = 3
a.log()
c = TestCounterClass()
tracer = VizTracer()
tracer.start()
c.test_inherit(tracer)
tracer.stop()
tracer.save(r'/tmp/example1.json')
just a simple example from test , but member "test" of a not changed from the report
You set trigger_on_change=False
and that means do not log attribute changes.
Ah get it, and it is working now, and other questions:
-
if log_var() could trace the value change? tried like this but from the report b_test stay 100 without change
b_test = 100
tracer = get_tracer()
tracer.log_var("name for the var", b_test)
while retry !=0 :
retry -= 1
b_test += 1
a.change()
time.sleep(0.1) -
description here is a little bit confusing,
If your class has a lot of attributes and they are frequently being written to, it is wise to turn off trigger_on_change
seems that we should use "include_attributes", because "trigger_on_change=False" means nothing for users from report view
- I think it's a legend -> meaning the full scale is 25. However, it's determined by Perfetto and viztracer has little control over it.
log_var()
is a single point log, it won't keep track of anything. Consider that as aprint
.- Yes, if you have a lot of attributes you should turn off
trigger_on_change
, and manuallylog()
when you want to.include_attributes
is what you need if you want to keep the triggering but limit the attributes being logged. It takes an extra check, compared to usingtrigger_on_change=False
.
To be honest, this is a feature that I implemented early and I had to go back to the code to understand the mechanism now :) You might be one of the few people that are using this.