npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-firebase-perf-tracer

v1.1.0

Published

React Native Component Performance Tracer based on Firebase Performance

Downloads

22

Readme

Performance Measure

This is a small API based on Firebase Perform library.

Install

npm install --save react-native-firebase-perf-tracer
// or
yarn add react-native-firebase-perf-tracer

Dependencies

  • react-native 0.61 or above
  • @react-native-firebase/perf 6.3.4 or above
// Install dependencies
npm install --save  @react-native-firebase/perf
// or
yarn add @react-native-firebase/perf

How to use it?

The most complete/automatic implementation can be easily get by the use of the it's Higher-Order Component. Here is a sample on how to use the Higher-Order Component on your components:

// raw-compoent.tsx
import React from 'react';
import { View, Text } from 'react-native';
import { PerformanceMeasureHOCTypeProps } from '../infra/performance/types.d';

interface Props extends PerformanceMeasureHOCTypeProps {
  yourProp: String;
}

class RawComponentClass extends React.Component<Props, {}> {
  constructor(props: Props) {
    super(props);
    // Now you have access to props defined in PerformanceMeasureHOCTypeProp
  }
}

export const RawComponent = withPerformanceMeasure(
  RawComponentClass,
  'RawComponentClass'
);

Now you have access to the pops created by the HOC described as:

interface PerformanceMeasureHOCTypeProps {
	performance: PerformanceMeasureHookType;
	profileTracerReady: () => void;
	profileTracerFail: () => void;
	profileTracerSession: PerformanceMeasureSession | undefined;
}

Breaking it down

performance: A performance instance of a PerformanceMeasureHookType already set to the provided identifier.

profileTracerReady: A simple callback to finish profileTracerSession

profileTracerFail: It finishes the profileTracerSession and set the fail attribute to true.

profileTracerSession: An already running session named profiler, which implements React.Profiler to get mount_time among and updates count, it is set to Firebase Perf Dashboard as [SNAKE_CASE_OF_PROVIDED_IDENTIFIER]::profiler it can be user to set your own attributes and metrics (Limited to 35)

Performance Measure Hook

Use it in your functional components like this:

import { PerformanceMeasureHOCTypeProps, usePerformanceMeasure } from '../infra/performance';
...
export const RawComponentFunction = React.FC<Props>() => {
	const performance = usePerformanceMeasure('RawComponent');
  ...
};

You should provide a identifier to be used in Firebase Perf Dashboard. The performance object implents PerformanceMeasureHookType.

interface PerformanceMeasureHookType {
	startTraceSession(name: string): Promise<PerformanceMeasureSession>;
	completeTraceSession(name: string): void;
	failTraceSession(name: string): void;
	traceMethod: TraceMethodType;
}

Breaking it down:

startTraceSession: Creates a new trace session appending the session name as provided [IDENTIFIER]::[SESSION_NAME] and returns an instance of PerformanceMeasureSession. It is described here.

completeTraceSession: A simple method to finish a tracer session.

failTraceSession: It finishes the tracer session and set the fail attribute to true.

traceMethod: For convenience, you can use this to wrap your methods so they can be tracked automatically.

Here is example of functional component we will be using:

// raw-compoent.tsx
import React from 'react';
import { View, Text } from 'react-native';

interface Props {
  yourProp: String;
}

export const RawComponentFunction = React.FC<Props>() => {
  const [isReady, setComponentReady] = React.useState<boolean>(false);
  const [error, setError] = React.useState<string>();

  const doSomethingReallyExpensive = async () => {
    const response = await getFromNetwork();
    await doSomethingThatTakesAWhile()
    await doSomethingThatTakesAWhileAgainWhyNot();
  }

  const doAnotherExpensiveThing = async () => {
    return await callSomeMethodHere();
  };

  const loadContent = async () => {
    setError();
    try {
      await doSomethingReallyExpensive();
      await doAnotherExpensiveThing();
    } catch (error) {
      setError(error.message);
    } finally {
      setComponentReady(true);
    }
  }

  const renderLoading = () => <View style={styles.loadingOverlay} />;

  const renderContent = () => {
    if (error) {
      return <Error message={error} retry={start} />
    }

    return (
      <View style={styles.container}>
        ...
      </View>
    );
  };

  React.useEffect(() => {
		loadContent();
	}, []);

  return isReady ? renderContent() : renderLoading();
};

Here is after the implementation diff:

// raw-compoent.tsx
import React from 'react';
import { View, Text } from 'react-native';
+ import { PerformanceMeasureHOCTypeProps, withPerformanceMeasure } from '../infra/performance';

- interface Props extends PerformanceMeasureHOCTypeProps {
+ interface Props extends PerformanceMeasureHOCTypeProps {
  yourProp: String;
}

- export const RawComponentFunction: React.FC<Props> = () => {
+ export const RawComponentFunction: React.FC<Props> = withPerformanceMeasure(({
+ 	performance,
+ 	profileTracerReady,
+		profileTracerFail,
+ 	profileTracerSession,
+	}) => {
	const [isReady, setComponentReady] = React.useState<boolean>(false);
	const [error, setError] = React.useState<string>();

	const doSomethingReallyExpensive = async () => {
+		const myTraceSession = await performance.startTraceSession('do_something_really_expensive');
-   const response = await getFromNetwork();
+   const response = await myTraceSession.traceCodeSection(async () => {
+			return await getFromNetwork();
+   }, 'get_from_network');
+
-   await doSomethingThatTakesAWhile()
-   await doSomethingThatTakesAWhileAgainWhyNot();
+   await myTraceSession.traceCodeSection(async () => {
+   	await doSomethingThatTakesAWhile()
+   	await doSomethingThatTakesAWhileAgainWhyNot();
+   }, 'do_something_that_takes_a_while');
+
+    performance.completeTraceSession('do_something_really_expensive');
	};

- const doAnotherExpensiveThing = async () => {
+ const doAnotherExpensiveThing = performance.traceMethod(async () => {
    return await callSomeMethodHere();
-  };
+ }, 'do_another_expensive_thing');

  const loadContent = async () => {
    setError(undefined);
    try {
      await doSomethingReallyExpensive();
      await doAnotherExpensiveThing();
+			profileTracerReady();
    } catch (error) {
      setError(error.message);
+			profileTracerFail();
    } finally {
      setComponentReady(true);
    }
  }

  const renderLoading = () => <View style={styles.loadingOverlay} />;

  const renderContent = () => {
    if (error) {
      return <Error message={error} retry={loadContent} />
    }

    return (
      <View style={styles.container}>
        ...
      </View>
    );
  };

  React.useEffect(() => {
		loadContent();
	}, []);

  return isReady ? renderContent() : renderLoading();
- }
+ }, 'raw_component_id');

Performance Measure Session

abstract class PerformanceMeasureSessionType {
	async startTraceSession(): void;
	completeTraceSession(): void;
	startLoading(loadingName: string): StopLoadingCallback;
	putAttribute(attributeName: string, value: string): void;
	incrementMetric(metricName: string, incrementBy: number): void;
}

License

Contributing

Contributions are very welcome!

From Developers

Made with ❤️ by Zenklub developer team.