Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vizobject/vizcounter not working for Inheritance #367

Open
WangJIanNNN opened this issue Sep 21, 2023 · 3 comments
Open

vizobject/vizcounter not working for Inheritance #367

WangJIanNNN opened this issue Sep 21, 2023 · 3 comments

Comments

@WangJIanNNN
Copy link

WangJIanNNN commented Sep 21, 2023

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

@gaogaotiantian
Copy link
Owner

You set trigger_on_change=False and that means do not log attribute changes.

@WangJIanNNN
Copy link
Author

Ah get it, and it is working now, and other questions:

  1. what does the showing number "25" stand for?
    image

  2. 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)

  3. 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

@gaogaotiantian
Copy link
Owner

  1. 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.
  2. log_var() is a single point log, it won't keep track of anything. Consider that as a print.
  3. Yes, if you have a lot of attributes you should turn off trigger_on_change, and manually log() 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 using trigger_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants