الواجهة Systrace في React Native

من موسوعة حسوب
< ReactNative
مراجعة 14:13، 9 أكتوبر 2021 بواسطة جميل-بيلوني (نقاش | مساهمات)
(فرق) → مراجعة أقدم | المراجعة الحالية (فرق) | مراجعة أحدث ← (فرق)
اذهب إلى التنقل اذهب إلى البحث

إن Systrace هي أداة تعريف الأداء، تعتمد على العلامات (marker-based profiling tool) المعيارية لمنصة Android، وتُثبّت مع حزمة أدوات منصّة Android. تُحاط كتل الشفرة البرمجيّة المشخَّصة بوسوم البداية، والنهاية فتظهر على شكل مخططٍ ملوَّنٍ، وتحوي كل من Android SDK، و React Native على الوسوم المعيارية التي يمكن إظهارها.

مثال

تسمح Systrace بوسمٍ أحداث ‎‎‏JavaScript بوسمٍ، وبقيمةٍ صحيحةٍ، وهذا المثال لالتقاط أحداث JS غير المرتبطة بالزمن (non-Timed) ضمن EasyProfiler.

import React from "react";
import { Button, Text, View, SafeAreaView, StyleSheet, Systrace } from "react-native";

const App = () =>  {

  const enableProfiling = () => {
    Systrace.setEnabled(true); // Call setEnabled to turn on the profiling.
    Systrace.beginEvent('event_label')
    Systrace.counterEvent('event_label', 10);
  }

  const stopProfiling = () => {
    Systrace.endEvent()
  }

  return (
    <SafeAreaView style={styles.container}>
      <Text style={[styles.header, styles.paragraph]}>React Native Systrace API</Text>
    <View style={styles.buttonRow}>
      <Button title="Capture the non-Timed JS events in EasyProfiler" onPress={()=> enableProfiling()}/>
      <Button title="Stop capturing" onPress={()=> stopProfiling()} color="#FF0000"/>
    </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: 44,
    padding: 8
  },
   header: {
    fontSize: 18,
    fontWeight: "bold",
    textAlign: "center"
  },
  paragraph: {
    margin: 24,
    fontSize: 25,
    textAlign: "center"
  },
  buttonRow: {
    flexBasis: 150,
    marginVertical: 16,
    justifyContent: 'space-evenly'
  }
});

export default App;
import React, { Component } from "react";
import { Button, Text, View, SafeAreaView, StyleSheet, Systrace } from "react-native";

class App extends Component {

  enableProfiling = () => {
    Systrace.setEnabled(true); // Call setEnabled to turn on the profiling.
    Systrace.beginEvent('event_label')
    Systrace.counterEvent('event_label', 10);
  }

  stopProfiling = () => {
    Systrace.endEvent()
  }

  render() {
    return (
      <SafeAreaView style={styles.container}>
        <Text style={[styles.header, styles.paragraph]}>React Native Systrace API</Text>
      <View style={styles.buttonRow}>
        <Button title="Capture the non-Timed JS events in EasyProfiler" onPress={()=> this.enableProfiling()}/>
        <Button title="Stop capturing" onPress={()=> this.stopProfiling()} color="#FF0000"/>
      </View>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: 44,
    padding: 8
  },
   header: {
    fontSize: 18,
    fontWeight: "bold",
    textAlign: "center"
  },
  paragraph: {
    margin: 24,
    fontSize: 25,
    textAlign: "center"
  },
  buttonRow: {
    flexBasis: 150,
    marginVertical: 16,
    justifyContent: 'space-evenly'
  }
});

export default App;

التوابع

installReactHook()‎

static installReactHook(useFiber)

setEnabled()‎

static setEnabled(enabled)

isEnabled()‎

static isEnabled()

beginEvent()‎

static beginEvent(profileName?, args?)

يُستخدم هذا التابع لبدء التشخيص.

endEvent()‎

static endEvent()

يُستخدم هذا التابع لإنهاء التشخيص، ويطلب بعد بدء التابع ()beginEvent التشخيص ضمن إطار مكدس الاستدعاءات.

beginAsyncEvent()‎

static beginAsyncEvent(profileName?)

يُستخدم التابع ‎‏()beginAsyncEvent لبدء التشخيص ثم يقوم التابع ()endAsyncEvent بإنهائه، ويمكن أن يحصل الإنهاء ضمن عمليّةٍ (thread) أخرى أو خارج إطار مكدس الاستدعاءات، مثل انتظار إعادة قيمة المتغير cookie المستخدم كدخل في استدعاء تابع الإنهاء ()endAsyncEvent.

endAsyncEvent()‎

static endAsyncEvent(profileName?, cookie?)

counterEvent()‎

static counterEvent(profileName?, value?)

يُستخدم هذا التّابع لتسجيل قيمة المتغيّر profileName ضمن الخطّ الزمني Systrace.

مصادر