osociety / network_tools

Networking Tools library which can help you discover open ports, devices on subnet and many other things.
https://pub.dev/packages/network_tools
BSD 3-Clause "New" or "Revised" License
45 stars 13 forks source link

Made host scanner 1000 time faster, added flag for pingData and will … #5

Closed guyluz11 closed 2 years ago

guyluz11 commented 2 years ago

…not be shown by default.

Fix for #4

git-elliot commented 2 years ago

I have tested this code and time to run the scan is still same because there is no improvement in code. You have just moved pingdata code in another method and collecting all futures in a list and then again await for the same future. I appreciate your efforts but code does the same thing and no improvements have been seen. Some of the code like creating list and a HostScanner instance is unnecessary.

guyluz11 commented 2 years ago

This is weird, there is clearly something missing here. The old code took 0:03:58.748177, My code took 0:00:01.325691,

The old code image

My code image

I am calling getHostFromPing for all the IPs at once, they are running in parallel in the background. Then await in the end wait for each of them to finish, but already are running in parallel in the background and the result will be much faster.

I am running only the beginning part in the example


  String ip = '192.168.1.1';
  // or You can also get ip using network_info_plus package
  // final String? ip = await (NetworkInfo().getWifiIP());
  final String subnet = ip.substring(0, ip.lastIndexOf('.'));
  final stream = HostScanner.discover(subnet,
      firstSubnet: 1, lastSubnet: 254, progressCallback: (progress) {});

  stream.listen((host) {
    //Same host can be emitted multiple times
    //Use Set<ActiveHost> instead of List<ActiveHost>
    print('Found device HostId: ${host.hostId}');
  }, onDone: () {
    print('Scan completed');
  }); // Don't forget to cancel the stream when not in use.

If you have a discord I will be glad to discuss it with you and take a look.

git-elliot commented 2 years ago

Please also tell me how you are measuring this time. My discord id is elliot-alderson#5736

guyluz11 commented 2 years ago

I have sent a friend request.

Tested the time using a timer as suggested here

final stopwatch = Stopwatch()..start();
doSomething();
print('doSomething() executed in ${stopwatch.elapsed}');
git-elliot commented 2 years ago

Sorry, a huge mistake from my side when trying to test your code. I forgot to fetch your code in the pubspec.yaml. Your code works.