如何使用Firebase计算延迟

时间:2017-11-27 17:51:07

标签: android firebase firebase-realtime-database latency

我刚刚开始学习Android编程,我正在使用在线资源来使用Firebase创建聊天应用程序。我有两个用户Alice和Bob,我想计算以下内容: 1.当Alice向Bob发送消息时,Alice的消息更新到Firebase数据库所需的时间 2.端到端延迟,即Alice发送消息和Bob接收消息之间的总时间。

上传邮件时,我正在上传系统当前时间。我无法弄清楚如何计算端到端延迟。这意味着,我应该在哪个部分代码中测量停止时间,并在停止时间和上传消息的时间之间取一个区别?我附上下面的代码供您参考:

public class MainActivity extends AppCompatActivity {
    int SIGN_IN_REQUEST_CODE = 10;
    boolean right_side = true;
    int layout_resource = 0;
    private FirebaseListAdapter<ChatMessage> adapter;
    DatabaseReference db = FirebaseDatabase.getInstance().getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listofmessages = (ListView) findViewById(R.id.list_of_messages);
        adapter=new messageadapter(this,ChatMessage.class,R.layout.leftchat,FirebaseDatabase.getInstance().getReference());

        if (FirebaseAuth.getInstance().getCurrentUser() == null) {
            //sign in / sign up
            startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE);
        } else {//user is signed in already

            Toast.makeText(this, "Welcome" + FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), Toast.LENGTH_LONG).show();
            displaychatmessages();
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText input_text = (EditText) findViewById(R.id.input);
                //        Toast.makeText(MainActivity.this,input_text.getText().toString(),Toast.LENGTH_LONG).show();
                //Read input field and upload to DB
                Log.d("where", "read the msg to push to db");

                db.push().setValue(new ChatMessage(input_text.getText().toString(),FirebaseAuth.getInstance().getCurrentUser().getDisplayName()));//FirebaseAuth.getInstance().getCurrentUser().getDisplayName()));
                Log.d("where", "pushed data to db");
                //  Toast.makeText(MainActivity.this,"message entered",Toast.LENGTH_LONG).show();;
                //clear input for next input
                input_text.setText("");
            }
        });
    }

    public void displaychatmessages() {
        ListView listofmessages = (ListView) findViewById(R.id.list_of_messages);
        adapter=new messageadapter(this,ChatMessage.class,R.layout.leftchat,FirebaseDatabase.getInstance().getReference());
        listofmessages.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SIGN_IN_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "Successfully signed in.Welcome !", Toast.LENGTH_LONG).show();
                //display chat msg
                displaychatmessages();
            } else {
                Toast.makeText(this, "We couldnt sign you in. Good bye ! ", Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }
}

消息适配器类:

public class messageadapter extends FirebaseListAdapter<ChatMessage> {
    HashMap<String,String> timemap=new HashMap<String,String>();
    private MainActivity activity;
    long local_start, local_stop;

    public messageadapter(MainActivity activity, Class<ChatMessage>
            modelClass, int modelLayout,DatabaseReference ref) {
        // FirebaseListOptions<ChatMessage> options=
        super(new FirebaseListOptions.Builder<ChatMessage>()
            .setQuery(ref, modelClass)
            .setLayout(modelLayout)
            .build());
        //super(activity,modelClass,modelLayout,ref);
        this.activity=activity;
        local_start=System.currentTimeMillis();
    }

    @Override
    protected void populateView(View v, ChatMessage model, int position) {
        TextView messageText=(TextView)v.findViewById(R.id.message_text);
        TextView timedifference=(TextView)v.findViewById(R.id.time_difference);
        TextView messageTime=(TextView)v.findViewById(R.id.message_time);

        //set Text
        messageText.setText(model.getMessageText());

        android.text.format.DateFormat df=new android.text.format.DateFormat();
        messageTime.setText(df.format("dd-MM-yyyy (HH:mm:ss)",model.getMessageTime()));
        long milli,milli2,diff;
        if(timemap.containsKey(model.getMessageId())) {
            timedifference.setText(timemap.get(model.getMessageId()));

        } else { //local_stop=System.currentTimeMillis();
            milli=System.currentTimeMillis();
            milli2=model.getMillitime();
            diff=local_stop-local_start;
            timemap.put(model.getMessageId(),String.valueOf(local_start-milli2));
            timedifference.setText(String.valueOf(milli-milli2));
        }
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ChatMessage chatMessage=getItem(position);
        if(chatMessage.getMessageUser().equals(FirebaseAuth.getInstance().getCurrentUser().getDisplayName())) {
            view=activity.getLayoutInflater().inflate(R.layout.leftchat,viewGroup,false);
        } else {
            view=activity.getLayoutInflater().inflate(R.layout.rightchat,viewGroup,false);
        }

        populateView(view,chatMessage,position);
        return view;
    }

    @Override
    public int getViewTypeCount() {
        // return the total number of view types. this value should never change
        // at runtime
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        // return a value between 0 and (getViewTypeCount - 1)
        return position % 2;
    }
}

目前,性能监控仪表板未显示任何数据,因此我尝试发送多条消息以达到所需数量的消息,以便Firebase进行采样并显示数据。

非常感谢任何计算2次测量的帮助。谢谢!

0 个答案:

没有答案