1

最近、Nexus 7 タブレットで Android アプリを開発するタスクを与えられました。これは、wifi を使用して tcp ソケットを介して PC に接続されます。

特に、画像のストリーム (圧縮されていない BMP など) をタブレットに渡す必要がありました。そこで、帯域幅を確認する簡単なテストを行いましたが、結果にがっかりしました。Wi-Fi 信号源のすぐ前にタブレットを置いて座っています。接続速度は毎秒 54Mb と書かれていますが、テスト結果を考慮すると、毎秒最大 16Mb しか得られません。

テストコード:

PC:

    public static void main(String[] args) throws Exception 
    {
        Socket socket = new Socket(androidIpAddr, port);

        OutputStream output = socket.getOutputStream();

        byte[] bytes = new byte[1024 * 100]; // 10K
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = 12;
        } // fill the bytes

        // send them again and again
        while (true) {
            output.write(bytes);
        }
     }

アンドロイド:

public class MyActivity extends Activity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);       

    new Thread(new Runnable() 
      {
        public void run() 
         {
           ServerSocket server = new ServerSocket(port);
           Socket socket = server.accept();

           InputStream input = socket.getInputStream();
           long total = 0;
           long start = System.currentTimeMillis();

           byte[] bytes = new byte[1024 * 100]; // 10K

           // read the data again and again
           while (true) 
           {
             int read = input.read(bytes);
             total += read;
             long cost = System.currentTimeMillis() - start;
             if (cost > 100) 
             {
               double megaBytes = (total / (1024.0 * 1024));
               double seconds = (cost / 1000.0);

               System.out.println("Read " + total + " bytes, speed: " + megaBytes / seconds + " MB/s");
               start = System.currentTimeMillis();
               total = 0;
             }
           }                
        }
    }).start();

}

}

私は何を逃したのですか?

4

1 に答える 1

1

Ok, first Wifi that is 54Mbps is 54 x 1024 x 1024 bits per second. So maximum 6.75 MB/s.

I did the experiment on my network that reports as 54 Mbps in my windows 7 box. Between my Windows 7 box (server) and XP box (client) I achieved 3.0 MB/s using basically the code above.

I then ran the same code between my Nexus 7 (server) and XP (client) and got 0.3 MB/s. So a significant drop-off in speed. I put the following in the constructor of the server thread:

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

and in the MANIFEST:

<uses-permission android:name="android.permission.RAISED_THREAD_PRIORITY"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

I also moved near the router and turned wifi off and then back on. It reported on the Nexus 7 as 54Mbps and when I ran it I got 3.0 MB/s.

So at this point same outcome as Windows 7 and about 44% of theoretical network throughput.

Using more than one TCP connection may allow us to get closer to 100% throughput.

于 2013-02-24T10:56:30.300 に答える